Skip to content
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,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);
}
}
}