7 ms·
What XML is also very good at is: representing tagged text, what is called "mixed content" in XML terminology. Just try to write the JSON equivalent of: <
by sacado2 7y ago
What XML is also very good at is: representing tagged text, what is called "mixed content" in XML terminology.
Just try to write the JSON equivalent of:
<div>The <a href="https://www.json.org/">JSON format</a> was invented by <em>Douglas Crockford</em>.</div>
If your document consists mainly in losely structured text with some annotation, you better use XML.
- arethuza 7y ago{ "type": "div", "children": [ "The ", { "type": "a", "attributes": { "href": "https://www.json.org/" https://www.json.org/" }, "children": [ "JSON format" ] }, " was invented by ", { "type": "a", "children": [ "Douglas Crockford" ] } ] } 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 ;-)
- m1el 7y ago["div", "The ", ["a", {"href": "https://www.json.org/" https://www.json.org/"}, "JSON format"], " was invented by ", ["em", "Douglas Crockford"], "."]
- ses1984 7y agoInteresting.
- ilogik 7y agothat looks like the JSON documents in AWS's CloudFormation
- sacado2 7y agoQuite readable but it's a hell to parse. The first argument is the name of the element, but what is the second one? If it is a string or an array, it's the first child, but if it's an object, it's the attribute set?
- kgwxd 7y agoWhat if text were quoted?
- 52-6F-62 7y agoMaybe something more like: [ "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.
- nkrisc 7y agoHow would you ever differentiate "div" from an element or text? Is "The " text or the <The > tag?
- arethuza 7y agoWhy not just: [ "div", "The ", [ "a", { "href": "https://www.json.org/" 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?
- 52-6F-62 7y agoYeah that seems to make the most sense. I had to actually work it out before I saw it. I was looking for some further separation beyond indices but I just ended up over-thinking it.
- fnordsensei 7y agoThat's is roughly how it's done in Reagent[1]/re-frame[2]: [: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/ https://holmsand.github.io/reagent/ 2: https://github.com/Day8/re-frame https://github.com/Day8/re-frame 3: https://github.com/edn-format/edn https://github.com/edn-format/edn 4: https://github.com/weavejester/hiccup https://github.com/weavejester/hiccup
- postalrat 7y ago["div", "The", ["a[href=https://www.json.org/]" https://www.json.org/]", "JSON format"], "was invented by", ["em", "Douglas Crockford"], "."]
- kickopotomus 7y agoBasically hiccup: https://github.com/weavejester/hiccup https://github.com/weavejester/hiccup
- iooi 7y agoOk, now try <div>div</div>
- arethuza 7y ago["div", "div"]
- keymone 7y agoCheck out hiccup
- Kuinox 7y agoSo Jupyter Notebooks should be XML instead of JSON. Right ?
- sacado2 7y agoAFAIK notebooks are a list of cells, each cell consisting in text, but there is no meta information within the cells, so JSON looks OK there.
- dnautics 7y agoHonestly, they should be markdown. This would also solve the versioning problem.
- wodenokoto 7y agoAnd where would you store the output? 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.
- akdor1154 7y agoWell yep, but that ship has long sailed.
- cerved 7y agoAmazing. Who would've thought a markup language would be appropriate for the task
- gunn 7y agoIt is ugly: ["div", 0, "The ", ["a", {"href": "https://www.json.org/" https://www.json.org/"}, "JSON format"], " was invented by ", ["em", 0, "Douglas Crockford"], "."]
- deleted 7y ago[deleted]
- jancsika 7y agoIt's funny to me that even in an XML shining example of purpose you've got non-semantic "<div>" there likely because, "just float where I want, dammit!" Just imagining a JSON that starts with "dummy": "don't remove this or the API will mis-align this across two structs!!!"
- jlokier 7y agoYou jest, but this is not unheard of in hand-maintained JSON: "comment": "The foo widget needs blah blah ...",
- Mikhail_Edoshin 7y agoThe semantic of these things is what the code does with them. It's the only semantic there is. E.g. the <i> tag has perfect semantic, while <article> not that much.
- jpochtar 7y agoAre you sure you want hierarchy though? [ ["the ", {}], ["JSON format", {"link": "https://www.json.org/"}], ["was invented by ", {}], ["Douglas Crockford", {"bold": true}], [".", {}] ]
- pig-disgusting 7y agoA. You forgot to capitalize the first word in the sentence. B. Your dangling whitespace characters are all wrong. This tells me that the steps you took to craft the serialized version of the data were totally disconnected from the intent of the transmission. You were more focused on syntax (decorative array brackets, and curly brace closures), than the human-readable aspects of the text. More often than not, this is expressed as unclosed tags in XML mark up, which would express as run-on underlined link activation and bold face. Yet this is often cleaner, when formatting gets stripped out, preserving whitespace delimiters such that text us ungarbled. In the source, this sometimes leads to excessive whitespace bloat, but luxuriously so.
- contravariant 7y agoIn this particular example you need hierarchy for on at least two occasions. 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.
- Mikhail_Edoshin 7y agoThe information stored in this XML is essentially that: { "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.