Deserializing Json array into Scala object

1,223 views
Skip to first unread message

Drew Hamlett

unread,
Nov 1, 2011, 9:58:40 AM11/1/11
to play-framework
I posted this on stack overflow but I thought I would post it here
also. Thanks.

I have been having major problems trying to deserialize a JSON array
to a Scala object



[{"name":"Cool","city":"College Park","address":"806","id":
1},{"name":"Mars ","city":"Durham","address":"12","id":2},
{"name":"Something","city":"Raleigh
","address":"","id":3},{"name":"test","city":"","address":"","id":
5}]

I have tried gson, jerkson(jackson scala wrapper), sjson, flexjson.
None of them have worked. What I have here is a List of Customers.
List[Customer].

This is the closest I've got:

val array = new JsonParser().parse( customers ).getAsJsonArray()

This gave me an 4 arrays. It obviously didn't give me a customer
object though. I tried Jerkson.

val array = parse[List[Customer]](customers)

But I get this.

GenericSignatureFormatError occured : null

I'm just trying to find a simple way like I would in Java.

Here is my Scala class.

case class Customer(
id : Pk[ Int ],
name : String,
address : Option[ String ],
city : Option[ String ],
state : Option[ String ],
user_id : Int )

object Customer extends Magic[ Customer ]
( Option( "Customer" ) ) {

def apply( name : String, address : String, city : String,
state : String, user_id : Int ) = {
new Customer( NotAssigned, name, Some( address ), Some( city ),
Some( state ), user_id )
}

def delete( id : Int ) = {
SQL( "DELETE from Customer where id =
{id}" ).onParams( id ).executeUpdate()
}

}

Thanks for any help.

Manuel Bernhardt

unread,
Nov 1, 2011, 11:09:18 AM11/1/11
to play-fr...@googlegroups.com
Hi,

which Jerkson version do you use and how have you integrated it? Here
is how I use it:

http://logician.free.fr/index.php/2011/09/16/play-scala-and-json/

Also it may be that you need to create custom de/serializers for Pk for Jerkson.

Manuel

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

Matt Hildebrand

unread,
Nov 1, 2011, 11:20:38 AM11/1/11
to play-fr...@googlegroups.com
Maybe it's because the "state" and "user_id" properties are missing from the JSON?  If their values are null in the JSON, your JSON library will likely deserialize them to None.

-Matt


Drew Hamlett

unread,
Nov 1, 2011, 12:12:41 PM11/1/11
to play-framework
The custom serializers for Pk seem to be the issue. Do you think you
can point me in the right direction for that? I would like to not
even use the Pk object but there seems to be no way around it from
what I've seen. Thanks a lot for the link.

On Nov 1, 11:09 am, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:

Aishwarya Singhal

unread,
Nov 1, 2011, 12:38:02 PM11/1/11
to play-framework
well you can use JPA/ sienna :-)

can you not mark it as transient and ignore?

Drew Hamlett

unread,
Nov 1, 2011, 12:46:51 PM11/1/11
to play-framework
Due to a log running issue that no one can seem to help I can't use
JPA Java models. I can't call the inherited methods like create,
findBy, which I would love to do.

http://stackoverflow.com/questions/7461674/using-java-models-with-scala-value-findbyid-is-not-a-member-of-object

Drew Hamlett

unread,
Nov 1, 2011, 12:53:40 PM11/1/11
to play-framework
I also can't see how this is the future of Play!. If your pushing
Scala and Anorm in Play 2.0 then the stack is incomplete. You have to
have have a JSON library that can deserialize the PK object or move
away from it. I mean this is basic level kind of stuff.

On Nov 1, 12:46 pm, Drew Hamlett <drewhj...@gmail.com> wrote:
> Due to a log running issue that no one can seem to help I can't use
> JPA Java models.  I can't call the inherited methods like create,
> findBy, which I would love to do.
>
> http://stackoverflow.com/questions/7461674/using-java-models-with-sca...

Manuel Bernhardt

unread,
Nov 1, 2011, 1:01:14 PM11/1/11
to play-fr...@googlegroups.com
Hi,

here some code that does de/serialization for a BSON ObjectId:

https://gist.github.com/1249014

the principle should be exactly the same for a PK, you can see how the
deserialization is handled in Play itself here:

https://github.com/playframework/play-scala/blob/master/src/play/scalasupport/Binders.scala

though I am not familiar with PK so I have no idea how that should be
handled. looks like you got to return NotAssigned when it's empty and
otherwise... maybe use the Binder's directBind to fetch a value and
return an Id(). not sure.


Also make sure you use your CustomJson singleton in your code for the
Json handling (and not the jerkson default one).

hope this helps,

Manuel

Drew Hamlett

unread,
Nov 2, 2011, 9:05:47 AM11/2/11
to play-framework
Hi Manuel. Thanks for all your help on this. I found that your posts
and new article(http://logician.free.fr/index.php/2011/11/01/writing-
custom-deserializers-for-jerkson/comment-page-1/#comment-477) helped
out a lot. If you feel like getting an answer on stack overflow you
can post in my original thread. Thanks.

On Nov 1, 1:01 pm, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:
> Hi,
>
> here some code that does de/serialization for a BSON ObjectId:
>
> https://gist.github.com/1249014
>
> the principle should be exactly the same for a PK, you can see how the
> deserialization is handled in Play itself here:
>
> https://github.com/playframework/play-scala/blob/master/src/play/scal...

Pascal Voitot Dev

unread,
Nov 2, 2011, 9:09:19 AM11/2/11
to play-fr...@googlegroups.com
Hi,
Just for info: I used Jerkson and it works well with simple JSON mapping! But I had problems with complex mapping with case classes in case classes and fields containing "-" in their labels ("my-field") and jackson annotations.
Maybe newer versions correct those issues!

regards
Pascal

Drew Hamlett

unread,
Nov 2, 2011, 1:35:07 PM11/2/11
to play-framework
Now I'm having more trouble with the Option seralization. I can't
catch a break. I ended up just doing manual de-serialization using
Gson. It's ugly, terrible, un-maintainable, non DRY and will never
work in the long run but I need something that will work.

On Nov 1, 1:01 pm, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:
> Hi,
>
> here some code that does de/serialization for a BSON ObjectId:
>
> https://gist.github.com/1249014
>
> the principle should be exactly the same for a PK, you can see how the
> deserialization is handled in Play itself here:
>
> https://github.com/playframework/play-scala/blob/master/src/play/scal...

Manuel Bernhardt

unread,
Nov 3, 2011, 7:49:08 AM11/3/11
to play-fr...@googlegroups.com
On Wed, Nov 2, 2011 at 6:35 PM, Drew Hamlett <drew...@gmail.com>
wrote:> Now I'm having more trouble with the Option seralization.  I

can't> catch a break.  I ended up just doing manual de-serialization
using> Gson.  It's ugly, terrible, un-maintainable, non DRY and will
never> work in the long run but I need something that will work.
Do you mean serialization or deserialization of the Object? In case of
deserialization, you might need to add a default argument to the
member:

case class Thing(id: Option[ObjectId] = None, name: Option[String] =
Some("default"))

Manuel

Reply all
Reply to author
Forward
0 new messages