News for hackers. Stuff that matters.
Re:XML Can Give the Same Performance as JSON by clarkevans on Thursday August 15, 2013 @09:15AM (#6217544)
It's significantly harder to write a compliant and performant parser for XML, even if one might be able to make it as fast. But, after parsing, the application developer still has to transform XML data into something usable to their program. This is much easier with JSON since it's information model (map/list/scalar) matches the information model of most modern programming languages.

XML is a poor choice for most serialization since it has the wrong information model. It's designed for extensible document markup and most application data packages aren’t documents that require markup.

Re:XML Can Give the Same Performance as JSON by zdw on Thursday August 15, 2013 @09:41AM (#6217671)
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.

Re:XML Can Give the Same Performance as JSON by lmm on Thursday August 15, 2013 @10:08AM (#6217817)
The 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.
Re:XML Can Give the Same Performance as JSON by zdw on Thursday August 15, 2013 @12:15PM (#6218790)
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.

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.

Re:XML Can Give the Same Performance as JSON by lmm on Thursday August 15, 2013 @12:46PM (#6219060)
>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.

Re:XML Can Give the Same Performance as JSON by Mindless2112 on Thursday August 15, 2013 @10:14AM (#6217846)
If 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

Re:XML Can Give the Same Performance as JSON by zdw on Thursday August 15, 2013 @12:22PM (#6218849)
IMO, 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.

Re:XML Can Give the Same Performance as JSON by zanny on Thursday August 15, 2013 @10:51AM (#6218119)
You mean yaml?
Re:XML Can Give the Same Performance as JSON by Mindless2112 on Thursday August 15, 2013 @12:09PM (#6218729)
Indeed, 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.

Re:XML Can Give the Same Performance as JSON by zdw on Thursday August 15, 2013 @12:16PM (#6218811)
My 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

Re:XML Can Give the Same Performance as JSON by clarkevans on Thursday August 15, 2013 @11:07AM (#6218237)
JSON Schema (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.

Re:XML Can Give the Same Performance as JSON by Tloewald on Thursday August 15, 2013 @10:35AM (#6218011)
The 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,...)
Re:XML Can Give the Same Performance as JSON by acdha on Thursday August 15, 2013 @11:12AM (#6218268)
XML 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.

Re:XML Can Give the Same Performance as JSON by zdw on Thursday August 15, 2013 @12:23PM (#6218866)
The "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.
Re:XML Can Give the Same Performance as JSON by acdha on Thursday August 15, 2013 @06:02PM (#6220884)
Again, 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.
Re:XML Can Give the Same Performance as JSON by TazeTSchnitzel on Thursday August 15, 2013 @09:59AM (#6217764)
I 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.
Re:XML Can Give the Same Performance as JSON by TeeWEE on Thursday August 15, 2013 @10:19AM (#6217887)
I like json to, because its lean and mean. But with a schema you can built a connection from system A to system B without having to deal with the underlying XML. However protocolbuffers would be a better choice then. :p
Re:XML Can Give the Same Performance as JSON by corresation on Thursday August 15, 2013 @09:42AM (#6217674)
It's designed for extensible document markup

This is, and always has been, an absolutely terrible way of writing off XML. Do we judge every technology today based upon their purported original purpose? "The computer was designed to solve linear equations, therefore it is ill suited for any other purpose". Noisy nonsense.

JSON only maps to the information model of JavaScript, and has no closer ties to any other language than XML does.

Re:XML Can Give the Same Performance as JSON by TazeTSchnitzel on Thursday August 15, 2013 @09:55AM (#6217749)
JSON maps to common structures in most languages. A JSON Array is comparable to a JS Array, a C/C++ array, a C++ list, a Python list, a PHP array, a C# List, etc. A JSON Object is comparable to a JS Object, a C++ map, a Python dict, a PHP array or object, a C# Dictionary, etc.

By contrast, XML does not directly map to common data structures.

Re:XML Can Give the Same Performance as JSON by clarkevans on Thursday August 15, 2013 @11:01AM (#6218194)
XML has lots and lots of complexity needed so that you could read the "underlying text" (by stripping elements) and for processing notation extensions. For example, you have attributes for out-of-band information, and, this requires additional escaping and content normalization rules. Comparison has multiple definitions because white-space is significant; or not, depending on context. XML specification is ten times longer; it has dozens of sharp edges you have to memorize in order to use effectively.

If you believe that XML's information model maps onto "any other language" just as well as JSON, please show me native mapping/object support in XML and, conversely which programming languages (besides XSLT) have mixed content and attributes.

Re:XML Can Give the Same Performance as JSON by corresation on Friday August 16, 2013 @09:17AM (#6223775)
If you believe that XML's information model maps onto "any other language" just as well as JSON, please show me native mapping/object support in XML and, conversely which programming languages (besides XSLT) have mixed content and attributes.

You can serialize to/from XML in virtually any modern language. Not sure if you're seriously asking this, or what the limiter "native" is supposed to mean (beyond that JSON is JavaScript. But it isn't C#, and it isn't Python, and it isn't...).

How they use attributes or elements is context specific. Every feature and pattern doesn't apply to every use.

XML specification is ten times longer; it has dozens of sharp edges you have to memorize in order to use effectively.

No, you actually don't. You create an XSD of a simple layout (trivial, monkey work). That is what you serialize from/to. The universe of possibilities in XML is, again, utterly and absolutely irrelevant.

Seriously, JSON doesn't even have a DATE type. That is so fundamentally broken I don't even know where to start (Microsoft has their own mystical blend of date when they use JSON, for instance, incompatible with anything else).