scala - Akka-HTTP JSON serialization -
how 1 control deserialization spray-json? example, have class defined as:
case class (name:string, value:string)
and deserialize following json list of objects:
{ "one": "1", "two": "2" }
and should become:
list(a("one", "1"), a("two", "2"))
the problem default json representation of list one, not want:
[ { "name": "one", "value": "1" }, { "name": "two", "value": "2" } ]
how can accomplish this?
you can write own custom deserializer structure looking for:
case class a(name:string, value:string) implicit object listaformat extends rootjsonreader[list[a]] { override def read(json: jsvalue): list[a] = { json.asjsobject.fields.tolist.collect { case (k, jsstring(v)) => a(k, v) } } } import spray.json._ def main(args: array[string]): unit = { val json = """ |{ | "one": "1", | "two": "2" |} """.stripmargin val result = json.parsejson.convertto[list[a]] println(result) }
prints:
list(a(one,1), a(two,2))
Comments
Post a Comment