2017-12-04 9 views
0

나는 내 프로젝트에 스칼라/스프레이 JSON 학습 및 스칼라 객체의 배열로 입력 JSON 변환하려고하지만, 아래의 오류 던지는 이잖아 오전 :스프레이 JSON : 배열로 변환 할 수 없습니다 [myClass가]

Error:(52, 43) not enough arguments for method convertTo: (implicit evidence$1: spray.json.JsonReader[Array[A$A57.this.myClass]])Array[A$A57.this.myClass]. 
Unspecified value parameter evidence$1. 
lazy val output = ref.parseJson.convertTo[Array[myClass]];} 
             ^
Error:(52, 43) Cannot find JsonReader or JsonFormat type class for Array[A$A57.this.myClass] 
lazy val output = ref.parseJson.convertTo[Array[myClass]];} 
을 다음은^

내 코드입니다 :

import spray.json.{JsArray, JsObject, _} 
import spray.json.{JsArray, JsFalse, JsNumber, JsObject, JsString, JsTrue, JsValue, JsonFormat, _} 

case class myClass (
               name: String, 
               source: Map[String, Any], 
               target: Map[String, Any]) 

trait myJsonProtocol extends DefaultJsonProtocol { 
    implicit object AnyJsonFormat extends JsonFormat[Any] { 
    def write(x: Any) = x match { 
     case b: Boolean if b == true => JsTrue 
     case b: Boolean if b == false => JsFalse 
     case n: Int => JsNumber(n) 
     case l: Long => JsNumber(l) 
     case f: Float => JsNumber(f.toString) 
     case d: Double => JsNumber(d.toString) 
     case s: String => JsString(s) 
     case a: Array[Any] => arrayFormat[Any].write(a) 
     case m: Map[String, Any]@unchecked => mapFormat[String, Any].write(m) 
     case x => serializationError("error Cannot determine object type " + x.getClass.getName) 
    } 

    def read(value: JsValue) = value match { 
     case JsTrue => true 
     case JsFalse => false 
     case JsNumber(n) => 
     if (n.isValidInt) n.intValue() 
     else if (n.isValidLong) n.longValue() 
     else if (n.isDecimalFloat) n.floatValue() 
     else if (n.isDecimalDouble) n.doubleValue() 
     else n.intValue() 
     case JsString(s) => s.toString 
     case a: JsArray => arrayFormat[Any].read(a) 
     case o: JsObject => mapFormat[String, Any].read(o) 
     case x => deserializationError("error: Cannot deserialize " + x) 
    } 
    } 

    implicit val myFormat = jsonFormat3(myClass.apply) 
} 

val ref = """[{ 
          "name": "test-db", 
          "source": { 
          "mydbport": 50000 
          }, 
          "target": { 
          "type": "newdb" 
          } 
          }]""" 
val output = ref.parseJson.convertTo[Array[myClass]] 

내가지도 직렬화 할 수 Serialize Map[String, Any] with spray json에서 참고로 위의 코드를했다하지만 [문자열을, 어떤] 하자 내가 위의 코드에 무엇이 잘못된 것인지 압니다.

감사합니다, 작업 암시 적 방법에 대한 게리

답변

0

, 그들은 클래스의 인스턴스에서 범위에 있어야합니다. 형질이 어디에도 설명되어 있지 않기 때문에 이것은 효과가 없습니다. 특성 대신 개체를 사용할 수 있습니다. 당신이 직장에 그것을 얻을 수있는 방법을

예는 다음과 같습니다

… 
object myJsonProtocol extends DefaultJsonProtocol { 
    implicit object AnyJsonFormat extends JsonFormat[Any] { 
    … 
    } 

    implicit val myFormat = jsonFormat3(myClass.apply) 
} 

import myJsonProtocol.myFormat 

val ref = """[{ 
          "name": "test-db", 
          "source": { 
          "mydbport": 50000 
          }, 
          "target": { 
          "type": "newdb" 
          } 
          }]""" 
val output = ref.parseJson.convertTo[Array[myClass]]