scala - spray-json can't find JsonReader for type List[T] -


i'm creating custom json readers case classes can't find implicit jsonreader type class list[t] used in other case class.

when checked defaultjsonprotocol, has implicit format collections already;

  implicit def listformat[t :jsonformat] = new rootjsonformat[list[t]] {     def write(list: list[t]) = jsarray(list.map(_.tojson).tovector)     def read(value: jsvalue): list[t] = value match {       case jsarray(elements) => elements.map(_.convertto[t])(collection.breakout)       case x => deserializationerror("expected list jsarray, got " + x)     }   } 

here simplified code;

case class test(i: int, d: double) case class listoftest(t: list[test])  trait testresultformat extends defaultjsonprotocol {    import customformat._      implicit object testjsonformat extends rootjsonreader[test] {      override def read(json: jsvalue): test = {        val jsobject = json.asjsobject       val jsfields = jsobject.fields        val = jsfields.get("i").map(_.convertto[int]).getorelse(0)       val d = jsfields.get("d").map(_.convertto[double]).getorelse(0d)        test(i, d)     }   }    implicit object listoftestjsonformat extends rootjsonreader[listoftest] {      override def read(json: jsvalue): listoftest = {        val jsobject = json.asjsobject       val jsfields = jsobject.fields        val tests = jsfields.get("hs").map(_.convertto[list[test]]).getorelse(list.empty)        listoftest(tests)     }   }  } 

here errors;

error:(230, 53) not enough arguments method convertto: (implicit evidence$1: spray.json.jsonreader[list[com.xx.test]])list[com.xx.test]. unspecified value parameter evidence$1.       val tests = jsfields.get("hs").map(_.convertto[list[test]]).getorelse(list.empty)                                                     ^ error:(230, 53) cannot find jsonreader or jsonformat type class list[com.xx.test]       val tests = jsfields.get("hs").map(_.convertto[list[test]]).getorelse(list.empty)                                                 ^ 

i think problem related fact jsonreader list[t] in defaultjsonprotocol rootjsonformat (not rootjsonreader), means can read , write it. so, when try read list[item], it's expected able write items. so, use rootjsonformat instead , throw exception in case try write (since don't support it). example:

import spray.json._  implicit object testjsonformat extends rootjsonformat[test] {    override def read(json: jsvalue): test = {      val jsobject = json.asjsobject     val jsfields = jsobject.fields      val = jsfields.get("i").map(_.convertto[int]).getorelse(0)     val d = jsfields.get("d").map(_.convertto[double]).getorelse(0d)      test(i, d)   }    override def write(obj: test): jsvalue = serializationerror("not supported") } 

if aware of clean solution involving readers, please let me know because ran problem myself , couldn't find else.


Comments

Popular posts from this blog

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

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

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