0

In scala, I am trying to convert json back to case class but the json sometimes contains empty string. In those cases, it throws:

com.fasterxml.jackson.databind.JsonMappingException: Instantiation of [simple type, class Person] value failed: null
  at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapException(StdValueInstantiator.java:399)
  at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:231)
  at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:135)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:442)
  at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1099)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:296)
  at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)
  at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
  at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
  at JacksonUtil$.fromJson(<console>:142)
  ... 58 elided
Caused by: java.lang.NullPointerException
  ... 66 more

My converter :

object JacksonUtil {

  private var mapper = new ObjectMapper() with ScalaObjectMapper
  intializeMapper( )

  def intializeMapper() = {
    // jackson library does not support seralization and deserialization of
// of scala classes like List and Map, this is needed to support it
mapper.registerModule( DefaultScalaModule )
// enables parsing of NaN. Enabling it here as JsonUtil class currently in
// use supports it.
mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true )
mapper.setSerializationInclusion(Include.NON_NULL)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setSerializationInclusion(Include.NON_EMPTY)
mapper.setSerializationInclusion(Include.NON_ABSENT)
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)


  }

  def fromJson(json: String, clazz: Class[_]) = {
     try {
    mapper.readValue( json, clazz )
    } catch {
      case e: com.fasterxml.jackson.core.JsonParseException => {
       println("Error while parsing JSON " + json)
      }
    }
  }
}

Can some one tell what I am doing wrong here ?

case class Person(itemId: Map[String, Int], itemNames: Map[String, Int]) extends Serializable

val test = """{}"""
JacksonUtil.fromJson(test, classOf[Person])

Is there any configuration I should add ? I can't change the jackson mapper due to some constraints.

I am getting same exception when try with proper one too :

 val test1 = """{"itemId": {"A": 1,"B": 8},"itemNames": {"AMC" : 2, "Regal" : 5}"""
    JacksonUtil.fromJson(test1, classOf[Person])
Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
user3407267
  • 1,524
  • 9
  • 30
  • 57
  • I think this post will answer your question https://stackoverflow.com/questions/21246733/serialize-an-object-with-no-data-in-jackson – Saurabh Gupta Mar 22 '18 at 22:14
  • @SaurabhGupta Tried that, still failing – user3407267 Mar 23 '18 at 00:01
  • I tried your code and it works fine for me. Your second JSON string is wrong,it needs `}` at the end. I tried commenting `//mapper.setSerializationInclusion(Include.NON_ABSENT)` as this configuration is not present in the jackson version that I'm using. I got `Person(null,null) Person(Map(A -> 1, B -> 8),Map(AMC -> 2, Regal -> 5))` after I edited your second JSON string. – vindev Mar 23 '18 at 05:59

0 Answers0