Fastjson和jackson的序列化小知识

在使用Fastjson和Jackson对对象进行序列化时,针对对象的get方法进行序列化(即使没有显示声明属性),不同的工具处理方式也有所不同,比如会处理掉大小写,下划线,数字,排序等等,如图:

public class TestClass {

    public String get2A(){
        return "";
    }

    public String get1B(){
        return "";
    }

    public String Get222(){
        return "";
    }

    public String get333(){
        return "";
    }

    public String get_213(){
        return "";
    }

    public String getZ_213(){
        return "";
    }

    public String getMad(){
        return "";
    }

    public String getMad_Sad(){
        return "";
    }

    public String getMad_sad(){
        return "";
    }

    public String getMadSad(){
        return "";
    }

    public String get_sad(){
        return "";
    }

}

处理代码:

JsonMapper来自于Jackson

JSON来自于Fastjson

System.out.println(JsonMapper.simpleMapper().toJson(new TestClass()));
System.out.println(JSON.toJSONString(new TestClass()));

返回结果

{"1B":"","2A":"","333":"","mad":"","mad_Sad":"","mad_sad":"","madSad":"","_sad":"","_213":"","z_213":""}
{"1B":"","213":"","2A":"","mad":"","madSad":"","mad_Sad":"","mad_sad":"","sad":"","z_213":""}

总结 :

1.只有get开头的方法会被序列化(如:Get222()未参与序列化)

2.Jackson和Fastjson都会对结果进行排序,排序规则略有不同

3.FastJson会忽略纯数字(如get333()),以下划线开头会去掉下划线(如get_sad())

猜你喜欢

转载自blog.csdn.net/guangtaqiu2745/article/details/85159390