Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 4530596

Browse files
committed
Handle null map
1 parent 5e274ee commit 4530596

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ServiceStack.Text/JsonObject.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ public static T JsonTo<T>(this Dictionary<string, string> map, string key)
1717
/// <summary>
1818
/// Get JSON string value converted to T
1919
/// </summary>
20-
public static T Get<T>(this Dictionary<string, string> map, string key, T defaultValue = default(T))
20+
public static T Get<T>(this Dictionary<string, string> map, string key, T defaultValue = default)
2121
{
22+
if (map == null)
23+
return default;
2224
return map.TryGetValue(key, out var strVal) ? JsonSerializer.DeserializeFromString<T>(strVal) : defaultValue;
2325
}
2426

2527
public static T[] GetArray<T>(this Dictionary<string, string> map, string key)
2628
{
29+
if (map == null)
30+
return TypeConstants<T>.EmptyArray;
2731
return map.TryGetValue(key, out var value)
2832
? (map is JsonObject obj ? value.FromJson<T[]>() : value.FromJsv<T[]>())
2933
: TypeConstants<T>.EmptyArray;
@@ -34,6 +38,8 @@ public static T[] GetArray<T>(this Dictionary<string, string> map, string key)
3438
/// </summary>
3539
public static string Get(this Dictionary<string, string> map, string key)
3640
{
41+
if (map == null)
42+
return null;
3743
return map.TryGetValue(key, out var strVal)
3844
? JsonTypeSerializer.Instance.UnescapeString(strVal)
3945
: null;
@@ -56,11 +62,11 @@ public static List<T> ConvertAll<T>(this JsonArrayObjects jsonArrayObjects, Func
5662
return results;
5763
}
5864

59-
public static T ConvertTo<T>(this JsonObject jsonObject, Func<JsonObject, T> converFn)
65+
public static T ConvertTo<T>(this JsonObject jsonObject, Func<JsonObject, T> convertFn)
6066
{
6167
return jsonObject == null
62-
? default(T)
63-
: converFn(jsonObject);
68+
? default
69+
: convertFn(jsonObject);
6470
}
6571

6672
public static Dictionary<string, string> ToDictionary(this JsonObject jsonObject)

0 commit comments

Comments
 (0)