Skip to content

Commit abe7d08

Browse files
github-actions[bot]eiriktsarpalisericstj
authored
[release/6.0] Fix System.Object serialization with custom number handling (#62193)
* Fix System.Object serialization with custom number handling * fix typos * Address feedback Co-authored-by: Eirik Tsarpalis <[email protected]> Co-authored-by: Eric StJohn <[email protected]>
1 parent dc56b08 commit abe7d08

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-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: 16 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,20 @@ public static void TooLittleJsonFails(string json)
655656

656657
Assert.Equal(0, reader.BytesConsumed);
657658
}
659+
660+
[Theory]
661+
[InlineData(JsonUnknownTypeHandling.JsonElement, typeof(JsonElement))]
662+
[InlineData(JsonUnknownTypeHandling.JsonNode, typeof(JsonNode))]
663+
public static void ReadSystemObjectWithNumberHandling(JsonUnknownTypeHandling unknownTypeHandling, Type expectedType)
664+
{
665+
var options = new JsonSerializerOptions
666+
{
667+
NumberHandling = JsonNumberHandling.AllowReadingFromString,
668+
UnknownTypeHandling = unknownTypeHandling
669+
};
670+
671+
object result = JsonSerializer.Deserialize<object>(@"{ ""key"" : ""42"" }", options);
672+
Assert.IsAssignableFrom(expectedType, result);
673+
}
658674
}
659675
}

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+
[Fact]
143+
public static void WriteSystemObjectWithNumberHandling()
144+
{
145+
var options = new JsonSerializerOptions { NumberHandling = JsonNumberHandling.AllowReadingFromString };
146+
string result = JsonSerializer.Serialize(new object(), options);
147+
Assert.Equal("{}", result);
148+
}
141149
}
142150
}

0 commit comments

Comments
 (0)