I'm trying to deserialize a json string into case class when some of the attribute is missing in json string using jackson
eg.
case class ParsedPage(crawlDate: Option[String], domain: Option[String], url:Option[String], text: Option[String], abc: Option[Long])
val js1 = """ {
"crawlDate": "ddkke",
"domain": "0x20.be",
"url": "http://www.beer.com?title=99_Bottles_of_Beer&id=6214",
"text": "99 Bottles of Beer From Whitespace Subpages",
"abc": 123
}"""
val js2 = """ {
"domain": "0x20.be",
"url": "http://www.beer.com?title=99_Bottles_of_Beer&id=6214",
"text": "99 Bottles of Beer From Whitespace Subpages",
"abc": 123
}"""
should give me
Ans1 = ParsedPage(Some("ddkke"), Some("0x20.be"), Some("http://www.beer.com?title=99_Bottles_of_Beer&id=6214", Some("99 Bottles of Beer From Whitespace Subpages"), Some(123))
Ans2 = ParsedPage(None, Some("0x20.be"), Some("http://www.beer.com?title=99_Bottles_of_Beer&id=6214", Some("99 Bottles of Beer From Whitespace Subpages"), Some(123))
What I've tried
import com.fasterxml.jackson.databind._
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
val objectMapper = new ObjectMapper() with ScalaObjectMapper
objectMapper.registerModule(DefaultScalaModule)
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
val ans = objectMapper.readValue[ParsedPage](js)
Getting exception:
com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class ParsedPage] value failed: null