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
NullReference when JsonExtensionData dictionary have a custom JsonConverter #32903
privateclassClassWithExtensionData{[JsonExtensionData]publicDictionary<string,object>Overflow{get;set;}}publicclassDictionaryOverflowConverter:JsonConverter<Dictionary<string,object>>{publicoverrideDictionary<string,object>Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){thrownewNotImplementedException();}publicoverridevoidWrite(Utf8JsonWriterwriter,Dictionary<string,object>value,JsonSerializerOptionsoptions){// Do something.}}[Fact]publicstaticvoidQuickTest(){varroot=newClassWithExtensionData();root.Overflow=newDictionary<string,object>();root.Overflow.Add("test","TestValue");varopts=newJsonSerializerOptions();opts.Converters.Add(newDictionaryOverflowConverter());stringjson=JsonSerializer.Serialize(root,opts);}
@ahsonkhan Overflow properties does not come on a particular order when reading the JSON and the Read method must return a Dictionary<string, JsonElement> or Dictionary<string, object> while overflow properties are parsed to JsonElement entries in such dictionary. That is why we don't use it on deserialization.
can only be used on serialization. Deserialization is handled by the serializer.
That's a bit strange. Is there a reason for that and should we (can we?) fix it?
@ahsonkhan it's because we may not have all the extra JSON in one go - we'd have to parse and wrap all the extension properties in one JSON object, or pass the entire payload to the converter to be filtered.
Activity
layomia commentedon Feb 28, 2020
Note: custom converters for extension properties can only be used on serialization. Deserialization is handled by the serializer.
ahsonkhan commentedon Mar 2, 2020
That's a bit strange. Is there a reason for that and should we (can we?) fix it?
jozkee commentedon Mar 2, 2020
@ahsonkhan Overflow properties does not come on a particular order when reading the JSON and the
Readmethod must return aDictionary<string, JsonElement>orDictionary<string, object>while overflow properties are parsed toJsonElemententries in such dictionary. That is why we don't use it on deserialization.layomia commentedon Mar 2, 2020
@ahsonkhan it's because we may not have all the extra JSON in one go - we'd have to parse and wrap all the extension properties in one JSON object, or pass the entire payload to the converter to be filtered.
FWIW Newtonsoft does not honor custom converters for both reading and writing extension data: https://dotnetfiddle.net/qwMrPS. Writing seems doable, so I think the action for this issue is to fix the NRE and make sure custom converters are honored on serialization.
@steveharter, it seems that the assumption in
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
Line 283 in 0bf721a
RezaJooyandeh commentedon Apr 6, 2020
I think for writing it makes sense to support custom serialization.
Fix NullReference on JsonExtensionData (#34569)