2017-07-03 16 views
0
case class Box(id: String, container: Container) 
    case class Container(id: String, boxes: List[Box]) 

    object CustomProtocol extends DefaultJsonProtocol { 
    implicit object BoxFormat extends RootJsonFormat[Box] { 
     override def read(json: JsValue): Box = json.asJsObject.getFields("id", "container") match { 
     case Seq(JsString(id), container) => Box(id, container.convertTo[Container]) 
     } 
     override def write(obj: Box): JsValue = ??? 
    } 
    implicit object ContainerFormar extends RootJsonFormat[Container] { 
     override def read(json: JsValue): Container = json.asJsObject.getFields("id", "boxes") match { 
     case Seq(JsString(id), boxes) => Container(id, boxes.convertTo[List[Box]]) 
     } 
     override def write(obj: Container): JsValue = ??? 
    } 
    } 

コンパイル時にこのエラーメッセージが表示されます。スプレーjson逆シリアル化の問題

Cannot find JsonReader or JsonFormat type class for app.Main.Container 
    [error]   case Seq(JsString(id), container) => Box(id, container.convertTo[Container]) 
    [error]                  ^
    [error] one error found 
    [error] (compile:compileIncremental) Compilation failed 

私は箱とコンテナがフィールドとしてお互いを持っているからだと思います。どうすればこの問題を解決できますか?

答えて

0

case class Box(id: String, container: Container) 
    case class Container(id: String, boxes: List[Box]) 

    object CustomProtocol extends DefaultJsonProtocol { 
    implicit object BoxFormat extends JsonFormat[Box] { 
     override def read(json: JsValue): Box = json.asJsObject.getFields("id", "container") match { 
     case Seq(JsString(id), container) => Box(id, container.convertTo[Container](ContainerFormat)) 
     } 
     override def write(obj: Box): JsValue = ??? 
    } 

    implicit object ContainerFormat extends JsonFormat[Container] { 
     override def read(json: JsValue): Container = json.asJsObject.getFields("id", "boxes") match { 
     case Seq(JsString(id), boxes) => Container(id, boxes.convertTo[List[Box]](listFormat(BoxFormat))) 
     } 
     override def write(obj: Container): JsValue = ??? 
    } 
    } 
を解決