JASpitz
JASpitz

Reputation: 33

Converting nested lists to json gives superfluous arrays

I have some history data, that I want to convert to json. So these are Lists of lists. Their type is List[List[Position]] where Position is a simple case class. I wrote a formatter to help Json.toJson cope. I was expecting an output of exactly one outer array with two inner arrays that contain 3 objects each. What I got instead was this. Note the additional array wrappings.

[[[[{"amount":1.0,"minAmount":2.0,"price":3.0,"volume":4.0},
{"amount":5.0,"minAmount":6.0,"price":7.0,"volume":8.0},
{"amount":9.0,"minAmount":10.0,"price":11.0,"volume":12.0}]],
[[{"amount":0.1,"minAmount":0.2,"price":0.3,"volume":0.4},
{"amount":5.0,"minAmount":6.0,"price":7.0,"volume":8.0},
{"amount":9.0,"minAmount":10.0,"price":11.0,"volume":12.0}]]]]

I don't know where the wrapping arrays come from. Can somebody help me out here? This is a test with the wrapper I am using:

class ApplicationSpec extends Specification {

  implicit object PositionFormat extends Format[List[List[Position]]] {
    def writes(historyList: List[List[Position]]) : JsValue = {
      Json.arr(historyList.map{
        o => Json.arr(o.map{ p =>
          Json.obj(
            "amount"    -> JsNumber(p.amount),
            "minAmount" -> JsNumber(p.minAmount),
            "price"     -> JsNumber(p.price),
            "volume"    -> JsNumber(p.volume)
          )
        })
      })
    }
    def reads(json: JsValue): JsResult[List[List[Position]]] = ???
  }

  "Application" should {

    "Convert position data to json" in {
      val l1 = ListBuffer(new Position(1.0D,2.0D,3.0D,4.0D),
        new Position(5.0D,6.0D,7.0D,8.0D),
        new Position(9.0D,10.0D,11.0D,12.0D)).toList
      val l2 = ListBuffer(new Position(0.1D,0.2D,0.3D,0.4D),
        new Position(5.0D,6.0D,7.0D,8.0D),
        new Position(9.0D,10.0D,11.0D,12.0D)).toList
      val obj = ListBuffer(l1,l2).toList
      val json = Json.toJson(obj)
      var string: String = json.toString()
      println(string)
    }
  }
}

Upvotes: 3

Views: 1160

Answers (1)

Faiz
Faiz

Reputation: 16245

It seems that Json.arr takes it's arguments and returns a JsValue for a JSON array of them. It looks like you could simply do with Json.toJson:

Here's how arr is meant to be used:

// Json.arr
Json.arr("one", "two")
// Gives you
// play.api.libs.json.JsArray = ["one","two"]

If you instead do:

// vs
val l = List("one", "two")
Json.arr(l)
// Gives you
// play.api.libs.json.JsArray = [["one","two"]]
// ... a nested array, which is what you don't want.

What you need is:

// Json.toJson
Json.toJson(l)
// Gives you:
// play.api.libs.json.JsValue = ["one","two"]

Upvotes: 9

Related Questions