Friday, December 13, 2013

Speeding up JSON export in Grails

In one of my grails apps, I export a large quantity of data in JSON format. This process was incredibly slow (~30s) and somehow I was convinced that this was a database issue since numerous joins were involved as well. As it turns out, however, the database query took ~1s to complete so that I got curious what the other 29s were spent on. Very unexpectedly, I had to blame grails' native JSON converter. A google search revealed that this was already known:

http://sysgears.com/articles/speedup-json-parsing-grails/

I decided to follow the suggested solution and started playing around with Jackson. Pitfalls are that some Java objects seem to cause Jackson some trouble, e.g. if objects can't be serialized. I therefore decided to use projections to efficiently recover all the database fields I was interested in (probably speeding up the database access as well). Since all the fields were then strings, ints or doubles, conversion worked like a charm. The exact same conversion now takes only ~1s, adding up to ~2s in total.

This helped me to understand how to use Jackson:
http://wiki.fasterxml.com/JacksonInFiveMinutes

There even seems to be a grails plug-in available to replace the default JSON converter with Jackson:
https://github.com/sjhorn/grails-jackson

However, I didn't try it out since development does not appear to be active.

One last important thing to note is that if you want to use jackson to render JSON content you might get into conflict with the regular JSON converter if you try to do something along the lines of render AS JSON. What you need to do instead is:

No comments:

Post a Comment