I hope some of you find this interesting.
One feature that JSON offers is an array-like or list syntax by placing items inside square brackets. In contrast, XML feels a little flat. In XML, you can separate values with spaces, tabs or commas as CDATA or PCDATA but the application needs to know to pull out the text chunk and parse it into something more meaningful. I just like the JSON list syntax.
On the other hand, XML does distinguish attributes from elements. For nested content, the attributes are parsed first. This is a bonus because you can designate an attribute to have type information that provides clues as to what content follows.
Here's a contrived example where you have a binary image content that could be JPEG, PNG or TIFF.
JSON:
XML:
The two examples look very equivalent. The trouble with JSON is JSON makes no guarantees of field order. The "type" field might not appear before the "content" field. That content field can be huge. Your parser must cache the contents while waiting to see what type it is. Also consider more elaborate nested content involving more than just two fields.
On the XML side, if you're using SAX or Expat or STAX, you'll see the "type" attribute before you get to the binary blob.
This is one particular case where I think XML is easier to use over JSON.
Here's an example of someone fixing the order of JSON output.
In case anyone is wondering, when I do use JSON - it is almost always with nlohmann.
Happy Friday!
One feature that JSON offers is an array-like or list syntax by placing items inside square brackets. In contrast, XML feels a little flat. In XML, you can separate values with spaces, tabs or commas as CDATA or PCDATA but the application needs to know to pull out the text chunk and parse it into something more meaningful. I just like the JSON list syntax.
On the other hand, XML does distinguish attributes from elements. For nested content, the attributes are parsed first. This is a bonus because you can designate an attribute to have type information that provides clues as to what content follows.
Here's a contrived example where you have a binary image content that could be JPEG, PNG or TIFF.
JSON:
{"Type": "TIFF", "Content":"binary-content-coded-as-base64"}XML:
<Image type="TIFF">binary-content-coded-as-base64</Image>The two examples look very equivalent. The trouble with JSON is JSON makes no guarantees of field order. The "type" field might not appear before the "content" field. That content field can be huge. Your parser must cache the contents while waiting to see what type it is. Also consider more elaborate nested content involving more than just two fields.
On the XML side, if you're using SAX or Expat or STAX, you'll see the "type" attribute before you get to the binary blob.
This is one particular case where I think XML is easier to use over JSON.
Here's an example of someone fixing the order of JSON output.
In case anyone is wondering, when I do use JSON - it is almost always with nlohmann.
Happy Friday!