Skip to content

Commit fe68bf8

Browse files
Fix System.Object serialization with custom number handling (#62020)
* Fix System.Object serialization with custom number handling * fix typos
1 parent cc7b99b commit fe68bf8

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/ObjectConverter.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ namespace System.Text.Json.Serialization.Converters
77
{
88
internal sealed class ObjectConverter : JsonConverter<object?>
99
{
10-
public ObjectConverter()
11-
{
12-
IsInternalConverterForNumberType = true;
13-
}
14-
1510
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1611
{
1712
if (options.UnknownTypeHandling == JsonUnknownTypeHandling.JsonElement)
@@ -50,15 +45,5 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, object? va
5045

5146
runtimeConverter.WriteAsPropertyNameCoreAsObject(writer, value, options, isWritingExtensionDataProperty);
5247
}
53-
54-
internal override object? ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, JsonSerializerOptions options)
55-
{
56-
if (options.UnknownTypeHandling == JsonUnknownTypeHandling.JsonElement)
57-
{
58-
return JsonElement.ParseValue(ref reader);
59-
}
60-
61-
return JsonNodeConverter.Instance.Read(ref reader, typeof(object), options);
62-
}
6348
}
6449
}

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Object.ReadTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
8+
using System.Text.Json.Nodes;
89
using Xunit;
910

1011
namespace System.Text.Json.Serialization.Tests
@@ -655,5 +656,21 @@ public static void TooLittleJsonFails(string json)
655656

656657
Assert.Equal(0, reader.BytesConsumed);
657658
}
659+
660+
// Regression test for https://github.com/dotnet/runtime/issues/61995
661+
[Theory]
662+
[InlineData(JsonUnknownTypeHandling.JsonElement, typeof(JsonElement))]
663+
[InlineData(JsonUnknownTypeHandling.JsonNode, typeof(JsonNode))]
664+
public static void ReadObjectWithNumberHandling(JsonUnknownTypeHandling unknownTypeHandling, Type expectedType)
665+
{
666+
var options = new JsonSerializerOptions
667+
{
668+
NumberHandling = JsonNumberHandling.AllowReadingFromString,
669+
UnknownTypeHandling = unknownTypeHandling
670+
};
671+
672+
object result = JsonSerializer.Deserialize<object>(@"{ ""key"" : ""42"" }", options);
673+
Assert.IsAssignableFrom(expectedType, result);
674+
}
658675
}
659676
}

src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Object.WriteTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,13 @@ public static void EscapingShouldntStackOverflow()
138138

139139
Assert.Equal("{\"name\":\"\u6D4B\u8A6611\"}", result);
140140
}
141+
142+
// Regression test for https://github.com/dotnet/runtime/issues/61995
143+
[Fact]
144+
public static void WriteObjectWithNumberHandling()
145+
{
146+
var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString };
147+
JsonSerializer.Serialize(new object(), options);
148+
}
141149
}
142150
}

0 commit comments

Comments
 (0)