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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def criteria = SomeDomainClass.createCriteria() | |
def result = criteria.list { | |
... | |
} | |
ObjectMapper mapper = new ObjectMapper() | |
def jsonResult = mapper.writeValueAsString(result) | |
response.contentType = "text/json" | |
render jsonResult |
No comments:
Post a Comment