8/25/2012

Jerkson in Play2 development mode

Play2 includes Jerkson which is a Scala wrapper for the Java based JSON library Jackson.

It's very useful and I'm loving it. However when we use it in the Play2 application, it fails to deserialize case classes after reloading in development mode. See the following code:

import com.codahale.jerkson.Json

val json = Json.generate(UserInfo("Naoki Takezoe"))
val info = Json.parse[UserInfo](json)

After reloading in the development mode, this code throws an exception such as ParsingException: Unable to find a case accessor for models.UserInfo.

The reason of this problem is Jerkson hold a class loader in the singleton object.

So this problem could be solved to create new instance which is mixed-in com.codahale.jerkson.Json trait for each Json serialization / deserialization as following:

// This code works in the development mode!
import com.codahale.jerkson.Json

val json = new Json{}.generate(UserInfo("Naoki Takezoe"))
val info = new Json{}.parse[UserInfo](json)

Some of hotswap solutions (reloading recompiled classes) on JVM have similar problem because they are based on classloader swapping. They can make rapid development on JVM and very helpful for our work. However sometimes they trouble us like this.

0 件のコメント: