The last few weeks I've been playing with Basic4Android, a neat little IDE that presents a VB-like language and uses the actual Android emulator as the designer for visual elements. This week I used the HttpClient library along with the JSON library to create a program that consumes JSON-formatted web feeds. Since JSON is more streamlined than XML, at some point I decided to compare some JSON-encoded data against equivalent XML-encoded data to see the space-savings. To save some time I borrowed from a Google JSON example that presented the same data as both XML and JSON. I minimized both sets of data (trimmed whitespace, removed newline characters, etc) and then took a character-count of each data set. And then I did a double-take: the XML was smaller than the equivalent JSON. Huh?
JSON vs. XML
Conventional thinking says that JSON should be smaller than equivalent XML, not the other way around. It's easy to see how XML will typically exhibit more bloat than JSON:
I do believe this basic premise to hold true more often than not: XML-encoded data requires more space than JSON-encoded data. In case you find the topic (and the exception I ran across) to be as interesting as I do, here are links to the code and tools I used for the comparisons:
Conventional thinking says that JSON should be smaller than equivalent XML, not the other way around. It's easy to see how XML will typically exhibit more bloat than JSON:
XML (73 characters): <MyItemList> <MyItem> <MyItemName> Harold </MyItemName> </MyItem> </MyItemList> | JSON (51 characters): {"MyItemList": [ {"MyItem": {"MyItemName":"Harold"} } ] } |
I do believe this basic premise to hold true more often than not: XML-encoded data requires more space than JSON-encoded data. In case you find the topic (and the exception I ran across) to be as interesting as I do, here are links to the code and tools I used for the comparisons:
Google example page (with source XML and JSON data sets):
Note: Interestingly, the Google-provided JSON example code is invalid due to a couple of small-but-critical errors. A copy of corrected JSON code appears down below if you want to copy-and-paste your way through your own tests.
JSON code minimizer (must select the "compact" option in the dropdown box):
XML code minimizer:
Character counter:
RESULTS:
XML: 2096 characters
JSON: 2203 characters
Corrected JSON code (already minimized):
{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$gCal":"http://schemas.google.com/gCal/2005","id":{"$t":"..."},"updated":{"$t":"2006-11-12T21:25:30.000Z"},"title":{"type":"text","$t":"Google Developer Events"},"subtitle":{"type":"text","$t":"The calendar contains information about upcoming developer conferences at which Google will be speaking, along with other developer-related events."},"link":[{"rel":"...","type":"application/atom+xml","href":"..."},{"rel":"self","type":"application/atom+xml","href":"..."}],"author":[{"name":{"$t":"Google Developer Calendar"},"email":{"$t":"developer-calendar@google.com"}}],"generator":{"version":"1.0","uri":"http://www.google.com/calendar","$t":"Google Calendar"},"openSearch$startIndex":{"$t":"1"},"openSearch$itemsPerPage":{"$t":"25"},"gCal$timezone":{"value":"America/Los_Angeles"},"entry":[{"id":{"$t":"..."},"published":{"$t":"2006-11-12T21:25:30.000Z"},"updated":{"$t":"2006-11-12T21:25:30.000Z"},"category":[{"scheme":"...","term":"..."}],"title":{"type":"text","$t":"WebmasterWorld PubCon 2006: Google Developer Tools in General"},"content":{"type":"text","$t":"Google is sponsoring at <a href='http://www.pubcon.com/'>WebmasterWorld PubCon 2006</a>.\nCome and visit us at the booth or join us for an evening demo reception where we will be talking '5 ways to enhance your website with Google Code'.\nAfter all,\nit is Vegas, baby! See you soon."},"link":[{"rel":"alternate","type":"text/html","href":"...","title":"alternate"},{"rel":"self","type":"application/atom+xml","href":"..."}],"author":[{"name":{"$t":"Google Developer Calendar"},"email":{"$t":"developer-calendar@google.com"}}],"gd$transparency":{"value":"http://schemas.google.com/g/2005#event.opaque"},"gd$eventStatus":{"value":"http://schemas.google.com/g/2005#event.confirmed"},"gd$comments":{"gd$feedLink":{"href":"..."}},"gCal$sendEventNotifications":{"value":"true"},"gd$when":[{"startTime":"2006-11-15","endTime":"2006-11-17","gd$reminder":[{"minutes":"10"}]}],"gd$where":[{"valueString":"3150 Paradise Road,Las Vegas,NV 89109"}]}]}}
No comments:
Post a Comment