4 ms·
I'd argue the opposite, simply because XML has many well defined schema formats and XPath. If you end up having versions of data formats, or are dealing with d
by zdw 13y ago
I'd argue the opposite, simply because XML has many well defined schema formats and XPath. If you end up having versions of data formats, or are dealing with data from an unknown source, XML is far superior, and allows for easier interchange of data.
The correct way to load an XML document is as follows, IMO:
* Open it
* Run it through the schema to validate it's
structure
* Manipulate/access it via XPath, with the ability to
assume correct structure as defined by the schema.
Compare to JSON, where the process is
* Open it
* Attempt to access it, even though it's structure
may deviate from the expected
* Die or try to rollback work when an assumption about
the data structure isn't met.
Thus, most JSON code either trusts the input data implicitly, or is a mess of "is this where it should be?"/independently re-developed schema equivalent every time. I am aware of attempts to create JSON schema languages - unfortunately these are neither in common use, nor well defined enough to be standards, and they're more verbose and less expressive than XML's RelaxNG compact syntax.
JSON is fundamentally a dump of a data structure, not a document format. It isn't designed for long term data storage, or for inter-compatibility, despite being used those ways. Thus, it's great if you control both ends and your data structures don't change, as is the case with most web programming. It frequently falls down outside that realm.
Now, the above ignores runtime performance concerns. I'd argue (again) that outside of web apps, the load/store into a data structure is done rarely, or can be done in the background or in parallel if there's a lot of data concerned. Also, we're probably talking about less than an order of magnitude in the vast majority of cases.
- TazeTSchnitzel 13y agoI don't know about you, but I don't find it difficult to just put sanity checks when reading data in case it isn't as expected. And hey, you need to validate user data anyway, schema or no.
- lmm 13y agoThe thing is it's almost impossible to express all the constraints on your input in schema. You certainly can't encode "userId must match a user that exists in the database" as part of a schema. So with XML you end up having two validation processes - the schema and then the part of your code that checks that the input datum really is valid. And then when the time comes to add another check you realize that code is a much more expressive way to write your constraints, and it's always possible for input to be rejected at the code stage, so you might as well just add your check there. So the schema level ends up being a useless extra complication.
- zdw 13y agoI don't think you get what schemas are for. Schemas don't validate the content - they validate the structure and variable types. It doesn't magically make the data valid. It lets you make assumptions about how that data is structured. The advantage of a schema is that one schema can be used in multiple languages. Someone's Lua based embedded system can run the same schema as a Go program on a 32 CPU box. You can give someone a well written schema, tell them "make your data like this, and run the validator when you open and save your XML". In this way, it's a free, one line (in most languages) structure sanity check.
- lmm 13y ago>I don't think you get what schemas are for. Schemas don't validate the content - they validate the structure and variable types. It doesn't magically make the data valid. It lets you make assumptions about how that data is structured. In a typed language the very first stage of deserialization, the "turn this from a string into objects in my language" step, will do that for you "for free". In an untyped language I guess it could be helpful, but again: it's almost always easier to write these sort of checks in a general purpose programming language than in XML schema. >The advantage of a schema is that one schema can be used in multiple languages. Someone's Lua based embedded system can run the same schema as a Go program on a 32 CPU box. If that's what you want there are more robust and performant approaches, e.g. thrift. >You can give someone a well written schema, tell them "make your data like this, and run the validator when you open and save your XML". In this way, it's a free, one line (in most languages) structure sanity check. Right, but that doesn't tell them what they think it means. It doesn't make the request valid.
- Mindless2112 13y agoIf you have untrusted input, XML has it's own issues [1]. > JSON is fundamentally a dump of a data structure, not a document format. Exactly! IMHO, it's unfortunate that mark-up documents can be used to represent other data structures. It isn't what they're meant for, and there are (or "should be" if you're not fond of JSON) better tools for human-readable serialization. [1] http://en.wikipedia.org/wiki/Billion_laughs http://en.wikipedia.org/wiki/Billion_laughs
- zanny 13y agoYou mean yaml?
- Mindless2112 13y agoIndeed, YAML is a more powerful serialization format than JSON. The original name (Yet Another Markup Language) threw me off, so I never seriously considered using it. Even its Wikipedia article is in the "Markup languages" category.
- deleted 13y ago[deleted]
- zdw 13y agoMy general guidance is: Yaml for configuration files/things people touch XML for documents that are complex/nested, that need extra validation, or need to be stored for longer than a few days JSON for ephemeral data structures passed between two controlled endpoints
- zdw 13y agoIMO, if you're schema design would pass a Billion laughs style attack as being a valid file, you're probably doing something wrong. SAX style streaming/pull parsers tend to deal better with this kind of thing as well.
- Tloewald 13y agoThe lack of a schema system (that is alive and well) for JSON is certainly a strike against it. (The way XML namespaces work, on the other hand,...)
- deleted 13y ago[deleted]
- clarkevans 13y agoJSON Schema (http://json-schema.org/ http://json-schema.org/) seems quite competent. Perhaps the "problem" with JSON Schema/Path is that JSON is simple enough that you could load into native structures and then do validation in the programming language your most comfortable with. It took years and lots of committee meetings for a W3 blessed XML schema language; and then, key people didn't agree with how it should be done: James Clark's alternative was TREX and MURATA Makoto's was RELAX.
- acdha 13y agoXML schemas only solve the most obvious validation failures – even if the elements are present as expected, you still need the exact same process to handle invalid data. While XML has XPath, JSONPath exists and the downside to XPath is that many environments are stuck with 1.0 and XML libraries tend to be poorly designed (e.g. namespaces are simply unnecessarily painful in most XPath implementations). Every significant project I've worked on has the same cautious loading process for XML as JSON, with some extra checking at the early XML load stage because XML is harder to work with and thus fewer people produce valid documents (forget schema validations, errors with simple character encoding, well-formedness or namespace declarations are surprisingly common). In practice, I tend to end up with a forgiving parser, a collection of selectors and full validation on the results, which works equally well with either format.
- zdw 13y agoThe "producing valid documents" issue is solved by running the output through the schema before, or immediately after it's saved, and throwing an error if the code has generated an invalid document.
- acdha 13y agoAgain, this approach does not work on projects where you can't immediately reject invalid documents. In many cases, this is unacceptable from a business standpoint and so you're forced to attempt to salvage minor conformance problems.