java - how to map empty string with Map using jackson mapper -


my json string

{      "fieldinfo":{         "field1":{            "fieldname":"test1",          "values":""       },       "field2":{            "fieldname":"test2",          "values":{               "test":"5",             "test1":"2"          }       }    } } 

i facing problem while map values filed. in json string, values filed either empty string or map. mapping values field in below mentioned variable.

@jsonproperty("values") private map<string, string> values; 

so problem mapping empty string map.it gives exception,

com.fasterxml.jackson.databind.jsonmappingexception: can not instantiate  value of type [map type; class java.util.linkedhashmap, [simple type, class  java.lang.string] -> [simple type, class java.lang.string]] string  value; no single-string constructor/factory method (through reference  chain: com.test.model.extrainformation["fieldinfo"]->com.test.model.fieldinfo["values"]) 

i have used @jsoninclude(include.non_null). not working.

it seems trying map string map when value empty string.

@jsonproperty("values") private map<string, string> values; 

jackson use setter method map values. setter detected jackson , used when property read json. within setter, can check if field either map or empty string. can accept object. , check it.. bellow...

 public void setvalues(object values) {     if(values instanceof string){    this.values = null;   }else{    this.values = (map<string, string>) values;   }  } 

hope ... help...


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -