JackSon序列化和反序列化枚举类型

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public enum TimeRangeEnum {
    Today("今天", "today"), Yesterday("昨天", "yesterday"),
    ThisWeek("本周", "thisWeek"), LastWeek("上周", "lastWeek"),
    ThisMonth("本月", "thisMonth"), LastMonth("上月", "lastMonth"),
    ThisSeason("本季", "thisSeason"), LastSeason("上季", "lastSeason"),
    ThisYear("本年", "thisYear"), LastYear("上年", "lastYear");

    TimeRangeEnum(String name, String value) {
        this.name = name;
        this.value = value;
    }

    @ApiModelProperty("日期范围中文名")
    @JsonProperty
    private final String name;
    @ApiModelProperty("日期范围搜索值")
    @JsonProperty
    private final String value;

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    /**
     * 两个参数,反序列时JackSon框架自动根据方法参数进行自动调用。需要与序列化后的json格式进行一一对应。
     */
    @JsonCreator
    public static TimeRangeEnum from(String name, String value) {
        for (TimeRangeEnum timeRangeEnum : values()) {
            if (timeRangeEnum.name.equals(name) && timeRangeEnum.value.equals(value)) {
                return timeRangeEnum;
            }
        }
        return null;
    }

    /**
     * 获得所有枚举类型到list
     */
    public static List<TimeRangeEnum> getAllToList() {
        List<TimeRangeEnum> list = new ArrayList<>();
        TimeRangeEnum[] values = values();
        Collections.addAll(list, values);
        return list;
    }

    @Configuration
    static class TimeRangeConfig {
        @Bean
        public Jackson2ObjectMapperBuilderCustomizer timeRangeEnumCustomizer() {
            return jacksonObjectMapperBuilder -> jacksonObjectMapperBuilder.serializerByType(TimeRangeEnum.class, new JsonSerializer<TimeRangeEnum>() {
                @Override
                public void serialize(TimeRangeEnum timeRangeEnum, JsonGenerator gen, SerializerProvider serializers) throws IOException {
                    gen.writeStartObject();
                    gen.writeStringField("name", timeRangeEnum.getName());
                    gen.writeStringField("value", timeRangeEnum.getValue());
                    gen.writeEndObject();
                }
            });
        }
    }
}

测试效果

序列化
[
        {
            "name": "今天",
            "value": "today"
        },
        {
            "name": "昨天",
            "value": "yesterday"
        },
        {
            "name": "本周",
            "value": "thisWeek"
        },
        {
            "name": "上周",
            "value": "lastWeek"
        },
        {
            "name": "本月",
            "value": "thisMonth"
        },
        {
            "name": "上月",
            "value": "lastMonth"
        },
        {
            "name": "本季",
            "value": "thisSeason"
        },
        {
            "name": "上季",
            "value": "lastSeason"
        },
        {
            "name": "本年",
            "value": "thisYear"
        },
        {
            "name": "上年",
            "value": "lastYear"
        }
    ]
反序列化

JackSon序列化和反序列化枚举类型_第1张图片
JackSon序列化和反序列化枚举类型_第2张图片

你可能感兴趣的:(java,spring,boot,spring)