Json.NET XML Conversion and TypeNameHandling.Arrays

Immediate problem

This problem occurred while working with another library using Json.NET. We heavily used TypeNameHandling.Arrays when serializing C # objects in JSON, as well as deserializing them on the other end of the wire in our client application.

However, it seems that the Json.NET XmlNodeConverter does not work with this parameter, discarding errors when JSON is deserialized, for example:

 { 'people': { '$type': 'System.Collections.Generic.List`1[[MyNamespace.Person, MyDll]], mscorlib', '$values': [ { 'name': 'Alan' }, { 'name': 'Bob' } ] } } 

Cause

The exception was thrown because Json.NET is trying to interpret $values as a string attribute, and not as child nodes. We get a null reference exception when .ToString() is called with a null value around XmlNodeConverter.cs: 1367.

Development

The real problem here may be related to the way Json.NET handles arrays in XML: it does not create a wrapper like the one below:

 <people json:type="System.Collections.Generic.List`1[[MyNamespace.Person, MyDll]], mscorlib"> <person> <name>Alan</name> </person> <person> <name>Bob</name> </person> </people> 

... where he can add a custom json:type attribute, but instead relies on an implicit grouping of elements that share the same tag:

 <person> <name>Alan</name> </item> <person> <name>Bob</name> </item> 

This behavior seems a little unintuitive for my unprepared look - I expect the parent element to be included in the XML, and not without pauses. (This is probably also the reason why empty and singleton arrays need extra work, such as documentation in many threads here and on the Json.NET forums.)

Solutions..?

Here I appeal to the community ..!

  • Do you know any workarounds, perhaps some tweaks that make both play well?
  • Do you agree that dropping the parent node is the right way to serialize arrays?
+6
source share
1 answer

I get the impression that you don’t have much access to the way JSON.Net is used, because you mention that it is used by another library. If you have access, you can consider options such as deserializing an anonymous type, and then adding the appropriate code to initialize your class. (See: Deserialize json with json.net C # )

The JSON.net documentation mentions the following: http://james.newtonking.com/json/help/index.html?topic=html/ConvertingJSONandXML.htm

"Multiple nodes of the same name at the same level are grouped into an array."

"If the XML generated from JSON does not match what you want, you will need to convert it manually. The best way to do this is to load the JSON into a LINQ to JSON object such as a JObject or JArray, and then use LINQ to create an XDocument. The opposite process Using LINQ with XDocument to create a JObject or JArray also works. Learn more about using LINQ to JSON with LINQ here. "

For your specific questions:

  • There are no direct workarounds for deserialization to work properly in JSON.net with such structured arrays if you cannot edit the deserialization code.

  • Both methods are valid. Json.NET tends to target JSON and XML, which look like something you can write manually, while .net serializers are designed to ensure there is no data loss when deserializing again in .Net. All in all, it's ideal to just use the same library everywhere - in practice, Json.NET is a popular choice.

0
source

Source: https://habr.com/ru/post/970444/


All Articles