Skip to content

Instantly share code, notes, and snippets.

@negator
Last active January 15, 2019 13:34

Revisions

  1. Naveen Gattu revised this gist Mar 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    work with case classes with greater than 22 fields.
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:
    Note: ** Requires Play 2.3 and shapeless 2.1.0
    Note: ** Requires Play 2.3 and shapeless 2.0.0
    import SWrites._
  2. Naveen Gattu revised this gist Mar 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    work with case classes with greater than 22 fields.
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:
    Note: ** Requires Play 2.3 and shapeless 2.0.0
    Note: ** Requires Play 2.3 and shapeless 2.1.0
    import SWrites._
  3. Naveen Gattu revised this gist Sep 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@
    Additionally, you may get boilerplate free Format typeclasses:
    import SFormats._
    import SFormats.auto._
    case class Foo(value: String)
    case class Bar(value1: Int, foo: Foo)
  4. Naveen Gattu revised this gist Sep 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@
    Additionally, you may get boilerplate free json Format typeclasses:
    Additionally, you may get boilerplate free Format typeclasses:
    import SFormats._
  5. Naveen Gattu revised this gist Sep 27, 2014. 1 changed file with 63 additions and 30 deletions.
    93 changes: 63 additions & 30 deletions shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,9 @@
    work with case classes with greater than 22 fields.
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:
    Note: ** Requires Play 2.3 and shapeless 2.0.0
    import SWrites._
    import SReads._
    @@ -21,7 +22,21 @@
    implicit val reads: Reads[Bar] = SReads.auto.derive[Bar]
    Note: ** Requires Play 2.3 and shapeless 2.0.0
    Additionally, you may get boilerplate free json Format typeclasses:
    import SFormats._
    case class Foo(value: String)
    case class Bar(value1: Int, foo: Foo)
    def someFunc(value: T)(implicit val format: Format[T]) = ...
    **/

    @@ -31,39 +46,57 @@
    import shapeless.{ `::` => :#:, _ }
    import poly._

    object SWrites extends LabelledProductTypeClassCompanion[Writes] {
    implicit val writesInstance = new LabelledProductTypeClass[Writes] {
    implicit val writesInstance: LabelledProductTypeClass[Writes] = new LabelledProductTypeClass[Writes] {

    def emptyProduct: Writes[HNil] = Writes(_ => Json.obj())
    def emptyProduct: Writes[HNil] = Writes(_ => Json.obj())

    def product[F, T <: HList](name: String, FHead: Writes[F], FTail: Writes[T]) = Writes[F :#: T] {
    case head :#: tail =>
    val h = FHead.writes(head)
    val t = FTail.writes(tail)
    def product[F, T <: HList](name: String, FHead: Writes[F], FTail: Writes[T]) = Writes[F :#: T] {
    case head :#: tail =>
    val h = FHead.writes(head)
    val t = FTail.writes(tail)

    (h, t) match {
    case (JsNull, t: JsObject) => t
    case (h: JsValue, t: JsObject) => Json.obj(name -> h) ++ t
    case _ => Json.obj()
    }
    }
    def project[F, G](instance: => Writes[G], to: F => G, from: G => F) = Writes[F](f => instance.writes(to(f)))
    (h, t) match {
    case (JsNull, t: JsObject) => t
    case (h: JsValue, t: JsObject) => Json.obj(name -> h) ++ t
    case _ => Json.obj()
    }
    }
    def project[F, G](instance: => Writes[G], to: F => G, from: G => F) = Writes[F](f => instance.writes(to(f)))
    }
    object SReads extends LabelledProductTypeClassCompanion[Reads] {
    implicit val readsInstance = new LabelledProductTypeClass[Reads] {
    object SWrites extends LabelledProductTypeClassCompanion[Writes]

    implicit val readsInstance: LabelledProductTypeClass[Reads] = new LabelledProductTypeClass[Reads] {

    def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil))
    def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil))

    def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] {
    case obj @ JsObject(fields) =>
    for {
    head <- FHead.reads(obj \ name)
    tail <- FTail.reads(obj - name)
    } yield head :: tail
    case _ => JsError("Json object required")
    }
    def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] {
    case obj @ JsObject(fields) =>
    for {
    head <- FHead.reads(obj \ name)
    tail <- FTail.reads(obj - name)
    } yield head :: tail

    def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads)
    case _ => JsError("Json object required")
    }
    }

    def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads)
    }
    object SReads extends LabelledProductTypeClassCompanion[Reads]

    implicit val formatInstance: LabelledProductTypeClass[Format] = new LabelledProductTypeClass[Format] {
    def emptyProduct: Format[HNil] = Format(
    readsInstance.emptyProduct,
    writesInstance.emptyProduct
    )

    def product[F, T <: HList](name: String, FHead: Format[F], FTail: Format[T]) = Format[F :#: T] (
    readsInstance.product[F, T](name, FHead, FTail),
    writesInstance.product[F, T](name, FHead, FTail)
    )

    def project[F, G](instance: => Format[G], to: F => G, from: G => F) = Format[F](
    readsInstance.project(instance, to, from),
    writesInstance.project(instance, to, from)
    )
    }
    object SFormats extends LabelledProductTypeClassCompanion[Format]
  6. Naveen Gattu revised this gist Jul 30, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions shapeless-json.scala
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:
    import SWrites._
    import SReads._
    case class Foo(value: String)
    case class Bar(value1: Int, foo: Foo) //Didnt want to type out 23 fields, but you get the idea
  7. Naveen Gattu renamed this gist Jul 30, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. Naveen Gattu revised this gist Jul 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.gist
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    /**
    The Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt
    work with case class with greater than 22 fields.
    work with case classes with greater than 22 fields.
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:

  9. Naveen Gattu revised this gist Jul 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion shapeless-json.gist
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@
    implicit val reads: Reads[Bar] = SReads.auto.derive[Bar]


    Note: ** Requires Play 2.3 and shapeless
    Note: ** Requires Play 2.3 and shapeless 2.0.0

    **/

  10. Naveen Gattu revised this gist Jul 30, 2014. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions shapeless-json.gist
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@


    /*Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt
    work with case class with greater than 22 fields (scala 2.10 has removed this restriction).
    THe following code leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    /**
    The Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt
    work with case class with greater than 22 fields.
    The following gist leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:

    case class Foo(value: String)
    @@ -15,7 +16,10 @@
    implicit val writes: Writes[Bar] = SWrites.auto.derive[Bar]
    implicit val reads: Reads[Bar] = SReads.auto.derive[Bar]

    */

    Note: ** Requires Play 2.3 and shapeless

    **/

    import play.api.libs._
    import json._
  11. Naveen Gattu renamed this gist Jul 30, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. Naveen Gattu renamed this gist Jul 30, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. Naveen Gattu created this gist Jul 30, 2014.
    61 changes: 61 additions & 0 deletions gistfile1.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@


    /*Play (2.3) json combinator library is arguably the best in the scala world. However it doesnt
    work with case class with greater than 22 fields (scala 2.10 has removed this restriction).
    THe following code leverages the shapeless 'Automatic Typeclass Derivation' facility to work around this
    limitation. Simply stick it in a common location in your code base, and use like so:
    case class Foo(value: String)
    case class Bar(value1: Int, foo: Foo) //Didnt want to type out 23 fields, but you get the idea
    implicit val writes: Writes[Foo] = SWrites.auto.derive[Foo]
    implicit val reads: Reads[Foo] = SReads.auto.derive[Foo]
    implicit val writes: Writes[Bar] = SWrites.auto.derive[Bar]
    implicit val reads: Reads[Bar] = SReads.auto.derive[Bar]
    */

    import play.api.libs._
    import json._

    import shapeless.{ `::` => :#:, _ }
    import poly._

    object SWrites extends LabelledProductTypeClassCompanion[Writes] {
    implicit val writesInstance = new LabelledProductTypeClass[Writes] {

    def emptyProduct: Writes[HNil] = Writes(_ => Json.obj())

    def product[F, T <: HList](name: String, FHead: Writes[F], FTail: Writes[T]) = Writes[F :#: T] {
    case head :#: tail =>
    val h = FHead.writes(head)
    val t = FTail.writes(tail)

    (h, t) match {
    case (JsNull, t: JsObject) => t
    case (h: JsValue, t: JsObject) => Json.obj(name -> h) ++ t
    case _ => Json.obj()
    }
    }
    def project[F, G](instance: => Writes[G], to: F => G, from: G => F) = Writes[F](f => instance.writes(to(f)))
    }
    }
    object SReads extends LabelledProductTypeClassCompanion[Reads] {
    implicit val readsInstance = new LabelledProductTypeClass[Reads] {

    def emptyProduct: Reads[HNil] = Reads(_ => JsSuccess(HNil))

    def product[F, T <: HList](name: String, FHead: Reads[F], FTail: Reads[T]) = Reads[F :#: T] {
    case obj @ JsObject(fields) =>
    for {
    head <- FHead.reads(obj \ name)
    tail <- FTail.reads(obj - name)
    } yield head :: tail
    case _ => JsError("Json object required")
    }

    def project[F, G](instance: => Reads[G], to: F => G, from: G => F) = Reads[F](instance.map(from).reads)
    }
    }