2

Closed

XmlNodeConverter and empty arrays

description

Upgrading from json.net 4.0.2 to 4.5.6, I found a very annoying behavior change.
 
A simple xml like below :
<root>
<Reports d1p1:Array="true" xmlns:d1p1="http://james.newtonking.com/projects/json" />
</root>
 
was converted (4.0.2) to :
{
"root": {
    "Reports": []
}
}
 
and now (4.5.6) is converted to :
{
"root": {
    "Reports": [{}]
}
}
 
We have now an empty object in the table whereas I expected to have an empty table.
 
The issue 20604 is the root of the problem.
The code below (version:4.5.6, file:XmlNodeConverter.cs, ligne:l.965):
if (IsArray(node) && node.ChildNodes.All(n => n.LocalName == node.LocalName) && node.ChildNodes.Count > 0) // added node.ChildNodes.Count > 0
 
should be replace by :
if (IsArray(node) && node.ChildNodes.All(n => n.LocalName == node.LocalName) && !HasUserAttribute(node)) // add function HasUserAttribute
 
function HasUserAttribute :
private bool HasUserAttribute(IXmlNode node)
{
foreach (IXmlNode attribute in node.Attributes)
{
    if (attribute.LocalName != "Array" && attribute.Value != JsonNamespaceUri)
    {
        return true;
    }
}
return false;
}
Closed Jun 9, 2012 at 2:38 AM by JamesNK
No, I like it how it is. That attribute is to place a single object in an array always which is what it is doing now. Also I tested and your change means that even when there are multiple empty elements with Array="true" then the JSON array is still empty.

comments