Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix JsonNode deserialising from null #85463

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public override void Write(Utf8JsonWriter writer, JsonArray value, JsonSerialize
{
case JsonTokenType.StartArray:
return ReadList(ref reader, options.GetNodeOptions());
case JsonTokenType.Null:
return null;
default:
Debug.Assert(false);
throw ThrowHelper.GetInvalidOperationException_ExpectedArray(reader.TokenType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public override void Write(Utf8JsonWriter writer, JsonNode? value, JsonSerialize
return ObjectConverter.Read(ref reader, typeToConvert, options);
case JsonTokenType.StartArray:
return ArrayConverter.Read(ref reader, typeToConvert, options);
case JsonTokenType.Null:
return null;
default:
Debug.Assert(false);
throw new JsonException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public override void Write(Utf8JsonWriter writer, JsonObject value, JsonSerializ
{
case JsonTokenType.StartObject:
return ReadObject(ref reader, options.GetNodeOptions());
case JsonTokenType.Null:
return null;
default:
Debug.Assert(false);
throw ThrowHelper.GetInvalidOperationException_ExpectedObject(reader.TokenType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections;
using System.Collections.Generic;
using Xunit;

namespace System.Text.Json.Nodes.Tests
Expand Down Expand Up @@ -39,7 +41,20 @@ public static void JsonTypes_Deserialize()
Assert.IsAssignableFrom<JsonValue>(JsonNode.Parse("\"str\""));
Assert.IsAssignableFrom<JsonValue>(JsonNode.Parse(ToUtf8("\"str\"")));
Assert.IsType<JsonElement>(JsonSerializer.Deserialize<object>("\"str\""));

JsonType_Deserializes_Null<JsonNode>();
JsonType_Deserializes_Null<JsonArray>();
JsonType_Deserializes_Null<JsonObject>();
}

private static void JsonType_Deserializes_Null<TNode>() where TNode : JsonNode
{
Assert.Null(JsonSerializer.Deserialize<TNode>("null"));
Assert.Collection(JsonSerializer.Deserialize<TNode[]>("[null]"), Assert.Null);
Assert.Collection(JsonSerializer.Deserialize<IReadOnlyDictionary<string, TNode>>("{ \"Value\": null }"), kv => Assert.Null(kv.Value));
Assert.Null(JsonSerializer.Deserialize<ObjectWithNodeProperty<TNode>>("{ \"Value\": null }").Value);
}
private record ObjectWithNodeProperty<TNode>(TNode Value) where TNode : JsonNode;

[Fact]
public static void AsMethods_Throws()
Expand Down