Re: Is it possible to serialize / deserialize case classes without defining a protocol for each of them?

1,533 views
Skip to first unread message

Sean Walsh

unread,
Apr 26, 2013, 3:34:12 PM4/26/13
to spray...@googlegroups.com
Not to besmirch the excellent spray-json library but I had to go with json4s(ala) for this very reason, which uses Jackson under the covers.  With this library I can just do write(my object) or read[MyObject].  I haven't done any performance comparisons.

On Friday, April 26, 2013 11:06:59 AM UTC-4, michael...@googlemail.com wrote:
Hi,

I want to serialize case classes to json. I tried the example from the documentation and it works without problems:

object Test {
  case class Color(name: String, red: Int, green: Int, blue: Int)

  object MyJsonProtocol extends DefaultJsonProtocol {
    implicit val colorFormat = jsonFormat4(Color)
  }

  def main(args: Array[String]) {
    import MyJsonProtocol._

    val json = Color("CadetBlue", 95, 158, 160).toJson
    println(json)
    val color = json.convertTo[Color]
    println(color)
  }
}

I have a huge number of different case classes I want to serialize / deserialize. Using this approach I have to define a protocol for each class which is quite cumbersome. Is it possible to serialize / deserialize case classes using spray-json withou defining a protocol for each one of them?

Thanks,
Michael

Johannes Rudolph

unread,
Apr 29, 2013, 6:21:19 AM4/29/13
to spray...@googlegroups.com
On Fri, Apr 26, 2013 at 5:06 PM, <michael...@googlemail.com> wrote:
> I have a huge number of different case classes I want to serialize /
> deserialize. Using this approach I have to define a protocol for each class
> which is quite cumbersome. Is it possible to serialize / deserialize case
> classes using spray-json withou defining a protocol for each one of them?

There's an issue to track this:

https://github.com/spray/spray-json/issues/28

I don't think it's currently possible to support this in a type-safe,
configurable manner without falling back to reflection (but maybe that
would be ok). There's a solution on the horizon using macros, not sure
if it can be made to work with Scala 2.10 but maybe later with more
powerful macros.

--
Johannes

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

che...@tingatech.com

unread,
May 1, 2013, 12:55:36 PM5/1/13
to spray...@googlegroups.com, johannes...@googlemail.com
Not directly related to Spray , but if you use scala 2.10.1 and json4s
There is some conflict due to scala 2.10.0 and 2.10.1, which causing very strange behavior, you can read it here


Chester

david...@gmail.com

unread,
May 1, 2013, 3:40:35 PM5/1/13
to spray...@googlegroups.com, johannes...@googlemail.com
We've been successfully using jackson-module-scala for over a year with a pretty good level of success - we've had to work around a few problems, but most of them have been addressed in the current trunk of the project, and a few more (shameless plug for my own commits) will be released in the next point version.

We've written a simple adapter trait + object called JacksonSupport that when mixed in gives implicit serializer and deserializers for anything that extends Product - I've moved away from using this directly (not a huge fan of being able to accidentally arbitrarily send anything over the wire), but adding a single implicit val to the companion object for each case class you want to use as a transport is easy and works very very well.

--
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.
 
 

Diego Alvarez Zuluaga

unread,
Sep 25, 2013, 3:32:47 PM9/25/13
to spray...@googlegroups.com, johannes...@googlemail.com
Hi dpratt

Do you have any public working example with JacksonSupport trait and spray?

Tks
Reply all
Reply to author
Forward
0 new messages