如何在Jackson中自定义从数组反序列化?

j9per5c4  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(175)

我想将日期序列化/反序列化(Joda)为数组。
不幸的是,我不知道如何执行反序列化部分:

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import java.io.IOException;

public class TryJodaTime {

   public static class LocalTimeSerializer extends JsonSerializer<LocalTime> {

      @Override
      public void serialize(LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {

         gen.writeStartArray();
         gen.writeNumber(value.getHourOfDay());
         gen.writeNumber(value.getMinuteOfHour());
         gen.writeNumber(value.getSecondOfMinute());
         gen.writeNumber(value.getMillisOfSecond());

         gen.writeEndArray();

      }
   }

   public static class LocalTimeDeserializer extends JsonDeserializer<LocalTime> {

      @Override
      public LocalTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {

         JsonToken token;

         token = jp.nextToken();
         if( token != JsonToken.START_ARRAY ) {
            throw new JsonParseException("Start array expected", jp.getCurrentLocation());
         }

         int hour = jp.nextIntValue(0);
         int minute = jp.nextIntValue(0);
         int second = jp.nextIntValue(0);
         int ms = jp.nextIntValue(0);

         token = jp.nextToken();
         if( token != JsonToken.END_ARRAY ) {
            throw new JsonParseException("End array expected", jp.getCurrentLocation());
         }

         return new LocalTime(hour, minute, second, ms);
      }
   }

   public static class MyClass {

      private LocalTime localTime;

      public MyClass() {
         localTime = LocalTime.now();
      }

      @JsonSerialize(using = LocalTimeSerializer.class)
      public LocalTime getLocalTime() {
         return localTime;
      }

      @JsonDeserialize(using = LocalTimeDeserializer.class)
      public void setLocalTime(LocalTime localTime) {
         this.localTime = localTime;
      }

   }

   public static void main(String[] args) throws IOException {

      MyClass myClass = new MyClass();
      ObjectMapper mapper = new ObjectMapper();
      String string = mapper.writeValueAsString(myClass);

      System.out.println(string);

      MyClass myClass2 = mapper.readValue(string, MyClass.class);

      System.out.println(myClass2.toString());

   }
}

这段代码会引发我自己的异常

throw new JsonParseException("Start array expected", jp.getCurrentLocation());
p1tboqfb

p1tboqfb1#

LocalTimeDeserializer#deserialize被调用的时候,Jackson已经移动到了数组标记处,换句话说,在deserialize中,JsonParser的当前标记就是数组标记。
您可以使用它来验证您正在反序列化您所期望的内容,然后继续进行反序列化:

if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
    throw new JsonParseException("Start array expected", jp.getCurrentLocation());
}

相关问题