scala - Play Json - Conflict when deserialize model -
i met strange issue in unit test serialization / deserializable of case class in json. i'm using play-json (2.5.10) , defined model events :
case class bffevent ( starttime: long, endtime: long, eventtype: bffeventtype.value, // enum payload: option[payload] ) object bffevent { implicit val bffeventformat = json.format[bffevent] } payload adt many sub-types put conflicted case classes :
sealed trait payload case class userflag ( flag: option[bffeventflag], freetext: option[string], source: option[bffeventsource], timestamp : option[timestamp] ) extends payload case class communicationmode(mode: int) extends payload implicit val payloadreads = { val userflag = json.reads[userflag] val communicationmode = json.reads[communicationmode] __.read[userflag](userflag).map(x => x: payload) | __.read[communicationmode](communicationmode).map(x => x: payload) } implicit val payloadwrites = writes[payload] { case userflag: userflag => json.writes[userflag].writes(userflag) case communicationmode: communicationmode => json.writes[communicationmode].writes(communicationmode) } in unit test, check if can read/write json of payload model :
val communicationmode = bffevent(0, 0, bffeventtype.communicationmode, some(communicationmode(0))) should "serialize , deserialize communicationmode" in { val validatedjson = json.tojson(communicationmode).validate[bffevent] validatedjson should be(jssuccess(communicationmode)) } test output :
bffevent(0,0,communicationmode,some(communicationmode(0))) jssuccess(bffevent(0,0,communicationmode,some(userflag(none,none,none,none))),) jssuccess(bffevent(0,0,communicationmode,some(userflag(none,none,none,none))),) not equal jssuccess(bffevent(0,0,communicationmode,some(communicationmode(0))),) scalatestfailurelocation: com.bioserenity.bff.test.test_bff_models_serialiazer$$anonfun$1 @ (test_bff_models_serialiazer.scala:81) expected :jssuccess(bffevent(0,0,communicationmode,some(communicationmode(0))),) actual :jssuccess(bffevent(0,0,communicationmode,some(userflag(none,none,none,none))),) while input json conversion bffevent communicationmode payload, translated in userflag. think issue comes order of match in payloadreads function changing order of match cases creates conflits if met issue before, can provides me tip solve or better, solution ?
Comments
Post a Comment