Search code examples
scalaakka-httpspray-json

Single List or list of lists json parsing failing


I have a json that sometimes returns List[List[x, y], List[a, b]] or if there is only one then List[x, y] ?

My case class defines

case class Geometry(dataType: String, coordinates: List[List[BigDecimal]])

Exception in thread "main"

spray.json.DeserializationException:
  Expected List as JsArray, but got x when there is only one list List[x,y]

How can I define such a json response. Thanks.


Solution

  • Use Either to encapsulate both possibilities:

    case class Geometry(
      dataType: String,
      coordinates: Either[List[BigDecimal], List[List[BigDecimal]]]
    )