1

I am trying to understand Spray Json and very new to Scala. I have Seq(Seq("abc", 123, false, null), Seq("def", 45, "1234", 'C')) so a Seq[Seq[Any]]. I am not sure how to go about this and I cannot find any examples online.

case class SeqFormat(Seq[Seq[Any]]) => {
    // something that would convert to Seq[Seq[String]] 
    //which will would return a Json like this
    //[["abc", "123", "false", "null"],["def", "45", "1234", "C"]]
}

I tried

val someSeq = [["abc", "123", "false", "null"],["def", "45", "1234", "C"]]
val myObj = someSeq.toJson
// This gives me an error saying Any is not valid

I would appreciate any hints or snippets to help me understand this.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
ben
  • 137
  • 1
  • 2
  • 10

1 Answers1

1

You could use an encoder such as:

import spray.json._
import DefaultJsonProtocol._

implicit object AnyJsonFormat extends JsonFormat[Any] {

  def write(x: Any) =
    try {
      x match {
        case n: Int    => JsNumber(n)
        case s: String => JsString(s)
        case c: Char   => JsString(c.toString)
        case _         => JsString("null")
      }
    } catch {
      case _: NullPointerException => JsString("null")
    }

  def read(value: JsValue) = ???
}

Which you can use as follow:

val input = Seq(Seq("abc", 123, false, null), Seq("def", 45, "1234", 'C'))
println(input.toJson)

In order to get:

[["abc",123,"null","null"],["def",45,"1234","C"]]

This is an adapted version of this post: Serialize Map[String, Any] with spray json

Notice the NullPointerException handling for the null case.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
  • This does not seem to work with `val input = Seq(Seq("abc", "123", "false","null)", Seq(1,2,3,4,5)) println(input.toJson)` i.e. when each of the set has the same type of variables. It displays an error saying Cannot find jsonWriter or jsonFormat type class for Seq[Seq[AnyVal]]! Any suggestions on how to fix that – ben Mar 16 '18 at 20:24
  • @ben What's your error? Shouldn't it be `val input = Seq(Seq("abc", "123", "false","null"), Seq(1,2,3,4,5))`? – Xavier Guihot Mar 16 '18 at 20:28
  • i am running the code on intellij and it says "Connot find JsonWriter or JsonFormat type class for List[Seq[AnyVal]] && not enough arguments for method toJson: (implicit writer: spray.json.JsonWriter[List[Seq[AnyVal]]])spray.json.JsValue." I tried to change the Any to AnyVal but then my other test values gives me error says expected:Any found:AnyVal – ben Mar 16 '18 at 20:37
  • I'm using this version of json spray `"io.spray" %% "spray-json" % "1.3.3"`. Did you include the imports?: `import spray.json._` and `import DefaultJsonProtocol._` – Xavier Guihot Mar 16 '18 at 20:40
  • yeah I am using the same `libraryDependencies += "io.spray" %% "spray-json" % "1.3.3"` – ben Mar 16 '18 at 20:41
  • Is the `AnyJsonFormat` in the same class/object as the call to `println(input.toJson)`? – Xavier Guihot Mar 16 '18 at 20:44
  • Yeah they are in the same object. Maybe int's the intellij, lemme try running it on terminal directly – ben Mar 16 '18 at 20:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166996/discussion-between-xavier-guihot-and-ben). – Xavier Guihot Mar 16 '18 at 20:54