The following code (please note empty array):
string xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'>
<role json:Array='true' />
</person>";
var doc = new XmlDocument();
doc.LoadXml(xml);
var json = JsonConvert.SerializeXmlNode(doc);
returns this json (please note role 'array', it has an empty object [{}] in it):
Assert.AreEqual(@"{""person"":{""@id"":""1"",""role"":[{}]}}", json);
However, we need this instead (we don't want empty object [] in the array) -and implemented a fix with unit tests passing-:
Assert.AreEqual(@"{""person"":{""@id"":""1"",""role"":[]}}", json);
Is there a way to achieve this in the current implementation?
Not quite sure if this is fair to say, but if there is no element in the array, I would expect it to be completely empty. I would like to learn about the justification if this is not a bug :)
It looks like the XML has an element with no children but the JSON has an array with one child. They don't look equivalent to me.
The role element that json:Array='true' is on is an element in the array which is why you get that result.
Is there a way to get an empty array like "role": [] in the output?.
No.
when the "array" is dynamically generated and it's empty, {} tricks the parser at the other end as though there is an object in it. that just doesn't sound right, I'm a bit surprised how easily you closed the issue.
It doesn't sound right because it's not designed for empty arrays. The XML to JSON conversion only works under the strict set of rules listed in the documentation.
role= new List
Most helpful comment
Is there a way to get an empty array like
"role": []in the output?.