Phil Dreizen

November 26, 2013, 4:49 pm

I repeat: XML and JSON are for transporting data.

Just because you received data in XML or JSON form does not mean you need to continue examining, retrieving, and accessing that data in the format your program received it. You do not need to continuously parse your JSON and XML, you do not need to keep traversing your DOM object.

What you should do is work with XML and JSON (and whatever other format for transporting data) in your endpoints only. Take the XML/JSON you receive and turn it into data structures appropriate for you language. Turn it into primitives, into objects, into arrays and maps of primitives and objects. But DON'T KEEP IT AS XML OR JSON!!!

Code that isolates the transport format will be better for several reasons. The code that actually processes your data will be agnostic to whatever transportation formats that are available. If you later need your system to receive or transmit a new data format the code that does the actual processing will not need to be modified. Instead, the code at your endpoints will just need to be able to turn NEW_TRANSPORT_FORMAT into native data structures, and native data structures back into NEW_TRANSPORT_FORMAT. The code will also be easier to read. You won't find yourself in weird XML processing land, or constantly dealing with JSON parse exceptions land. You'll be in completely normal for your language land. This is especially true for XML, which can be especially painful to work with. But it's true for JSON too.

When it comes to the serialization and deserialization of data in binary formats, programmers don't make this mistake. No one would think to deserialize binary data multiple times for the sake of accessing their data. It's almost absurd. You deserialize your binary data once after receiving it, and serialize it once before transmitting it. But when it comes to marshalling and unmarshalling data in XML,JSON this mistake seems to happen often enough. (Never seen it happen with CSV data. Maybe it's TOO painful to work with?)