You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Text.Json unable to deserialize an object that implement IEnumerable but is itself not a list #81084
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:
public class UnitTest1
{
[Fact]
public void Test1()
{
string validJson = @"
{
""List1"": [{""Test"": ""a""}],
""List2"": [{""Test"": ""a""}]
}";
MyClass obj = JsonSerializer.Deserialize<MyClass>(validJson);
}
}
class MyClass //: IEnumerable<TestClass>
{
public List<TestClass> List1 { get; set; } = new List<TestClass>();
public List<TestClass> List2 { get; set; } = new List<TestClass>();
public IEnumerator<TestClass> GetEnumerator()
{
return List1.Concat(List2).GetEnumerator();
}
//IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
class TestClass
{
public string Test { get; set; }
}
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.
deserializer doesn't know how to add values to your collection (since you're implementing IEnumerable assumption is that it should deserialize from array and deserializer needs to add elements somehow)
Marking as needs-author-action because given current information we should close this issue as duplicate. Please close or add further details (bot will automatically close this issue if there is no response)
@krwq thank you for the reference to #63791. This looks like it'll resolve the issue. Is there any workaround to get an implementation like #63791 working with a custom converter? If so, what would this converter look like?
ghost added
needs-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Implementation could look something like this (done very minimal testing on that, just showing idea):
classMyClassConverter:JsonConverter<MyClass>{publicoverrideMyClass?Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){MyClassobj=new();while(reader.TokenType!=JsonTokenType.EndObject){if(reader.TokenType==JsonTokenType.PropertyName){string?propertyName=reader.GetString();reader.Read();List<TestClass>?list=JsonSerializer.Deserialize<List<TestClass>>(refreader,options);if(propertyName==nameof(MyClass.List1)){obj.List1=list;}elseif(propertyName==nameof(MyClass.List2)){obj.List2=list;}else{// we ignore unknown properties by default}}reader.Read();}returnobj;}publicoverridevoidWrite(Utf8JsonWriterwriter,MyClassvalue,JsonSerializerOptionsoptions){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:
Activity
krwq commentedon Jan 24, 2023
You'll need to implement CustomConverter for your type.
The reason this is not serializable is following:
krwq commentedon Jan 24, 2023
Marking as
needs-author-actionbecause given current information we should close this issue as duplicate. Please close or add further details (bot will automatically close this issue if there is no response)RyanGaudion commentedon Jan 25, 2023
@krwq thank you for the reference to #63791. This looks like it'll resolve the issue. Is there any workaround to get an implementation like #63791 working with a custom converter? If so, what would this converter look like?
krwq commentedon Jan 25, 2023
Implementation could look something like this (done very minimal testing on that, just showing idea):
then you add that to options which you pass to Serialize/Deserialize: