2014-03-25 1 views
0

스프레이 JSON 라이브러리를 사용하여 사례 클래스를 JSON으로 직렬화합니다. 문제는 우리가 몇 가지 상호 재귀 적 정의를 가지고 있다는 것입니다. 여기에 봉인 된 특성을 직렬화하는 예제에서 작업하고 있습니다. http://www.cakesolutions.net/teamblogs/2012/11/30/spray-json-and-adts/Spray를 사용하여 JSON에 직렬화하는 동안 implicits 및 주문 관련 문제를 효과적으로 해결할 수 있습니까?

다음은 간단한 예제입니다. C :

import spray.json._ 
import DefaultJsonProtocol._ 

sealed trait BaseTrait 
sealed trait SpecializedTrait extends BaseTrait 

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait 
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait 

case class C(foo: Int) extends BaseTrait 

object BaseTrait { 
    implicit val cJsonFormat = jsonFormat1(C) 

    implicit object BaseTraitJsonFormat extends RootJsonFormat[BaseTrait] { 
    override def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    override def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented") 
    } 
} 

object SpecializedTrait { 
    implicit val aJsonFormat = jsonFormat2(A) 
    implicit val bJsonFormat = jsonFormat2(B) 

    implicit object SpecializedTraitJsonFormat extends RootJsonFormat[SpecializedTrait] { 
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented") 
    } 
} 

"C"의 정의를 원래의 상호 재귀 적 정의로 변경하면.

분명히 아니오라고 말할 수는 없으며 상호 재귀 적 데이터 구조를 가질 수 없습니다. 암시 적 객체에 대한 해결 규칙 때문에 컴파일러가 문제가 된 것 같습니다.

해결 방법이 있습니까? 나는 마지막 조각 작품

import spray.json._ 
import DefaultJsonProtocol._ 

sealed trait BaseTrait 
sealed trait SpecializedTrait extends BaseTrait 

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait 
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait 

case class C(argC: SpecializedTrait, foo: Int) extends BaseTrait 

object BaseTrait { 
    implicit val cJsonFormat : RootJsonFormat[C] = jsonFormat2(C) 

    implicit val baseTraitFormat : RootJsonFormat[BaseTrait] = new RootJsonFormat[BaseTrait] { 
    def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented") 
    } 
} 

object SpecializedTrait { 
    implicit val aJsonFormat : RootJsonFormat[A] = jsonFormat2(A) 
    implicit val bJsonFormat : RootJsonFormat[B] = jsonFormat2(B) 

    implicit val specializedTraitFormat : RootJsonFormat[SpecializedTrait] = new RootJsonFormat[SpecializedTrait] { 
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented") 
    } 
} 

그건, "내장 객체"을 "암시 발"과 이후의 익명 클래스의 변경 사항에 주목 즉, 익명 클래스 선언에 개체를 변경하여 주위를 얻었다.

답변

0

implicits를 순서대로 정의하고 하나의 오브젝트에 넣으십시오.

import spray.json._ 
import DefaultJsonProtocol._ 

sealed trait BaseTrait 
sealed trait SpecializedTrait extends BaseTrait 

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait 
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait 
case class C(argC: SpecializedTrait, foo: Int) extends BaseTrait 

object SomeProtocol { 
    implicit object BaseTraitJsonFormat extends RootJsonFormat[BaseTrait] { 
    override def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    override def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented") 
    } 
    implicit object SpecializedTraitJsonFormat extends RootJsonFormat[SpecializedTrait] { 
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented") 
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented") 
    } 
    implicit val aJsonFormat = jsonFormat2(A) 
    implicit val bJsonFormat = jsonFormat2(B) 
    implicit val cJsonFormat = jsonFormat2(C) 
} 
+0

직렬화해야하는 상호 재귀 적 봉인 된 특성이있는 경우 작동하지 않습니다. – user3246214

+0

'SomeProtocol'은 여러분이 여러분의 질문에서 참조한'A','B','C' 사례 클래스를 직렬화 할 수있게 해줍니다 ('read'와'write' 메소드를 구현할 경우). 너는 할려고했다. –

관련 문제