0

I am learning scala/spray json in my project and trying to convert an input json to an array of scala objects , but thats throwing the below error:

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]];}

^

Here is my code snippet:

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]]

I took the above code as reference from Serialize Map[String, Any] with spray json but not able to serialize Map[String,Any] Let me know what is wrong with the above code.

Regards, Gary

Gary
  • 31
  • 4

1 Answers1

0

For implicit methods to work, they need to be in scope from an instance of the class. As the trait is nowhere instantiated, this doesn't work. You could use an object instead of trait.

An example how you could get that to work is:

…
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]]
Mark
  • 1,181
  • 6
  • 18