alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

安卓环境运行解析枚举失败 #3402

Open HsXuTao opened 4 years ago

HsXuTao commented 4 years ago

环境:fastjson:1.1.72.android 安卓10

代码如下

    List<EnumPerformanceTest> list = new ArrayList<>();
    list.add(EnumPerformanceTest.Performance1);
    list.add(EnumPerformanceTest.Performance2);
    String str = JSON.toJSONString(list);

    List<EnumPerformanceTest> list2 = JSON.parseArray(str, EnumPerformanceTest.class);

    UtilLog.logE("list1", list);
    UtilLog.logE("list2", list2);

当一个枚举类EnumPerformanceTest重写toString方法的时候,list2中的值为[null,null]

去掉自己重写的toString方法,或者使用 fastjson:1.1.73(非安卓版本的库),则不会有该问题

   public enum EnumPerformanceTest implements Serializable {
       Performance1("MPOS装机机具数", "台"),
       Performance2("MPOS押金版激活达标数", "台"),
       ;

       private String name;
       private String unit;

       EnumPerformanceTest(String name, String unit) {
           this.name = name;
           this.unit = unit;
       }

       public String getName() {
           return name;
       }

       public void setName(String name) {
           this.name = name;
       }

       public String getUnit() {
           return unit;
       }

       public void setUnit(String unit) {
           this.unit = unit;
       }

       @Override
       public String toString() {
           return "EnumPerformanceTest{" +
                   "name='" + name + '\'' +
                   ", unit='" + unit + '\'' +
                   '}';
       }
   }
ZivYan commented 4 years ago

确实是 BUG,这个已提 PR