Skip to content

Struct properties that implement IList<T> are unable to hold values on Deserialization #1998

Description

@jozkee

Properties of a custom struct type that implements IList<T> are unable to read values, this is because when we try to add the element into the list, we do it on a copy of said property which is returned by jsonPropertyInfo.GetValueAsObject(state.Current.ReturnValue).

This issue is probably duplicated also on custom structs that implement Dictionary or IDictionary.
This issue does not occur when the value type list is the root object.

I would wait to see if the new converter refactor indirectly fixes this issue.
cc @steveharter, @layomia, @ahsonkhan

Repro:

[Fact]publicstaticvoidStructListPropertyDoesNotHoldElements(){stringjson=$"{{\"StructList\":[10,20,30,40,50,60,70,80]}}";StructListWrapperwrapper=JsonSerializer.Deserialize<StructListWrapper>(json);Assert.Equal(8,wrapper.StructList.Count);}privateclassStructListWrapper{publicStructList<int>StructList{get;set;}}privatestructStructList<T>:IList<T>{privateList<T>_list;publicTthis[intindex]{get{InitializeIfNull();return_list[index];}set{InitializeIfNull();_list[index]=value;}}publicintCount=>_list==null?0:_list.Count;publicboolIsReadOnly=>false;privatevoidInitializeIfNull(){if(_list==null){_list=newList<T>();}}publicvoidAdd(Titem){InitializeIfNull();_list.Add(item);}publicvoidClear(){_list.Clear();}publicboolContains(Titem){return_list.Contains(item);}publicvoidCopyTo(T[]array,intarrayIndex){thrownewNotImplementedException();}publicIEnumerator<T>GetEnumerator(){return_list.GetEnumerator();}publicintIndexOf(Titem){return_list.IndexOf(item);}publicvoidInsert(intindex,Titem){_list.Insert(index,item);}publicboolRemove(Titem){return_list.Remove(item);}publicvoidRemoveAt(intindex){_list.RemoveAt(index);}IEnumeratorIEnumerable.GetEnumerator(){returnGetEnumerator();}}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions