mkaz.blog

Goodbye XML, Hello JSON

About four years ago I wrote an article on the limitations of XML, I'm here to back that up with some numbers. All of the same issues apply to XML today as they did then. It is still a bloated format, still requires external libraries and still takes plenty of code to parse. Our search at Maya's Mom runs on Lucene/Tomcat and was returning XML to PHP over HTTP. I'm not quite sure my initial decision on this, probably so used to hearing Java with XML over HTTP it sounded fine at the time. Since we are working on improving our search and always one for embracing change, we switched Lucene to return JSON.

Now instead of using an external library and several routines to parse the data, it is done with the PHP JSON module and one command: json_decode(). The JSON module is a C module built in to PHP, which is one reason it is much quicker. The decode converts the data straight to PHP objects and arrays. One stroke done.

deleting code is even better than writing code

The JSON format is far more terse than XML, reducing network load. Plus JSON gives us a bit more flexibility for search; we do some type-ahead stuff which requires Ajax to call a PHP page which performs the search, parses the results and encodes to JSON to hand back to Javascript. Now since search can return JSON, it can all be done straight from Javascript reducing the amount of code to write and maintain.

Performance Numbers

Converting to JSON removed over 200 lines of code, which deleting code is even better than writing code. Your mileage will obviously vary, during the conversion there was some code clean up and refactoringSpeed Improvements: a round trip query and parsing in XML was 400+ milliseconds; now reduced to less than 20 milliseconds, a 20x improvement!More info: there is a minor debate about the two interchange formats. Plus an article comparing the two.

To me, there is no comparison.