简体   繁体   中英

How to custom deserialize from array in Jackson?

I would like to serialize / deserialize (Joda) date as an array.

Unfortunately, I can't figure out how to do deserializtion part:

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());


   }
}

This code causes my own exception thrown

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

By the time LocalTimeDeserializer#deserialize is called, Jackson has already moved to the array token. In other words, within deserialize , the JsonParser 's current token is the array token.

You can use that to validate that you're deserializing what you expect and then move on to the deserialization.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM