Skip to content

Commit

Permalink
Fix System.Object serialization with custom number handling (#62020)
Browse files Browse the repository at this point in the history
* Fix System.Object serialization with custom number handling

* fix typos
  • Loading branch information
eiriktsarpalis authored Nov 30, 2021
1 parent cc7b99b commit fe68bf8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
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,21 @@ public static void TooLittleJsonFails(string json)

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

// Regression test for https://github.com/dotnet/runtime/issues/61995
[Theory]
[InlineData(JsonUnknownTypeHandling.JsonElement, typeof(JsonElement))]
[InlineData(JsonUnknownTypeHandling.JsonNode, typeof(JsonNode))]
public static void ReadObjectWithNumberHandling(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);
}

// Regression test for https://github.com/dotnet/runtime/issues/61995
[Fact]
public static void WriteObjectWithNumberHandling()
{
var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString };
JsonSerializer.Serialize(new object(), options);
}
}
}

0 comments on commit fe68bf8

Please sign in to comment.