java - Unable to perform Jackson deserialisation for boolean and int - Stack Overflow

My XML<Parent><childs><child>1<child><child>0<child><childs&

My XML

<Parent>
   <childs>
     <child>1</child>
     <child>0</child>
   </childs>
</Parent>

Pojo Structure is

class Parent
{
   @JacksonXmlElementWrapper(localName = "childs")
   ArrayList<Child> childs;
}
class Child
{ 
    @JacksonXmlProperty(localName = "child")
    boolean child;
}

Now as per my understanding, Jackson should convert this value 1/0 to boolean. But since Jackson is assuming it as String, I am getting this error:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type boolean from String "1": only "true"/"True"/"TRUE" or "false"/"False"/"FALSE" recognized

I know this can be solved using custom deserialization, but I'm looking for a cleaner solution.

Other Tried Solutions:

  1. @JsonFormat(shape = JsonFormat.Shape.STRING) private int chlid; //Didn't work
  2. CoercionConfig //Didn't work
  3. @JsonCreator with constructor and string as parameter //Worked but not feasible to write unused constructor in every class

My XMLMapper

    this.mXmlMapper = new XmlMapper();
    this.mXmlMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
    this.mXmlMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    this.mXmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    this.mXmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);

Same issue is for int variables, error stating String cannot be converted to int.

Using this in Android for project.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745058902a4608841.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信