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())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值