使用注释

时间:2018-11-25 16:53:53

标签: java spring-boot jackson jackson-databind jsr310

我正在使用带有Java 8模块的Jackson将JSON文件转换为Java对象。但是,在将JSON数组转换为LocalDate []应用程序时,会引发异常。

如何使用注释将下面的JSON数组转换为LocalDate []?

JSON

{
    "skip": [
        "01/01/2019",
        "26/01/2019"
    ]
}

代码

@JsonFormat(pattern = "dd/MM/yyyy")
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate[] skip;

例外

com.fasterxml.jackson.databind.exc.MismatchedInputException: Unexpected token (VALUE_STRING) within Array, expected VALUE_NUMBER_INT
 at [Source: (ByteArrayInputStream); line: 25, column: 3] (through reference chain: com.saalamsaifi.springwfrlroster.model.Team["skip"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:110)
    at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:38)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3070)

3 个答案:

答案 0 :(得分:0)

我怀疑正在查看您的代码和json模型,它正在尝试使用为一个对象定义的解串器将其转换为数组。简单来说,您正在尝试将单个项目转换为无法解析的数组。您可以尝试使用LocalDate列表。像这样:

Column.getBytes(name)

您甚至可能需要根据日期序列化器创建自己的反序列化器。

答案 1 :(得分:0)

由于Animal的类型为 array ,因此skipLocalDateSerializerLocalDateDeserializer不能立即使用-它们被实现为期望直接值令牌而不是数组。

您可以实现自己的串行器/解串器。我实现的用于对示例进行反序列化的天真的反序列化器如下:

JsonFormat

然后我将字段注释更改为public class CustomLocalDateArrayDeserializer extends JsonDeserializer<LocalDate[]> { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); @Override public LocalDate[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { ArrayList<LocalDate> list = new ArrayList<>(); JsonToken currentToken = p.getCurrentToken(); if (currentToken != JsonToken.START_ARRAY) { throw new JsonMappingException(p, "Not an array!"); } currentToken = p.nextToken(); while (currentToken != JsonToken.END_ARRAY) { if (currentToken != JsonToken.VALUE_STRING) { throw new JsonMappingException(p, "Not a string element!"); } LocalDate localDate = LocalDate.parse(p.getValueAsString(), formatter); list.add(localDate); currentToken = p.nextToken(); } return list.toArray(new LocalDate[0]); } }

如果您认为值得这样做,则可以对其进行迭代和改进,使其阅读并尊重@JsonDeserialize(using = CustomLocalDateArrayDeserializer.class)注释,等等。

答案 2 :(得分:-1)

乍一看:您的json数组中实际上是json对象,还是显示的只是Strings?这应该是这样的: {     “跳过”:[         “ key1”:“ 01/01/2019”,         “ key2”:“ 26/01/2019”     ] }