I thought XML is highly portable and can be used as a mini database. I have seen XML used everywhere. I even see large companies switching over to JSON. Even Microsoft has integrated support for JSON. What is all the hype over JSON?
相关问题
- Jackson Deserialization not calling deserialize on
- Illegal to have multiple roots (start tag in epilo
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- How to fix IE ClearType + jQuery opacity problem i
JSON is best for consumption of data in web applications from webservices for its size and ease of use, especially due to the built-in support in JavaScript. Imagine the computation overhead for parsing an xml fragment compared to the instant lookup in JSON.
A very good example is JSON-P. You can get back data from a webservice wrapped in a callback function call, like
my_callback({"color": "blue", "shape":"square"});
inside a dynamically generated<script>
tag so the data can be directly consumed in the functionmy_callback()
. There is no way to get even close to this convenience using XML.XML would be the format of choice for large documents, where you have a framework of rendering pages of data in multiple formats using XSLT. XML can also be used with application configuration files for readability among many other uses.
XML is bloated snake oil in most situations. JSON gives you most of the benefits without the bloat.
Easy consumption by JavaScript can be one of the reasons ..
No one here has mentioned XML-s main advantage: validation rules (DTD, XSD). My conclusions, having used both:
Of course, there's json-schema being developed but you won't find built-in support for it anywhere.
I'm a fanboy of both, they have just different strengths.
JSON is effectively serialized JavaScript in that you can eval(aJsonString) directly into a JavaScript object. Inside of a browser it's a no-brainer JSON is perfectly suited for JavaScript. At the same time JavaScript is a very loosely-typed dynamic language and cannot natively take advantage of all the specific type information available contained within an Xml/Xsd document. This extra metadata (which is great for interoperability) is a hinderance in JavaScript making it more tedious and cubersome to work with.
Size vs Performance
If you're in a browser JSON is faster to serialize/deserialize as it's simpler, more compact and more importantly natively supported. I have some northwind database benchmarks available comparing the size and speed between the different serializers available. In the Base Class Library Microsoft's XML DataContract serializer is over 30% faster than their JSON one. Although this has more to do with the effort Microsoft put into their XML serializer as I was able to develop a JsonSerializer that is more than 2.6x faster than their XML one. As for payloads based on the benchmarks it looks as though XML is roughly more than 2x the size of JSON. However this can quickly blow out if your XML payload uses many different namespaces within the same document.
It's lightweight compared to XML. If you need to scale, reduce your bandwidth requirements!
Compare JSON
to XML: