EDIT
I try to make the question more clear. Say I am expecting a response like
{
"foo": [1, 2, 3],
"bar": [2, 5]
}
I want to be able to parse it; taking failure into account I want to get something of type Option[Map[String, List[Int]]]. Now the problem is that parsing errors can appear at any level. Maybe I get a map with string keys, but its values are not lists. Or maybe the values are lists, but the content of the lists are strings instead of ints. And so on.
Whenever I want to parse a value, say as a string, I can choose between x.as[String] and x.asOpt[String]. The former will throw an exception when dealing with wrong input, while the latter returns a Option[String]. Similarly, when dealing with an array, I can do pattern matching like
x match {
case JsArray(xs) => // continue parsing
case _ => // deal with failure
}
I can deal with failure returning None or with an exception.
If I choose to use exceptions all along, then I can just wrap everything inside a try..catch and be sure to have handled all failures. Otherwise, my problem is that at any level, I have to put an option. So I have to
- get something with an awful type like
Option[Map[Option[String], Option[List[Option[Int]]]]] - find some way to flatten this all the way down to turn into the more manageable
Option[Map[String, List[Int]]]
Of course, thing become even worse as soon as the JSON gets more nested.