All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.chobit.commons.json.LocalDateDeserializer Maven / Gradle / Ivy

The newest version!
package org.chobit.commons.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.chobit.commons.utils.LocalDateKit;

import java.io.IOException;
import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;

/**
 * @author robin
 */
public class LocalDateDeserializer extends JsonDeserializer {


    private static final int LEN_LOCAL_DATE_ARR = 3;


    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctx) throws IOException {
        if (p.getCurrentToken() == JsonToken.START_ARRAY) {
            return deserializeFromArray(p);
        }
        return LocalDateKit.parseDate(p.getText());
    }


    private LocalDate deserializeFromArray(JsonParser p) throws IOException {
        List arr = new LinkedList<>();
        while (true) {
            JsonToken t = p.nextToken();
            switch (t) {
                case VALUE_NUMBER_INT:
                    arr.add(p.getIntValue());
                    break;
                case END_ARRAY:
                    return localDateOf(arr);
                default:
                    return null;
            }
        }
    }


    /**
     * 根据数组生成LocalDate实例
     *
     * @param arr 数组
     * @return LocalDate实例
     */
    private LocalDate localDateOf(List arr) {
        if (arr.size() < LEN_LOCAL_DATE_ARR) {
            return null;
        }
        return LocalDate.of(arr.get(0), arr.get(1), arr.get(2));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy