I think I prefer the XML :-)
Edit: I agree that tagged text is a clear win for XML - for pretty much everything else I'd go for JSON.
Edit2: HN ate my formatting, but probably not a bad thing ;-)
[
"div",
[
"The ",
[
"a",
{ "href": "https://www.json.org/" },
[ "JSON format" ]
],
" was invented by ",
[
"em",
"Douglas Crockford"
],
"."
]
]
With all children being encapsulated in an array...edit: though it again gets confusing when the 0 index is sometimes the element type and sometimes the straight text... so never mind I guess the problem persists.
[:div "The" [:a {:href "https://www.json.org/"} "JSON format"]
" was invented by " [:em "Douglas Crockford"] "."]
The fact that EDN[3] supports keywords makes it a bit easier to parse. Representing HTML in EDN this way was first done in a library called Hiccup[4], so it’s usually called “Hiccup” even when encountered outside of the original library.1: https://holmsand.github.io/reagent/
2: https://github.com/Day8/re-frame
[ "div", "The ", [ "a", { "href": "https://www.json.org/" }, "JSON format" ], " was invented by ", [ "em", "Douglas Crockford" ], "." ]
First thing in array is element, if there is an object at the second location then that's the attributes?
If the second argument is an object/dictionary, it's attributes. Otherwise, it's the first child. Alternatively, the ambiguity can be resolved by using null/0 instead of empty attributes.
[
["the ", {}],
["JSON format", {"link": "https://www.json.org/"}],
["was invented by ", {}],
["Douglas Crockford", {"bold": true}],
[".", {}]
]1. The xml denotes that the whole thing should be wrapped in a div, and shouldn't just be considered a paragraph. With only lists you can't denote what kind of level you're at, unless you force each level to always carry the same meaning (which makes the format weaker than XML).
2. Setting "bold" to true isn't the same thing as emphasizing the text. Typically 'em' is italic, but more importantly a nested 'em' should no longer be italic. To achieve this with JSON you're forced to combine formatting and structure in a way that CSS was designed to prevent.
{ "string": "The JSON format was invented by Douglas Crockford",
"structure" [
[ 0, 48, "div", {} ],
[ 4, 13, "a", { "href": "https://www.json.org" } ],
[ 32, 48, "em", {} ]
]
}
(If we allow empty elements, we'd need another field in the structural entries to be able to reconstruct the hierarchy.) Now I'd say that XML way to combine these three kinds of data is pretty elegant and natural. The JSON would be impossible to write by hand, although it's good for machine processing.Upd: The three kinds of data: the string, the structures (div, a, em), and their position in the string.
Just imagining a JSON that starts with
"dummy": "don't remove this or the API will mis-align this across two structs!!!"
"comment": "The foo widget needs blah blah ...",R uses markdown + a separate file with the output cached and while the non-rendered file is easier to version the whole thing is a lot more difficult to handle.
["div", 0, "The ", ["a", {"href": "https://www.json.org/"}, "JSON format"], " was invented by ", ["em", 0, "Douglas Crockford"], "."]
Just try to write the JSON equivalent of:
If your document consists mainly in losely structured text with some annotation, you better use XML.