News for hackers. Stuff that matters.
Re:When XML Beats JSON: UI Layouts by sacado2 on Wednesday October 30, 2019 @08:51AM (#21397111)
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.
Re:When XML Beats JSON: UI Layouts by arethuza on Wednesday October 30, 2019 @09:13AM (#21397301)
{ "type": "div", "children": [ "The ", { "type": "a", "attributes": { "href": "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 ;-)

Re:When XML Beats JSON: UI Layouts by m1el on Wednesday October 30, 2019 @09:32AM (#21397473)
["div", "The ", ["a", {"href": "https://www.json.org/"}, "JSON format"], " was invented by ", ["em", "Douglas Crockford"], "."]
Re:When XML Beats JSON: UI Layouts by sacado2 on Wednesday October 30, 2019 @09:51AM (#21397696)
Quite 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?
Re:When XML Beats JSON: UI Layouts by 52-6F-62 on Wednesday October 30, 2019 @10:04AM (#21397834)
Maybe 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.

Re:When XML Beats JSON: UI Layouts by fnordsensei on Wednesday October 30, 2019 @04:57PM (#21402625)
That'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/

2: https://github.com/Day8/re-frame

3: https://github.com/edn-format/edn

4: https://github.com/weavejester/hiccup

Re:When XML Beats JSON: UI Layouts by nkrisc on Wednesday October 30, 2019 @12:18PM (#21399353)
How would you ever differentiate "div" from an element or text? Is "The " text or the <The > tag?
Re:When XML Beats JSON: UI Layouts by arethuza on Wednesday October 30, 2019 @12:58PM (#21399862)
Why not just:

[ "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?

Re:When XML Beats JSON: UI Layouts by 52-6F-62 on Wednesday October 30, 2019 @02:56PM (#21401206)
Yeah 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.
Re:When XML Beats JSON: UI Layouts by m1el on Wednesday October 30, 2019 @10:31AM (#21398146)
> The first argument is the name of the element, but what is the second one?

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.

Re:When XML Beats JSON: UI Layouts by kgwxd on Wednesday October 30, 2019 @09:58AM (#21397774)
What if text were quoted?
Re:When XML Beats JSON: UI Layouts by kickopotomus on Wednesday October 30, 2019 @11:41AM (#21398936)
Re:When XML Beats JSON: UI Layouts by postalrat on Wednesday October 30, 2019 @11:08AM (#21398550)
["div", "The", ["a[href=https://www.json.org/]", "JSON format"], "was invented by", ["em", "Douglas Crockford"], "."]
Re:When XML Beats JSON: UI Layouts by ilogik on Wednesday October 30, 2019 @09:46AM (#21397646)
that looks like the JSON documents in AWS's CloudFormation
Re:When XML Beats JSON: UI Layouts by iooi on Wednesday October 30, 2019 @12:27PM (#21399470)
Ok, now try <div>div</div>
Re:When XML Beats JSON: UI Layouts by arethuza on Wednesday October 30, 2019 @12:36PM (#21399590)
["div", "div"]
Re:When XML Beats JSON: UI Layouts by ses1984 on Wednesday October 30, 2019 @09:41AM (#21397576)
Interesting.
Re:When XML Beats JSON: UI Layouts by keymone on Wednesday October 30, 2019 @10:26AM (#21398092)
Check out hiccup
Re:When XML Beats JSON: UI Layouts by cerved on Wednesday October 30, 2019 @09:21AM (#21397361)
Amazing. Who would've thought a markup language would be appropriate for the task
Re:When XML Beats JSON: UI Layouts by jpochtar on Wednesday October 30, 2019 @02:51PM (#21401142)
Are you sure you want hierarchy though?

  [
    ["the ", {}],
    ["JSON format", {"link": "https://www.json.org/"}],
    ["was invented by ", {}],
    ["Douglas Crockford", {"bold": true}],
    [".", {}]
  ]
Re:When XML Beats JSON: UI Layouts by contravariant on Wednesday October 30, 2019 @07:13PM (#21404020)
In 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.

Re:When XML Beats JSON: UI Layouts by Mikhail_Edoshin on Thursday October 31, 2019 @02:31AM (#21406207)
The 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.

Re:When XML Beats JSON: UI Layouts by jancsika on Wednesday October 30, 2019 @11:22AM (#21398703)
It'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!!!"

Re:When XML Beats JSON: UI Layouts by jlokier on Wednesday October 30, 2019 @02:05PM (#21400632)
You jest, but this is not unheard of in hand-maintained JSON:

  "comment": "The foo widget needs blah blah ...",
Re:When XML Beats JSON: UI Layouts by Mikhail_Edoshin on Thursday October 31, 2019 @02:38AM (#21406227)
The 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.
Re:When XML Beats JSON: UI Layouts by Kuinox on Wednesday October 30, 2019 @09:19AM (#21397344)
So Jupyter Notebooks should be XML instead of JSON. Right ?
Re:When XML Beats JSON: UI Layouts by sacado2 on Wednesday October 30, 2019 @09:55AM (#21397736)
AFAIK 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.
Re:When XML Beats JSON: UI Layouts by dnautics on Wednesday October 30, 2019 @10:25AM (#21398079)
Honestly, they should be markdown. This would also solve the versioning problem.
Re:When XML Beats JSON: UI Layouts by wodenokoto on Wednesday October 30, 2019 @12:14PM (#21399302)
And 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.

Re:When XML Beats JSON: UI Layouts by akdor1154 on Wednesday October 30, 2019 @10:53AM (#21398387)
Well yep, but that ship has long sailed.
Re:When XML Beats JSON: UI Layouts by gunn on Wednesday October 30, 2019 @09:22AM (#21397368)
It is ugly:

["div", 0, "The ", ["a", {"href": "https://www.json.org/"}, "JSON format"], " was invented by ", ["em", 0, "Douglas Crockford"], "."]