[英]How to deserialize from JSON to LocalDate, while also using JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS?
I have enabled jackson-datatype-joda , but it's not working with the JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
. 我已启用jackson-datatype-joda ,但它不能使用JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
。
I have set up ObjectMapper as following: 我已经设置了ObjectMapper如下:
ObjectMapper jacksonObjectMapper = new ObjectMapper();
jacksonObjectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
jacksonObjectMapper.registerModule(new JodaModule());
When deserializing (using com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer ), I end up with the error: 反序列化时(使用com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer ),我最终得到错误:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
at [...]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Current token (VALUE_STRING) not numeric, can not use numeric value accessors
I've debugged the LocalDateDeserializer and found out, that it's expecting VALUE_NUMBER_INT - while the JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
makes the date having Strings. 我调试了LocalDateDeserializer并发现它期望VALUE_NUMBER_INT - 而JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
使日期具有字符串。
When I've turned off that Feature, everything was all right. 当我关闭该功能时,一切都很好。
Is there any possibility that this feature (or similar) will work with Joda Time? 这个功能(或类似的)有可能与Joda Time一起使用吗?
Joda
module uses com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer
for deserializing org.joda.time.LocalDate
objects. Joda
模块使用com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer
来反序列化org.joda.time.LocalDate
对象。 This class looks like this: 这个类看起来像这样:
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException
{
// [yyyy,mm,dd]
if (jp.isExpectedStartArrayToken()) {
jp.nextToken(); // VALUE_NUMBER_INT
int year = jp.getIntValue();
jp.nextToken(); // VALUE_NUMBER_INT
int month = jp.getIntValue();
jp.nextToken(); // VALUE_NUMBER_INT
int day = jp.getIntValue();
if (jp.nextToken() != JsonToken.END_ARRAY) {
throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDate ints");
}
return new LocalDate(year, month, day);
}
switch (jp.getCurrentToken()) {
case VALUE_NUMBER_INT:
return new LocalDate(jp.getLongValue());
case VALUE_STRING:
String str = jp.getText().trim();
if (str.length() == 0) { // [JACKSON-360]
return null;
}
return parser.parseLocalDate(str);
default:
}
throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Array, String or Number");
}
As you can see, if LocalDate
is array this deserializer expects ints in this array. 如您所见,如果LocalDate
是数组,则此反序列化器需要此数组中的整数。 You can write your custom deserializer and extend this implementation. 您可以编写自定义反序列化程序并扩展此实现。 See below example: 见下面的例子:
class LocalDateWithStringsDeserializer extends LocalDateDeserializer {
private static final long serialVersionUID = 1L;
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
// [yyyy,mm,dd] or ["yyyy","mm","dd"]
if (jp.isExpectedStartArrayToken()) {
return parseArray(jp, ctxt);
}
return super.deserialize(jp, ctxt);
}
private LocalDate parseArray(JsonParser jp, DeserializationContext ctxt)
throws JsonParseException, IOException {
int year = getNextValue(jp);
int month = getNextValue(jp);
int day = getNextValue(jp);
if (jp.nextToken() != JsonToken.END_ARRAY) {
throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDate ints");
}
return new LocalDate(year, month, day);
}
private int getNextValue(JsonParser jp) throws IOException, JsonParseException {
jp.nextToken();
return new Integer(jp.getText());
}
}
Now you can link above deserializer and your property: 现在您可以链接上面的反序列化器和您的属性:
@JsonDeserialize(using = LocalDateWithStringsDeserializer.class)
private LocalDate date;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.