JSON: empty arrays


Still on the topic of JSON support [1], I think the trickiest bit is
the handling of empty arrays, and the handling of `array="true"`.

In the case of non-empty arrays, the logic seems pretty simple: each
array element is represented by an XML element with the attribute
`array="true"` on it:

- If the array is a property value in an object, the item has the name
of the property.
- If the array is anonymous, whether top-level or within another
array, the item has a blank name and a `_` XML name.

So far so good, and the examples reflect that.

Now we come to empty arrays. The first example is:

    {"p": []}

The XML mapping is:

    <json object="true"><p array="true"/></json>

This seems reasonable, and we know, when looking at the XML, that the
array is there, but has no value, because element `<p>` has no `type`
or `object` attribute and has no content.

But the outlier is the top-level empty array:


which, in the examples, shows as:

    <json array="true"/>

This seems to break the rule that `array="true"` is present on an XML
element which represents either an actual array element, or which
indicates that the array is empty. For consistency, shouldn't this map
instead to the following?

      <_ name="" array="true"/>

Further arguing for the ugly-but-consistent solution, consider what
should happen if you want to move from an empty array to an array
containing, for example a single element, you get, as per the spec:

      <_ name="" array="true" type="string">red</_>

I think that the rule for going from empty array to an array with one
element should be the same, whether you are at the top-level or not.



[1] https://www.w3.org/community/xformsusers/wiki/XForms_2.0#External_JSON_values

Received on Thursday, 19 November 2015 05:49:05 UTC