zoukankan      html  css  js  c++  java
  • FastJson序列化枚举类

    场景说明

    比如我们想打印返回参数的具体情况,如果里面有枚举类型,打印的参数不完整。

    @Getter
    @Setter
    @ToString
    public class ResultResponse {
    
        private String name;
    
        private Integer age;
    
        private ResultEnum resultEnum;
    
    }
    
    public enum ResultEnum {
    
        SUCCESS("0000", "成功"),
        FAIL("0001", "失败"),
        EXIST("0002", "已存在"),
        SYS_ERROR("9999", "系统异常");
        private String code;
        private String msg;
    
        ResultEnum(String code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
    }
    

    默认序列化方式

    public class MainTest {
    
        public static void main(String[] args) {
            ResultResponse resultResponse = new ResultResponse();
            resultResponse.setAge(11);
            resultResponse.setName("张三");
            resultResponse.setResultEnum(ResultEnum.SUCCESS);
            System.out.println(JSONObject.toJSONString(resultResponse));
        }
    
    }
    

    运行结果:{"age":11,"name":"张三","resultEnum":"SUCCESS"},并不是我想要的结果,枚举类型属性没打印出来

    设置枚举序列化

    public class MainTest {
    
        public static void main(String[] args) {
            ResultResponse resultResponse = new ResultResponse();
            resultResponse.setAge(11);
            resultResponse.setName("张三");
            resultResponse.setResultEnum(ResultEnum.SUCCESS);
    
            SerializeConfig config = new SerializeConfig();
            config.configEnumAsJavaBean(ResultEnum.class);
    
            System.out.println(JSONObject.toJSONString(resultResponse, config));
        }
    
    }
    

    运行结果:{"age":11,"name":"张三","resultEnum":{"code":"0000","msg":"成功"}}

  • 相关阅读:
    资源链接
    Silverlight4 RIA应用开发
    社会心理学(第8版)
    C# 2.0 锐利体验
    C# 3.0 锐利体验
    C#面向对象设计模式纵横谈
    ASP.NET MVC2程序开发入门到精通
    Silverlight数独游戏
    StringBuilder研究和探索
    .NET 开发从入门到精通
  • 原文地址:https://www.cnblogs.com/zhangjianbing/p/15517925.html
Copyright © 2011-2022 走看看