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

[release/6.0] Fix System.Object serialization with custom number handling #62193

Merged
merged 3 commits into from
Jan 8, 2022
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 @@ -7,11 +7,6 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class ObjectConverter : JsonConverter<object?>
{
public ObjectConverter()
{
IsInternalConverterForNumberType = true;
}

public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (options.UnknownTypeHandling == JsonUnknownTypeHandling.JsonElement)
Expand Down Expand Up @@ -50,15 +45,5 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, object? va

runtimeConverter.WriteAsPropertyNameCoreAsObject(writer, value, options, isWritingExtensionDataProperty);
}

internal override object? ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)
{
if (options.UnknownTypeHandling == JsonUnknownTypeHandling.JsonElement)
{
return JsonElement.ParseValue(ref reader);
}

return JsonNodeConverter.Instance.Read(ref reader, typeof(object), options);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json.Nodes;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -655,5 +656,20 @@ public static void TooLittleJsonFails(string json)

Assert.Equal(0, reader.BytesConsumed);
}

[Theory]
[InlineData(JsonUnknownTypeHandling.JsonElement, typeof(JsonElement))]
[InlineData(JsonUnknownTypeHandling.JsonNode, typeof(JsonNode))]
public static void ReadSystemObjectWithNumberHandling(JsonUnknownTypeHandling unknownTypeHandling, Type expectedType)
{
var options = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString,
UnknownTypeHandling = unknownTypeHandling
};

object result = JsonSerializer.Deserialize<object>(@"{ ""key"" : ""42"" }", options);
Assert.IsAssignableFrom(expectedType, result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,13 @@ public static void EscapingShouldntStackOverflow()

Assert.Equal("{\"name\":\"\u6D4B\u8A6611\"}", result);
}

[Fact]
public static void WriteSystemObjectWithNumberHandling()
{
var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString };
string result = JsonSerializer.Serialize(new object(), options);
Assert.Equal("{}", result);
}
}
}