2014-08-30 7 views
1

RescueTime API와 spray-json을 통해 인터페이스를 시도하고 있습니다. 결과는 다음 형식과 같습니다.spray-json - 암시 적 확장이 다양 함

{ 
"notes":"data is an array of arrays (rows), column names for rows in row_headers", 
"row_headers":[ 
    "Rank", 
    "Time Spent (seconds)", 
    "Number of People", 
    "Activity", 
    "Category", 
    "Productivity" 
], 
"rows":[ 
    [ 
    1, 
    16611, 
    1, 
    "iTerm", 
    "Systems Operations", 
    2 
    ] 
] 
} 

아직까지는 오른쪽 사례 클래스를 해싱하려고 시도하고 있습니다. 여기에 지금까지있어 무엇 : 암시 적 확장을 발산에서 가장 거칠고의

Error:(31, 15) diverging implicit expansion for type spray.json.JsonWriter[test.RescueTimeApiResult] 
starting with method rescueTimeApiResultFormat in object MyJsonProtocol 
    println(obj.toJson) 
     ^

나의 이해 :

package test 

import spray.json._ 

case class RescueTimeApiResult(notes: String, row_headers: List[String], rows: List[List[Either[Int, String]]]) 

object MyJsonProtocol extends DefaultJsonProtocol { 
    implicit def rescueTimeApiResultFormat[T: JsonFormat] = jsonFormat3(RescueTimeApiResult) 
} 

private object Tester extends App { 
    val notes = "data is an array of arrays (rows), column names for rows in row_headers" 
    val headers = List("Rank", "Time Spent (seconds)", "Number of People", "Activity", "Category", "Productivity") 

    val entry1 = List(wrapInt(1), 
       wrapInt(16611), 
       wrapInt(1), 
       wrapString("iTerm"), 
       wrapString("Systems Operations"), 
       wrapInt(2)) 

    def wrapInt(x: Int): Either[Int, String] = Left(x) 
    def wrapString(s: String): Either[Int, String] = Right(s) 

    val rows = List(entry1) 

    val obj = RescueTimeApiResult(notes, headers, rows) 
    println(obj) 

    import MyJsonProtocol._ 
    println(obj.toJson) 
} 

scalac 수율 잘 좋은,하지만 컴파일시. 대략 암시 적 검색에는주기가 있습니다. 알다시피, toJson 암시 적은 암시 적 JsonWriter 암시적인 spray.json._ 가져 오기에서옵니다. 이것은 차례로 개체에 의해 제공됩니다. 그러나이 암시 적 매개 변수가 순환을 소개하는 이유는 저 밖에 있습니다. 한 이론은 Any에 적용되는 toJson 변환이 암시 적 매개 변수 해석을 방해 할 수 있지만 이는 단지 직감에 불과합니다.

도움을 주시면 대단히 감사하겠습니다.

답변

1

나는 일반적인 해결책에 여전히 관심이 있지만 해결책을 찾았습니다.

기본적으로이 문제는 MyJsonProtocol의 암시적인 정의로 줄였습니다. 암시적인 매개 변수를 val이 아니라 def으로 선언하여 평가 지연에 많은 유용성이 없었습니다. 또한, type 매개 변수를 제거 할 때, 유스 케이스가 구체적이므로, 전체 확장 된 암시 적 확장 비트를 처리 할 수있었습니다.