Play Scala Json Writer for Seq of Tuple -


i'm trying find way use built in macro json writer in order serialize seq[(string,customer)]

i managed seq[customer] when adding touple, compiler starts screaming @ me.

this code works:

package models.health  import play.api.libs.json._  case class customer(name: string, age: int)  //we use dummy var workaround json writer    limitations (cannot handle single argument case class) case class demo(customers: seq[customer], dummy: option[string] =    none)  object demo {   import play.api.libs.functional.syntax._   implicit val customer_writer = json.writes[customer]   implicit val writes: writes[demo] = (  (__ \ "customers").write[seq[customer]] ,  (__ \ "dummy").writenullable[string]) {      (d: demo) => (d.customers,d.dummy)  }  } 

but below code (simply change seq[customer] seq[(string,customer)] doesn't copmile... appreciated:

package models.health  import play.api.libs.json._  case class customer(name: string, age: int)  //we use dummy var workaround json writer    limitations (cannot handle single argument case class) case class demo(customers: seq[(string,customer], dummy: option[string] =    none)  object demo {   import play.api.libs.functional.syntax._   implicit val customer_writer = json.writes[customer]   implicit val writes: writes[demo] = (  (__ \ "customers").write[seq[(string,customer)]] ,  (__ \ "dummy").writenullable[string]) {      (d: demo) => (d.customers,d.dummy)  }  } 

this compiler error got:

no json serializer found type seq[(string,models.health.customer)] 

the library makes no assumption how want tuple serialize. use array, object, etc.

by adding implicit writes function, serializer write out array.

  implicit def tuple2writes[a, b](implicit a: writes[a], b: writes[b]): writes[tuple2[a, b]] = new writes[tuple2[a, b]] {     def writes(tuple: tuple2[a, b]) = jsarray(seq(a.writes(tuple._1), b.writes(tuple._2)))   } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -