2 ms·
"If it looks like a document, use XML. If it looks like an object, use JSON. It’s that simple. The essential difference isn’t simplicity/complex
by andyjohnson0 10y ago
"If it looks like a document, use XML. If it looks like an object, use JSON. It’s that simple. The essential difference isn’t simplicity/complexity or compact/verbose or typed/text, it’s ordered-by-default or not."
I can appreciate that an order-independent representation can make schema versioning easier, but what other advantages does this provide?
- teach 10y agoMostly for programming languages with hash tables / dictionaries. Python dictionaries hold keys but the keys aren't in any particular order, so allowing them to come out in whatever order maps very naturally to the language.
- Pxtl 10y agoYes, the order-obessession of XML is a constant source of surprise in XML. The idea that <myObj> <Foo>myFoo</Foo> <Bar>myBar</Bar> </myObj> is somehow different from <myObj> <Bar>myBar</Bar> <Foo>myFoo</Foo> </myObj> is incredibly surprising and frustrating, and XML schema actually supports ignoring the difference but xml schema tools seem to hate using the "all" instead of the "sequence" schema definitions. The worse offender, imho, is Microsoft's DataContractSerializer, whose default behavior is to silently ignore this ordering failure and also silently fail to deserialize any out-of-order elements. Either fail loudly or be flexible about ordering.
- jdnier 10y ago"If it looks like a document, use XML." And the reason for that is XML originated from SGML and both were initially focused on marking up documents (where everything is ordered, just like the words in this sentence), not objects or bits of data. And that's why JSON is usually simpler and more useful for your example and similar. Whether or not the order is significant in your XML example becomes a schema-level issue (DTD, Relax, XMLSchema). Maybe you are being forced to use XML, but if not, you're describing an object, so use JSON.
- Pxtl 10y agoYes, but xmlschema allows for a decent platform-agnostic system for defining objects. The problem is that most tools default (or even force) to "sequence" instead of "all". With "all" you get object-ish XML behavior. There is no well-adopted analog to xmlschema for json.
- Mikhail_Edoshin 10y agoIf it was a log of transactions, the order would matter. E.g. it could be a list of changes: <create>...</create> <delete>...</delete> Relax NG handles unordered collections well.
- Mikhail_Edoshin 10y agoOrder-independent actually makes schema testing more difficult.