Fork me on GitHub

12 Aug 2009

JSON Rendering Your Classes

It turns out adding custom JSON renderers for your own types to Grails is really easy. Since I habitually use Joda Time instead of the horrible java.util.Date and even more horrible java.util.Calendar I need to be able to render classes such as Joda's DateTime as JSON so that domain objects with fields of those types will convert properly.

Implementing a renderer for a type is as easy as this: DateTimeMarshaller.java. After that all that's required is to register the new renderer in BootStrap.groovy or some other appropriate spot with:

grails.converters.JSON.registerObjectMarshaller new DateTimeMarshaller()

I'll be adding this to the next release of the Joda Time Plugin so that it's completely transparent.

Update: Actually it's even easier than I thought. This being Groovy you can just use closures like so:

JSON.registerObjectMarshaller(DateTime) {
return it?.toString("yyyy-MM-dd'T'HH:mm:ss'Z'")
}

Update: This is now built in to version 0.5 of the Joda Time Plugin.

7 comments:

Siddhartha said...

Hi,
I am using the 0.5 joda plugin as well as i tried the Bootstrap solution given in your post I am still getting an exception when I am using DateTime

va:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.
ava:522)
Caused by: java.lang.NullPointerException
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:156)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.convertAnother(JSON.java:156)
at grails.converters.JSON.value(JSON.java:199)
at grails.converters.JSON.render(JSON.java:128)
at grails.converters.JSON.render(JSON.java:144)
at com.test.PersonController$_closure3.doCall(PersonController.groovy:2
)
at com.test.PersonController$_closure3.doCall(PersonController.groovy)



Could you please help

J-Wicz said...
This comment has been removed by the author.
J-Wicz said...

Dear Rob this blog post of yours, was one of my source for this longer post about XML marshalling. Thanks for the hints

http://jwicz.wordpress.com/2011/07/11/grails-custom-xml-marshaller/

cheers
J-Wicz

Erik Pragt said...

Interesting reads! Today, my colleague Marcin also posted an interesting article about marshalling using annotations in JSON. You can find it here:

http://www.jworks.nl/2011/08/15/excluding-fields-and-properties-from-marshalling-using-annotations-in-grails

Anonymous said...

From: Apkzm.com
I have read your blog it's very attractive and impressive. I like your blog.
Vidmate Apk Download

Unknown said...

l6c88l9g32 u9c12l1v03 a3m05o1j92 t7h17l7q25 b3n78s9s88 y1s93i0z38

Jeramy S Hick said...

Nice post which you shared with us. thanks for your genuine post. As we know system work more required in all fields. so typing skill is more important for kids, students and Adult. It is used in various things. You should know 10-Minute Test For Typing Jobs. you will get more knowledege to improve your typing skill.