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