I have many large JSON documents, and to save space, when a value does not exist, it's key is omitted.
Here's a scaled down example, where one of the addresses is missing a city:
case class Address(city: String, state: String)
case class Person(name: String, addresses: List[Address])
implicit val formats = DefaultFormats
val json = parse(""" { "name" : "Billy", "addresses" : [{"city": "New York", "state": "NY"}, {"state": "MA"}] } """)
println(json.extract[Person])
But I keep getting errors like this:
//> net.liftweb.json.MappingException: No usable value for addresses
//| No usable value for city
//| Did not find value which can be converted into java.lang.String
//| at net.liftweb.json.Meta$.fail(Meta.scala:191)
//| at net.liftweb.json.Extraction$.mkValue$1(Extraction.scala:357)
//| at net.liftweb.json.Extraction$.build$1(Extraction.scala:317)
//| at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
//| at net.liftweb.json.Extraction$$anonfun$12.apply(Extraction.scala:253)
//| at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
//| .scala:233)
//| at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
//| .scala:233)
//| at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.
//| scala:59)
//| at scala.collection.immutable.List.foreach(List.scala:76)
//| at scala.collection.TraversableLike$class.map(TraversableLike.scala:233)
Is there a way to make lift-json pass in a null or empty string if a key doesn't exist? (rather than throwing an exception)
Or some other clean way to handle the many cases where a key is missing?
Thanks