scala - Json Parse obj without case class ref -


i don't know how explain want do, i'll make example :

case class c1(id: string, name: string, description: string) case class c2(id: string, status: boolean)  trait test[anyref] {    implicit val writesc1 = json.writes[c1]   implicit val writesc2 = json.writes[c2]    def test(obj:anyref) = {     println(json.tojson(obj))   } }  object oc1 extends test[c1] {}  object oc2 extends test[c2] {}  val x = c1(1, "test", "desc test")  c1.test(x) // here want c1 instance parsed json 

how this:

trait test[t] {    implicit val writest = json.writes[t]    def test(obj: t) {     println(json.tojson(obj))   } }  object c1 extends test[c1]  val x = c1(1, "test", "desc test")  oc1.test(x) 

i unfortunately don't have play installed right can't test.

update if upper doesn't work, will:

trait test[t] {   implicit val writest: writes[t]   // ... }  object oc1 extends test[c1] {   implicit val writest = json.writes[c1] } 

but requires more typing.


Comments

Popular posts from this blog

ios - Memory not freeing up after popping viewcontroller using ARC -

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -