Horrors From the Legacy Code

So, the background: There’s a whole table in a certain database to store configuration data for services. At startup the service loads the latest records of all its corresponding config data and caches them. If you want to upload a newer version of the file you insert a new record and bounce the respective service. There’s better ways to do this, but this is what we have.

My current task was to remove some internal config from one service and create a new entry in the previously described config data table to contain it. I thought, hey I’ll use JSON for the config data, because it’s more lightweight and compact as compared to XML. So I went about creating a bean to load this data and parse it into a POJO, wrote some unit tests, integrated it into the existing flow, and bam! everything’s looking good. All tests are green! Then I go to upload the JSON config file to the database…

The underlying configuration data table has a data type of CLOB for the file content. However, I was getting a weird error when trying to use the config upload tool to insert to the database: “Data at the root level is invalid” or some crap like that. I’m like “that appears to be an XML error, but I’m using JSON, what gives?” Turns out the upload tool inserts into a view of the table, not the table itself. The view actually converts the data in the the CLOB column into an, you guessed it, XMLTYPE?!?!?

Luckily, all was not lost! What’s this I see? Another column of type VARCHAR2(4000) in the config data table! “This must be designed for non-XML data” I think. Alas, after investigating, I discover that there is no api to populate this column. It’s just some more vestigial legacy code :( So, all actually was lost.

What did I do, you may ask? Well, I just used XML instead of JSON. Really not a big deal. Just much uglier, and more than double the number of characters, and slower parsing. Oh and I had to rewrite my bean to use an XML library instead of JSON. And the first one I tried wouldn’t work because of dependency mismatches with another library being used. Man I hate XML…

Most new APIs are being written in JSON these days. Below is an image from an article (linked in the caption) that while a little dated, helps to show this trend. If you’re one of the holdouts who still loves to use XML, I honestly would like to know why. Please let me know. I have not personally come across a case where I would prefer XML to JSON.