spray-json icon indicating copy to clipboard operation
spray-json copied to clipboard

Support unboxed (de)serialization of single-member types

Open sirthias opened this issue 12 years ago • 8 comments

see this thread

sirthias avatar Dec 27 '12 13:12 sirthias

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)
}

fommil avatar Dec 27 '12 13:12 fommil

Any thoughts when this can be implemented?

13h3r avatar Jun 24 '15 09:06 13h3r

actually, I vote to close. This all falls under https://github.com/ensime/ensime-server/tree/master/spray-json-shapeless/

fommil avatar Jun 24 '15 10:06 fommil

@fommil could you please suggest how to (de)serialize unboxed AnyVal using https://github.com/fommil/spray-json-shapeless ?

savulchik avatar Jul 01 '16 15:07 savulchik

In lieu of a solution, the handling of value classes should be documented on the README. I'm willing to write the docs.

performantdata avatar Jul 15 '17 22:07 performantdata

If you can whip up some shot note for the README that would be nice :) PR welcome :)

ktoso avatar Jul 18 '17 14:07 ktoso

https://github.com/circe/circe/issues/469

fommil avatar Jul 18 '17 15:07 fommil

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.

performantdata avatar Jul 23 '17 07:07 performantdata