scala read/write format for json with Type -
i having problems understanding how serialize type.
lets used serialize way:
class:
case class localizeditem(itemid:string, var value:string) { def this(itemid:option[string]) = this(itemid.getorelse(null), "") def this(itemid:string) = this(itemid, "") }
and in formatter do:
trait productformats extends errorformats { implicit val localizeditemformat = new format[localizeditem]{ def writes(item: localizeditem):jsvalue = { json.obj( "itemid" -> item.itemid, "value" -> item.value ) } def reads(json: jsvalue): jsresult[localizeditem] = jssuccess(new localizeditem( (json \ "itemid").as[string], (json \ "value").as[string] )) }
my question how use same pattern object receive generic item/type (generic have write/read implemented same way localizeditem has)
e.g:
case class dtoresponse[t](d: t) { def iserror = false def get() = d }
when trying implement in way:
implicit val dtoresponseformat = new format[dtoresponse] { def writes(item: dtorespons):jsvalue = { json.obj( "itemid" -> item.itemid, "value" -> item.value ) }
i receive error:
class dtoresponse takes type parameters
something along lines of:
implicit def dtoresponseformat[t: format] = new format[dtoresponse[t]] { val tformatter: format[t] = implicitly[format[t]] def writes(item: dtoresponse):jsvalue = { json.obj( "itemid" -> tformatter.writes(item.get()) ) } }
here assumption need format values of type t well. if not, can remove type constraint.
Comments
Post a Comment