spray-json
spray-json copied to clipboard
Support unboxed (de)serialization of single-member types
see this thread
I have been using this horrible hack, but I would love to move to a more general AnyVal
approach
/**
* Flattens the JSON representation of a case class that contains a single `value`
* element from:
*
* {{{
* {"value": "..."}
* }}}
*
* to `"..."`
*/
class SingleValueCaseClassFormat[T <: {def value : V}, V](val construct: V => T)(implicit delegate: JsonFormat[V]) extends JsonFormat[T] {
import scala.language.reflectiveCalls
override def write(obj: T) = delegate.write(obj.value)
override def read(json: JsValue) = construct(delegate.read(json))
}
object SingleValueCaseClassFormat {
def apply[T <: {def value : V}, V](construct: V => T)(implicit delegate: JsonFormat[V]) = new SingleValueCaseClassFormat(construct)
}
Any thoughts when this can be implemented?
actually, I vote to close. This all falls under https://github.com/ensime/ensime-server/tree/master/spray-json-shapeless/
@fommil could you please suggest how to (de)serialize unboxed AnyVal using https://github.com/fommil/spray-json-shapeless ?
In lieu of a solution, the handling of value classes should be documented on the README. I'm willing to write the docs.
If you can whip up some shot note for the README that would be nice :) PR welcome :)
https://github.com/circe/circe/issues/469
https://github.com/spray/spray-json/pull/235
I retracted my earlier comment that jsonFormat1
didn't work on value classes: I couldn't reproduce the error in a simple test.