gpt4 book ai didi

java - 如何将 FasterXML\Jackson 中的 boolean 值序列化/反序列化为 Int?

转载 作者:IT老高 更新时间:2023-10-28 13:50:36 24 4
gpt4 key购买 nike

我正在为一个返回 Boolean 值作为“0”和“1”的服务器编写 JSON 客户端。当我尝试运行我的 JSON 客户端时,我目前收到以下异常:

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; }
}

休息客户专线
注意 1: 这是使用 Spring 3.2
注 2: toJSONString() - 是一个辅助方法,它使用 Jackson 来序列化我的参数对象
注意 3: 异常发生在读入结果对象

DocInfoResponse result = restTemplate.getForObject(docInfoURI.toString()
+ "/?input={input}",
DocInfoResponse.class,
toJSONString(params));

最佳答案

正如 Paulo Pedroso 的回答提到和引用的那样,您需要滚动自己的自定义 JsonSerializerJsonDeserializer。创建后,您需要将 @JsonSerialize@JsonDeserialize 注释添加到您的属性;为每个指定要使用的类。

我在下面提供了一个小(希望是简单的)示例。序列化器和反序列化器的实现都不是 super 健壮的,但这应该可以帮助您入门。

public static class SimplePojo {

@JsonProperty
@JsonSerialize(using=NumericBooleanSerializer.class)
@JsonDeserialize(using=NumericBooleanDeserializer.class)
Boolean bool;
}

public static class NumericBooleanSerializer extends JsonSerializer<Boolean> {

@Override
public void serialize(Boolean bool, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException {
generator.writeString(bool ? "1" : "0");
}
}

public static class NumericBooleanDeserializer extends JsonDeserializer<Boolean> {

@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\"}"));
}

关于java - 如何将 FasterXML\Jackson 中的 boolean 值序列化/反序列化为 Int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34297506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com