Skip to content

Commit 4705cc5

Browse files
authored
fix null value exception (#15)
* fix null value exception * bug fix --------- Co-authored-by: Wenxi Zeng <[email protected]>
1 parent 83ad7c1 commit 4705cc5

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

Serialization.NET/Segment/Serialization/JsonConvertersForMS.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ private JsonObject ReadObject(ref Utf8JsonReader reader, JsonSerializerOptions o
7676
case JsonTokenType.StartArray:
7777
value = JsonSerializer.Deserialize<JsonArray>(ref reader, options);
7878
break;
79+
case JsonTokenType.Null:
80+
value = JsonNull.Instance;
81+
break;
7982
default:
8083
value = JsonSerializer.Deserialize<JsonPrimitive>(ref reader, options);
8184
break;
@@ -120,6 +123,9 @@ private JsonArray ReadArray(ref Utf8JsonReader reader, JsonSerializerOptions opt
120123
case JsonTokenType.StartArray:
121124
value = JsonSerializer.Deserialize<JsonArray>(ref reader, options);
122125
break;
126+
case JsonTokenType.Null:
127+
value = JsonNull.Instance;
128+
break;
123129
default:
124130
value = JsonSerializer.Deserialize<JsonPrimitive>(ref reader, options);
125131
break;

Serialization.NET/Segment/Serialization/JsonElement.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ IEnumerator IEnumerable.GetEnumerator()
187187
public JsonElement this[string key]
188188
{
189189
get => Content[key];
190-
set => Content[key] = value;
190+
set => Content[key] = value ?? JsonNull.Instance;
191191
}
192192
}
193193

@@ -240,7 +240,7 @@ IEnumerator IEnumerable.GetEnumerator()
240240
public JsonElement this[int index]
241241
{
242242
get => Content[index];
243-
set => Content[index] = value;
243+
set => Content[index] = value ?? JsonNull.Instance;
244244
}
245245
}
246246

Tests/JsonUtilityTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class JsonUtilityTest
1717
["bool"] = true,
1818
["datetime"] = "2015-12-10T04:08:31.905Z",
1919
["date"] = s_date,
20+
["null"] = null,
2021
["object"] = new JsonObject
2122
{
2223
["another object"] = "obj"
@@ -31,7 +32,7 @@ public class JsonUtilityTest
3132
};
3233

3334
private readonly string _jsonStr =
34-
"{\"int\": 1,\"float\": 1,\"long\": 1,\"double\": 1,\"string\": \"1\",\"bool\": true,\"datetime\": \"2015-12-10T04:08:31.905Z\",\"date\": \"" + s_date + "\",\"object\": {\"another object\": \"obj\"},\"array\": [1,1,1,1,\"1\",true,{\"object in array\": \"obj\"}]}";
35+
"{\"int\": 1,\"float\": 1,\"long\": 1,\"double\": 1,\"string\": \"1\",\"bool\": true,\"datetime\": \"2015-12-10T04:08:31.905Z\",\"date\": \"" + s_date + "\",\"null\": null,\"object\": {\"another object\": \"obj\"},\"array\": [1,1,1,1,\"1\",true,{\"object in array\": \"obj\"}]}";
3536

3637
[Fact]
3738
public void Test_ToJson()

0 commit comments

Comments
 (0)