如何从FasterXML \ Jackson序列化/反序列化布尔值作为Int?
我正在为服务器编写一个Json客户端,它将布尔值返回为“0”和“1”。 当我尝试运行我的Json客户端时,我目前得到以下exception:
HttpMessageNotReadableException: Could not read JSON: Can not construct instance of java.lang.Boolean from String value '0': only "true" or "false" recognized
那么如何设置FasterXML \ Jackson来正确解析类似的东西:
{ "SomeServerType" : { "ID" : "12345", "ThisIsABoolean" : "0", "ThisIsABooleanToo" : "1" } }
示例Pojo:
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({"someServerType"}) public class myPojo { @JsonProperty("someServerType") SomeServerType someServerType; @JsonProperty("someServerType") public SomeServerType getSomeServerType() { return someServerType; } @JsonProperty("someServertype") public void setSomeServerType(SomeServerType type) { someServerType = type; } }
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({"someServerType"}) public class SomeServerType { @JsonProperty("ID") Integer ID; @JsonProperty("ThisIsABoolean") Boolean bool; @JsonProperty("ThisIsABooleanToo") Boolean boolToo; @JsonProperty("ID") public Integer getID() { return ID; } @JsonProperty("ID") public void setID(Integer id) { ID = id; } @JsonProperty("ThisIsABoolean") public Boolean getThisIsABoolean() { return bool; } @JsonProperty("ThisIsABoolean") public void setThisIsABoolean(Boolean b) { bool = b; } @JsonProperty("ThisIsABooleanToo") public Boolean getThisIsABooleanToo() { return boolToo; } @JsonProperty("ThisIsABooleanToo") public void setThisIsABooleanToo(Boolean b) { boolToo = b; } }
rest客户线
注1:这是使用Spring 3.2
注2: toJSONString() – 是一个使用Jackson来序列化我的参数对象的辅助方法
注3:在读取结果对象时发生exception
DocInfoResponse result = restTemplate.getForObject(docInfoURI.toString() + "/?input={input}", DocInfoResponse.class, toJSONString(params));
正如Paulo Pedroso的回答中提到并引用的那样,您将需要推出自己的自定义JsonSerializer
和JsonDeserializer
。 创建后,您需要将@JsonSerialize
和@JsonDeserialize
注释添加到您的属性中; 指定要用于每个的类。
我在下面提供了一个小的(希望直截了当的)示例。 串行器和解串器实现都不是非常强大,但这应该可以帮助您入门。
public static class SimplePojo { @JsonProperty @JsonSerialize(using=NumericBooleanSerializer.class) @JsonDeserialize(using=NumericBooleanDeserializer.class) Boolean bool; } public static class NumericBooleanSerializer extends JsonSerializer { @Override public void serialize(Boolean bool, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { generator.writeString(bool ? "1" : "0"); } } public static class NumericBooleanDeserializer extends JsonDeserializer { @Override public Boolean deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { return !"0".equals(parser.getText()); } } @Test public void readAndWrite() throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); // read it SimplePojo sp = mapper.readValue("{\"bool\":\"0\"}", SimplePojo.class); assertThat(sp.bool, is(false)); // write it StringWriter writer = new StringWriter(); mapper.writeValue(writer, sp); assertThat(writer.toString(), is("{\"bool\":\"0\"}")); }
而不是自定义反序列化器,您也可以简单地使用如下的setter:
public void setThisIsABoolean(String str) { if ("0".equals(str)) { bool = false; } else { bool = true; } }
因为您的方法可以声明与您在内部使用的类型不同的类型。
如果您必须同时支持Boolean
和String
,则可以指示value是一个Object
,并检查您可能获得的内容。
甚至可以为getter方法( Boolean
)和setter( String
或Object
)设置不同的类型。