How can I Serialize/De-serialize a Boolean Value from FasterXML\Jackson as an Int?

后端 未结 2 597
小鲜肉
小鲜肉 2021-01-01 14:41

I\'m writing a JSON Client for a Server that returns Boolean values as \"0\" and \"1\". When I try to run my JSON Client I currently get the following Exception:

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 15:02

    Instead of custom deserializer, you could also simply have a setter like:

    public void setThisIsABoolean(String str) {
      if ("0".equals(str)) {
        bool = false;
      } else {
        bool = true;
      }
    }
    

    since your method can claim different type than what you use internally.

    And if you have to support both Boolean and String, you can indicate value is an Object, and check what you might get.

    It should even be possible to have different type for getter method (Boolean) and setter (String or Object).

提交回复
热议问题