1

I have the following situation.

import shapeless.{:+:, CNil}
import julienrf.json.derived.{DerivedOWrites, DerivedReads, NameAdapter}

    case class A(i : Int)
    case class B(s : String)

    object Event {
       type AllEvent = A :+: B :+: CNil
    implicit def allFormats[T](implicit dr: DerivedReads[T], dw: DerivedOWrites[T]): OFormat[T] = derived.oformat[T](NameAdapter.snakeCase)
    }
    case class C(id : String, event : Event.AllEvent)

and I want to be able to parse the following json into a C

import play.api.libs.json.{Json, OFormat}
import Event.allFormats
val json = Json.parse(
"""{
     "id" : "id",
     "event":{
       "i" : 1
     }
}"""
).as[C]

However I'm getting the error Error: No Json deserializer found for type C. Try to implement an implicit Reads or Format for this type. ).as[C]

It appears to be struggling to resolve a reads for A :+: B :+: CNil.

Whatever I try doesn't seem to work. Am I missing something obvious.

John Cragg
  • 201
  • 1
  • 2
  • 4
  • I've never used that JSON library, but looking at [the code](https://github.com/julienrf/play-json-derived-codecs/blob/master/library/src/main/scala/julienrf/json/derived/DerivedReads.scala) I think you need to `import julienrf.json.derived.DerivedReads._` – Chris B Feb 07 '19 at 17:03
  • Added it and it gives me the same error – John Cragg Feb 07 '19 at 17:11
  • 3
    play-json-derived-codecs doesn't provide instances for arbitrary coproducts, but only for labelled coproducts (i.e. Shapeless's generic representation of ADTs). In my view this is appropriate—in circe we have both, but the instances for arbitrary coproducts are in a circe-shapes module that's separate from circe-generic (which is roughly equivalent to derived-codecs). – Travis Brown Feb 07 '19 at 17:46
  • Okay, I'll give it a go! – John Cragg Feb 11 '19 at 09:56
  • I've got it working using code from https://stackoverflow.com/questions/52117213/generic-derivation-for-adts-in-scala-with-a-custom-representation, thanks a lot. I don't suppose theres a way to pass snake case configuration to these generic encoder/decoders without reproducing the derivation macros? – John Cragg Feb 11 '19 at 11:34

0 Answers0