zoukankan      html  css  js  c++  java
  • 基于 枚举值 输出 枚举描述的 jackson 自定义注解方法

    @Documented
    @JacksonAnnotationsInside
    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    @JsonSerialize(using = EnumShowSerializer.class)
    public @interface JacksonEnumShow {
        /**
         * 要转换成的枚举
         *
         * @return
         */
        Class<? extends Enum<?>> using();
    
        /**
         * 是否复写本身
         *
         * @return
         */
        boolean self() default false;
    
        /**
         * 如果不复写本身新增字段是本身字段的增加的后缀
         * @return
         */
        String suffix() default "Text";
    }
    
    
    
    public class EnumShowSerializer extends JsonSerializer<Integer> implements ContextualSerializer {
    
        private JacksonEnumShow enumShow;
    
        public EnumShowSerializer() {
    
        }
    
        public EnumShowSerializer(final JacksonEnumShow enumShow) {
            this.enumShow = enumShow;
        }
    
    
        @SneakyThrows
        @Override
        public void serialize(Integer integer, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    
            if (null == integer || null == enumShow) {
                jsonGenerator.writeObject(integer);
                return;
            }
            String fieldName = jsonGenerator.getOutputContext().getCurrentName();
            Class<?>[] interfaces = enumShow.using().getInterfaces();
            String theMsg = null;
            if (interfaces.length > 0) {
    
                Class<?> baseInterface = interfaces[0];
                if (baseInterface.equals(BuzEnum.class)) {
                    Class newIns = enumShow.using();
                    Optional<? extends BuzEnum<?>> enumOpt = BuzEnum.of(newIns, integer);
                    if (enumOpt.isPresent()) {
                        theMsg = enumOpt.get().getValue();
                    }
                }
                if (baseInterface.equals(AuthBaseEnum.class)) {
                    Class newIns = enumShow.using();
                    Optional<? extends AuthBaseEnum<?>> enumOpt = AuthBaseEnum.of(newIns, integer);
                    if (enumOpt.isPresent()) {
                        theMsg = enumOpt.get().getValue();
                    }
                }
    
            } else {
                if (enumShow.using().isEnum()) {
                    Method codeMethod = enumShow.using().getDeclaredMethod("getCode");
                    Method descMethod = enumShow.using().getDeclaredMethod("getDesc");
                    for (Object obj : enumShow.using().getEnumConstants()) {
                        if (integer.equals(codeMethod.invoke(obj))) {
                            theMsg = descMethod.invoke(obj).toString();
                        }
                    }
                }
            }
            if (enumShow.self()) {
                jsonGenerator.writeString(theMsg);
            } else {
                jsonGenerator.writeNumber(integer);
                jsonGenerator.writeStringField(fieldName + enumShow.suffix(), theMsg);
            }
    
    
        }
    
        @Override
        public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
    
            JacksonEnumShow annotation = beanProperty.getAnnotation(JacksonEnumShow.class);
            if (annotation != null) {
                return new EnumShowSerializer(annotation);
            }
            return this;
        }
    }
    
    
    

    使用方式

    
        @Data
        @Builder
        public class TheClass {
            @JacksonEnumShow(using = FlagEnum.class, self = true)
            private Integer flag;
    
            @JacksonEnumShow(using = DictFromEnum.class)
            private Integer dict;
        }
    

    输出的json

    {
    	"flag":"是",
    	"dict":1,
    	"dictText":"字典"
    }
    
  • 相关阅读:
    HDU5732 Subway【树重心 树哈希】
    HDU6311 Cover【欧拉路径 | 回路】
    HDU6370 Werewolf 【基环内向树】
    HDU6321 Dynamic Graph Matching【状压DP 子集枚举】
    HDU6331 Problem M. Walking Plan【Floyd + 矩阵 + 分块】
    HDU6403 Card Game【基环树 + 树形DP】
    HDU5691 Sitting in Line【状压DP】
    Codeforces Round #650 (Div. 3)
    2017-2018 ACM-ICPC, NEERC, Northern Subregional Contest
    Codeforces Round #649 (Div. 2)
  • 原文地址:https://www.cnblogs.com/akashicbrother/p/15439147.html
Copyright © 2011-2022 走看看