Skip to content

Commit 4a9cdf1

Browse files
Brent Schmaltzbrentschmaltz
Brent Schmaltz
authored andcommitted
added support for bool, ulong and fixed datetime issue.
1 parent 719b3c0 commit 4a9cdf1

File tree

6 files changed

+333
-604
lines changed

6 files changed

+333
-604
lines changed

src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs

+14-23
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,23 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
213213
// the below here should only be simple types, string, int, ...
214214
else if (typeof(T) == typeof(string))
215215
{
216-
return (T)((object)obj.ToString());
216+
if (objType == typeof(DateTime))
217+
return (T)((object)((DateTime)obj).ToString("o", CultureInfo.InvariantCulture));
218+
219+
return (T)((object)obj.ToString());
220+
}
221+
else if (typeof(T) == typeof(bool))
222+
{
223+
if (bool.TryParse(obj.ToString(), out bool value))
224+
return (T)(object)value;
217225
}
218226
else if (typeof(T) == typeof(int))
219227
{
220-
if (objType == typeof(int))
221-
return (T)obj;
222-
223228
if (int.TryParse(obj.ToString(), out int value))
224229
return (T)(object)value;
225230
}
226231
else if (typeof(T) == typeof(long))
227232
{
228-
if (objType == typeof(long))
229-
return (T)obj;
230-
231233
if (objType == typeof(int))
232234
return (T)(object)(long)(int)obj;
233235

@@ -255,7 +257,6 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
255257

256258
return (T)(object)new Collection<string> { obj.ToString() };
257259
}
258-
259260
else if (typeof(T) == typeof(object[]))
260261
return (T)(object)new object[] { obj };
261262

@@ -267,9 +268,6 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
267268

268269
else if (typeof(T) == typeof(DateTime))
269270
{
270-
if (objType == typeof(DateTime))
271-
return (T)obj;
272-
273271
if (DateTime.TryParse(obj.ToString(), out DateTime value))
274272
return (T)(object)value;
275273
}
@@ -294,33 +292,26 @@ internal T GetValue<T>(string key, bool throwEx, out bool found)
294292
}
295293
else if (typeof(T) == typeof(double))
296294
{
297-
if (objType == typeof(double))
298-
return (T)obj;
299-
300295
if(double.TryParse(obj.ToString(), out double value))
301296
return (T)(object)value;
302297
}
303298
else if (typeof(T) == typeof(uint))
304299
{
305-
if (objType == typeof(uint))
306-
return (T)obj;
307-
308300
if (uint.TryParse(obj.ToString(), out uint value))
309301
return (T)(object)value;
310302
}
303+
else if (typeof(T) == typeof(ulong))
304+
{
305+
if (ulong.TryParse(obj.ToString(), out ulong value))
306+
return (T)(object)value;
307+
}
311308
else if (typeof(T) == typeof(float))
312309
{
313-
if (objType == typeof(float))
314-
return (T)obj;
315-
316310
if (float.TryParse(obj.ToString(), out float value))
317311
return (T)(object)value;
318312
}
319313
else if (typeof(T) == typeof(decimal))
320314
{
321-
if (objType == typeof(decimal))
322-
return (T)obj;
323-
324315
if (decimal.TryParse(obj.ToString(), out decimal value))
325316
return (T)(object)value;
326317
}

src/Microsoft.IdentityModel.Tokens/Json/JsonSerializerPrimitives.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ public static void WriteObject(ref Utf8JsonWriter writer, string key, object obj
825825
else if (obj is bool b)
826826
writer.WriteBoolean(key, b);
827827
else if (obj is DateTime dt)
828-
writer.WriteString(key, dt.ToUniversalTime());
828+
writer.WriteString(key, dt.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture));
829829
else if (typeof(IDictionary).IsAssignableFrom(objType))
830830
{
831831
IDictionary dictionary = (IDictionary)obj;

test/Microsoft.IdentityModel.JsonWebTokens.Tests/GetPayloadValueTheoryData.cs

+2
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public GetPayloadValueTheoryData(string testId) : base(testId)
2121
public Type PropertyType { get; set; }
2222

2323
public object PropertyValue { get; set; }
24+
25+
public string Json { get; set; }
2426
}
2527
}

0 commit comments

Comments
 (0)