-
Notifications
You must be signed in to change notification settings - Fork 5k
options.GetConverter used inside other JsonConverter causes "Deserialization of types without[...]" error #94538
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
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionGetting a converter via Reproduction Stepsusing System.Text.Json;
using System.Text.Json.Serialization;
public class TestDocument
{
public TestType TestType { get; init; }
}
public class TestType { }
public class TestConverter : JsonConverter<TestType>
{
private readonly JsonConverter<TestType> _innerConverter;
public TestConverter(JsonConverter<TestType> innerConverter)
{
_innerConverter = innerConverter;
}
public override TestType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
_innerConverter.Read(ref reader, typeToConvert, options);
public override void Write(Utf8JsonWriter writer, TestType value, JsonSerializerOptions options) =>
throw new NotImplementedException();
}
public class InnerConverterTests
{
[Fact]
public void InnerConverter_Works()
{
var json = """{ "TestType": {} }""";
var options = new JsonSerializerOptions();
var innerConverter = (JsonConverter<TestType>)options.GetConverter(typeof(TestType));
var testConverter = new TestConverter(innerConverter);
options.Converters.Add(testConverter);
// This next line produces the error
var testDocument = JsonSerializer.Deserialize<TestDocument>(json, options);
}
} Expected behaviorWith the given example, "standard" deserialization, as if I didn't provide any Actual behaviorThrows a Maybe this is a weird thing to do, but the error message is at least still misleading then. Regression?I don't know. Known WorkaroundsNo response Configuration.NET 7.0.401 Other informationNo response
|
This is a known problem, unfortunately certain built-in converters cannot function independently of contract metadata. As a workaround you could try wrapping the var innerTypeInfo = (JsonTypeInfo<TestType>)JsonSerializerOptions.Default.GetTypeInfo(typeof(TestType));
var options = new JsonSerializerOptions
{
Converters = { new TestConverter(innerTypeInfo) }
};
JsonSerializer.Deserialize<TestDocument>(json, options);
public class TestConverter : JsonConverter<TestType>
{
private readonly JsonTypeInfo<TestType> _innerTypeInfo;
public TestConverter(JsonTypeInfo<TestType> innerTypeInfo)
{
_innerTypeInfo = innerTypeInfo;
}
public override TestType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
JsonSerializer.Deserialize(ref reader, _innerTypeInfo);
public override void Write(Utf8JsonWriter writer, TestType value, JsonSerializerOptions options) =>
throw new NotImplementedException();
} Duplicate of #50205. |
Description
Getting a converter via
GetConverter()
and using it inside another JsonConverter throws a NotSupportedException.Reproduction Steps
Expected behavior
With the given example, "standard" deserialization, as if I didn't provide any
JsonSerializerOptions
.Actual behavior
Throws a
System.NotSupportedException
with message "Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported."Maybe this is a weird thing to do, but the error message is at least still misleading then.
Regression?
I don't know.
Known Workarounds
No response
Configuration
.NET 7.0.401
win11
x64
Other information
No response
The text was updated successfully, but these errors were encountered: