-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json unable to deserialize an object that implement IEnumerable but is itself not a list #81084
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionJsonSerializer.Deserialize is unable to deserialize a class that contains it's own properties will also implementing IEnumerable Reproduction StepsPlease see the test code here:
This code passes, but when uncommenting out the 2 comments in MyClass - the tests then fail. Expected behaviorI expected adding an interface and method to an object to not effect it's ability to be deserialized Actual behaviorThe deserialization fails with this error Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
You'll need to implement CustomConverter for your type. The reason this is not serializable is following:
|
This issue has been marked |
Marking as |
Implementation could look something like this (done very minimal testing on that, just showing idea): class MyClassConverter : JsonConverter<MyClass>
{
public override MyClass? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
MyClass obj = new();
while (reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
string? propertyName = reader.GetString();
reader.Read();
List<TestClass>? list = JsonSerializer.Deserialize<List<TestClass>>(ref reader, options);
if (propertyName == nameof(MyClass.List1))
{
obj.List1 = list;
}
else if (propertyName == nameof(MyClass.List2))
{
obj.List2 = list;
}
else
{
// we ignore unknown properties by default
}
}
reader.Read();
}
return obj;
}
public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WritePropertyName(nameof(MyClass.List1));
JsonSerializer.Serialize(writer, value.List1, options);
writer.WritePropertyName(nameof(MyClass.List2));
JsonSerializer.Serialize(writer, value.List2, options);
writer.WriteEndObject();
}
} then you add that to options which you pass to Serialize/Deserialize: JsonSerializerOptions options = new()
{
Converters = { new MyClassConverter() }
}; |
Description
JsonSerializer.Deserialize is unable to deserialize a class that contains it's own properties will also implementing IEnumerable
Reproduction Steps
Please see the test code here:
This code passes, but when uncommenting out the 2 comments in MyClass - the tests then fail.
Expected behavior
I expected adding an interface and method to an object to not effect it's ability to be deserialized
Actual behavior
The deserialization fails with this error
System.Text.Json.JsonException : The JSON value could not be converted to MyClass. Path: $ | LineNumber: 1 | BytePositionInLine: 1.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: