Re: [spray-user] Spray-JSON and nested List/Map

1,580 views
Skip to first unread message

Johannes Rudolph

unread,
Nov 7, 2012, 10:35:13 AM11/7/12
to spray...@googlegroups.com
Hi François-Xavier,

did you import the predefined JsonFormats with

import DefaultJsonProtocol._

?

I guess that won't be enough, because since you have a Map of type
Map[String, Object] you need a JsonWriter[Object] around. Is Object
really the most concrete type you know for the values in the Map?

Johannes

On Wed, Nov 7, 2012 at 4:18 PM, François-Xavier Thomas
<fx.t...@gmail.com> wrote:
> Hi all,
>
> In order to send data to an API (http://trakt.tv/api-docs/movie-seen for an
> example of what I want to send), I created a structure with nested Lists and
> Maps. I do not want to have to create a case class and a JSONFormat object,
> as there's no need to access that particular structure after sending it to
> the server, and the data is not that complicated.
>
> What I would like Spray to do is : traverse the whole structure (a
> Map[String, Object] according to the Scala type system) and output the JSON.
> If needed (but there should be no need to, all the fields are Strings), use
> the toString method to get a String.
>
> What Spray does is : "Error: Cannot find JsonWriter or JsonFormat type class
> for scala.collection.immutable.Map[String,Object]".
>
> Any idea on how to solve that?
>
> Thanks,
> François-Xavier
>
> --
>
>



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

François-Xavier Thomas

unread,
Nov 7, 2012, 4:46:49 PM11/7/12
to spray...@googlegroups.com, johannes...@googlemail.com
On Wednesday, November 7, 2012 4:35:36 PM UTC+1, Johannes Rudolph wrote:
Hi François-Xavier,

did you import the predefined JsonFormats with

import DefaultJsonProtocol._

Yup. A subclass of it anyway. 

I guess that won't be enough, because since you have a Map of type 
Map[String, Object] you need a JsonWriter[Object] around. Is Object
really the most concrete type you know for the values in the Map?

It's specifically defined as (authParams being a Map[String,String]) :

val data = authParams ++ Map("movies" -> List(Map("imdb_id" -> movie.imdb_id)))

So I guess there's no simplifying it. I was hoping Spray could simply "know" in some way that my map could be translated into a JSON dictionary.
After fiddling with it for a while, adding an implicit JsonFormat for Object works, but that seems really hacky, and I'm thinking it could pose issues later.

Is there a better way?

François-Xavier Thomas

unread,
Nov 7, 2012, 4:47:53 PM11/7/12
to spray...@googlegroups.com, johannes...@googlemail.com
Correction : Adding an implicit for Object _doesn't_ work. I'm getting (of course) the value for List(...).toString, and not the individual Strings.

Any other idea?

Johannes Rudolph

unread,
Nov 9, 2012, 4:42:35 AM11/9/12
to spray...@googlegroups.com
On Wed, Nov 7, 2012 at 4:18 PM, François-Xavier Thomas
<fx.t...@gmail.com> wrote:
> In order to send data to an API (http://trakt.tv/api-docs/movie-seen for an
> example of what I want to send), I created a structure with nested Lists and
> Maps. I do not want to have to create a case class and a JSONFormat object,
> as there's no need to access that particular structure after sending it to
> the server, and the data is not that complicated.

But would it do any harm to use case classes? That way you are
documenting the API you are using on the Scala side as well and can
make use of all of the machinery which is already in place.

François-Xavier Thomas

unread,
Nov 9, 2012, 4:49:31 AM11/9/12
to spray...@googlegroups.com
On Nov 9, 2012, at 10:42 AM, Johannes Rudolph wrote:

> On Wed, Nov 7, 2012 at 4:18 PM, François-Xavier Thomas
> <fx.t...@gmail.com> wrote:
>> In order to send data to an API (http://trakt.tv/api-docs/movie-seen for an
>> example of what I want to send), I created a structure with nested Lists and
>> Maps. I do not want to have to create a case class and a JSONFormat object,
>> as there's no need to access that particular structure after sending it to
>> the server, and the data is not that complicated.
>
> But would it do any harm to use case classes? That way you are
> documenting the API you are using on the Scala side as well and can
> make use of all of the machinery which is already in place.

For now I just wanted to do some quick testing, so I didn't want to have to deal with creating tons of case classes.
I'll try to do it, it could be a good thing to have that API available for Scala somewhere.

But even so, it would be logical for arbitrary nested List/Maps to be handled exactly the same way, without having to define JsonFormats...

Thanks for the help anyway,
François-Xavier

Mathias

unread,
Nov 9, 2012, 4:55:04 AM11/9/12
to spray...@googlegroups.com
François-Xavier,

what you want would require spray-json to look at runtime-type information via reflection, rather than relying only on the types known at compile time.
So far we only support the latter because it's both cleaner and faster.

However, you are of course free to yourself construct a JsonFormat for arbitrary types that uses runtime types.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
>
>

François-Xavier Thomas

unread,
Nov 9, 2012, 4:59:26 AM11/9/12
to spray...@googlegroups.com
Ah, now I understand! Forgive me for being a beginner in both Scala and Spray ;)
I'll use case classes in that case, it'll be enough.

Cheers,
François-Xavier
Reply all
Reply to author
Forward
0 new messages