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

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -