I'm running into a situation where I'm retrieving some Json from an external server (I do not have any control over that server). The Json has one element that may occur 1 or more times. I'm trying to parse it using the net.liftweb.json facilities and that works fine only if the element occurs more than once. If the element occurs only once, it fails to parse.
Here's some example code:
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
case class JSonListIssue(foo: List[String])
class JSonTest extends TestCase {
implicit val formats = net.liftweb.json.DefaultFormats;
def testJsonList {
val jsonStr2Foos = "{\"foo\": \"bar\", \"foo\": \"bar2\"}"
val json = (parse(jsonStr2Foos).extract[JSonListIssue])
assertEquals(2, json.foo.size)
val jsonStr1Foo = "{\"foo\": \"bar\"}"
val json2 = (parse(jsonStr1Foo).extract[JSonListIssue]) // Results in Json MappingException
assertEquals(1, json2.foo.size)
}
}
The second parse statement fails in the above code. If I would define the case class as follows, the second parse would work, but the first would fail.
case class JSonListIssue(foo: String)
Any suggestions on how to solve this in a clean way? I could of course catch the MappingException and then parse it using the other case class, but that dirty...
Thanks, Gero