Converting Groovy Objects to JSON using Grails

Posted on February 14, 2009. Filed under: Grails, Groovy, Uncategorized | Tags: , , , |

Disclaimer: this might not be the best way to do it. I’m open for improvements, but it’s a way I’m using now, and it works pretty nice!

Currenly, I’m creating an application which does some JSON communication. Since Grails already has nice support for JSON, I wanted to reuse their libraries, but, at the moment, I don’t need a complete web Grails application. So, the plan was to reuse the JSON (deep) converters to convert my Groovy classes to JSON, and work with that. I created the following small test to try to accomplish what I wanted:

import grails.converters.JSON

class Person {
String name = "erik"
int age = 10

println new JSON(new Person()).toString()

Well, I don’t think I’d be writing this blog entry if things were really this easy. Executing the above code fails with a nasty NullPointerException:

Caught: org.apache.commons.lang.UnhandledException: java.lang.NullPointerException
	at testjson.main(testjson.groovy)

Why is this, you might ask. Well, it turns out the JSON converter is using a ConverterUtil class, which expects a GrailsApplication. When there’s no GrailsApplication, the code will fail with a NPE as a result. If you’d like to see this fixed: I’ve created a JIRA issue here.

However, I did came up with a workaround. It’s not perfect, but it works. Since the ConvertUtil is a sort of Singleton, you call it yourself, and set a DummyGrailsApplication instead. Well, instead of the dummy version, we can use a DefaultGrailsApplication instead. So, after our fix, the code looks like this:

import grails.converters.JSON
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.web.converters.ConverterUtil

class Person {
String name = "erik"
int age = 10
ConverterUtil.getInstance().grailsApplication = new DefaultGrailsApplication()
println new JSON(new Person()).toString()

When running this, the following output is produced:


Which is exactly what we want!

Read Full Post | Make a Comment ( 4 so far )

IntelliJ JSON Formatter Plugin 0.2

Posted on February 14, 2009. Filed under: IntelliJ JSON Formatter Plugin | Tags: , , , |

I’ve worked with my JSON Formatter plugin for a couple of days now to debug my Grails communication layer, and even though I created it myself, I want to say that I’m quite pleased with it. The only thing which annoyed me a bit is that it’s hard to see  if the JSON code is valid. Well, while it’s not 100% complete (and correct) yet, I’m happy to say that I’ve added simple validation to the Plugin, thanks to the JSON library of Bruno Ranschaert, who was quick to respond to a small bug in the ANTLR part of the JSON library. Since that’s fixed now, you can use the JSON plugin to further enjoy Grails and IntelliJ development! 🙂 (See the pictures for the feedback panel!)


Read Full Post | Make a Comment ( None so far )

IntelliJ JSON Formatter Plugin

Posted on February 5, 2009. Filed under: Grails, IntelliJ JSON Formatter Plugin | Tags: , , , , , |

I’ve just created my first IntelliJ plugin, a JSON formatter. Since I work a lot with Groovy and Grails, and Grails supports JSON so nicely, it’s sometimes necessary to debug the JSON code going back and from the client to the Grails backend. However, since the JSON code is stripped from whitespaces, it’s a bit harder to read than a nested structure. To format the JSON, you can use various websites , but it requires a context switch (from IDE to Webbrowser), plus it requires an Internet connection. To solve this, I created an IntelliJ JSON Formatter plugin, which nicely hooks into IntelliJ. To give you an idea of what the plugin looks like, please take a look at the following image:

IntelliJ JSON Formatter plugin

This images gives an impression of a formatted piece of JSON code, and also includes brace matching and coloring thanks to the RSyntaxTextArea. The JSON code is parsed with the help of the JSON tools from Berlios.

You can install the plugin by using IntelliJ’s integrated plugin manager. If you’ve used it, please give me some feedback! I’d like it!

Read Full Post | Make a Comment ( 11 so far )

Liked it here?
Why not try sites on the blogroll...