akka http marshalling/unmarshalling

528 views
Skip to first unread message

Leon Ma

unread,
Jun 19, 2015, 4:19:16 AM6/19/15
to akka...@googlegroups.com
Hi,

I just checked some code on 2.3-dev branch:


It seems that akka-http use jackson to support java bean marshall/unmarshall and use spray json to support scala classes, right?
Or the spray json can support both?

I have some questions:

1. What if I have cases that scala class and java class are mixing used, for example a case class reference a java bean, or java bean reference a scala class.
2. Do you have some implicit function which can act like a facade, and determine which json serializer to use by the input object or class?


I'm asking this is because I'm using json4s to do the marshall/unmarshll for my spray project.
And it turns out that json4s does not support plain java bean well, I have to optionally to choose which serializer to use, something like:


  private implicit def json4jMarshaller[T <: AnyRef](obj: T): Marshaller[T] =
    if (isJavaClass(obj))
      Marshaller.delegate[T, String](ContentTypes.`application/json`)(new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY).writeValueAsString(_))
    else
      Marshaller.delegate[T, String](ContentTypes.`application/json`)(org.json4s.jackson.Serialization.write(_))

  private def isJavaClass(v: Any): Boolean = {
    import reflect.runtime.universe._
    val typeMirror = runtimeMirror(v.getClass.getClassLoader)
    val instanceMirror = typeMirror.reflect(v)
    val symbol = instanceMirror.symbol
    symbol.isJava
  }


The purpose of doing this is to give user a unified entry instead of:

Ah, this is a java bean, and I'll import  JacksonProtocol._ 
oh, this is a scala case class, I should import json4sProtocol._


I'm just curious that whether akka-http has some solution to make it smoothly.

Thanks a lot!

Leon



Konrad Malawski

unread,
Jun 23, 2015, 12:22:51 PM6/23/15
to Akka User List
Hi Leon,
You're correct - we provide default helpers "...Support" classes/traits for 
Java and Scala - for Java it's Jackson and for Scala it's using SprayJson.

This however means that "if your routes are in a scala file, you can mix in the scala Support trait". Scala and Java classes are pretty much "all created equally" when you think about serializing them to json - you can use the jackson serializer from Scala as well.

It does not work the other way though, because Spray-Json uses Scala specific features which you can't really use (as nicely) from Java (type-classes).

In one sentence: feel free to use any json serialization library that fits your needs. When in Scala you can use the simple trait we provide for SprayJson, but adding your own support trait is pretty simple. :-)
I personally did not have trouble serializing scala/java mixed classes so I'm not sure what precise problem you bumped into here... 

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Konrad 'ktoso' Malawski
Reply all
Reply to author
Forward
0 new messages