[Spray-Json] How to handle nested objects?

1,749 views
Skip to first unread message

Freek Wielstra

unread,
Sep 9, 2011, 6:56:29 AM9/9/11
to spray...@googlegroups.com
Hello all,

I have a (relatively simple) nested JSON object format, composed of two objects with values in them. I managed to deserialize the two objects without too much problems (besides scala etc being novel to me). However, I can't for the life of me figure out how I should have the 'parent' object deserialize. I haven't been able to find any examples of this anywhere, and my own messing about hasn't provided any solutions.

I've uploaded my code to gist, along with an example JSON object. I haven't been able to find out how to deserialize the composed object, or how to convert a JsValue or JsObject to its 'own' format. Can anyone help me out, provide a code example, or point me in the right directions?

Mathias

unread,
Sep 9, 2011, 7:11:18 AM9/9/11
to spray...@googlegroups.com
You are almost there.

The key is to not have three different custom JsonProtocols but just one, in which you put all your custom formats:

object MyJsonFormat extends DefaultJsonFormat {
implicit val tripFormat = jsonFormat(Trip, …)
implicit val notificationFormat = jsonFormat(Notification, …)
implicit val notificationPlanFormat = jsonFormat(NotificationPlan, "trip", "notification")
}

Cheers,
Mathias

---
mat...@spray.cc
http://www.spray.cc

Freek Wielstra

unread,
Sep 9, 2011, 7:25:43 AM9/9/11
to spray...@googlegroups.com
Hello Mathias,

That was exactly it - thanks a million for that solution!

philip andrew

unread,
Nov 12, 2013, 10:32:25 PM11/12/13
to spray...@googlegroups.com
Hi Mathias,

DefaultJsonFormat seems not to exist in the current version of Spray. What to do?

Thanks, Philip

Mathias Doenitz

unread,
Nov 13, 2013, 3:36:00 AM11/13/13
to spray...@googlegroups.com
Philip,

`DefaultJsonFormat` is provided by [spray-json](https://github.com/spray/spray-json).

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages