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?