fastjson

2020-07-18  本文已影响0人  张明学

fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。fastjson地址

引用

<!-- fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
</dependency>

下面例子中所使用的对象:

@Data
public class EmployeeVo implements Serializable {
    private Long id;
    private String name;
    private Short sex;
    private Date birthday;
    private String telephone;
    private BigDecimal salary;
    private Boolean isOfficial;
    private String hobby;

    public String getAbc() {
        return "abc方法";
    }
}

默认序列化(JSON.toJSONString)

    @Test
    public void serializeTest() {
        EmployeeVo employeeVo = new EmployeeVo();
        employeeVo.setId(1000L)
                .setName("haha")
                .setBirthday(new Date())
                .setSalary(new BigDecimal("19865.32"))
                .setIsOfficial(true)
                .setSex(new Short("0"))
                .setTelephone("021-2546589");
        String jsonStr = JSON.toJSONString(employeeVo);
        log.info(jsonStr);
    }

结果:

{"abc":"abc方法","birthday":1595037120123,"id":1000,"isOfficial":true,"name":"haha","salary":19865.32,"sex":0,"telephone":"021-2546589"}

默认反序列化(JSON.parseObject)

    @Test
    public void deserializeTest() {
        String jsonStr = "{\"abc\":\"abc方法\",\"birthday\":1592872441001,\"id\":1000,\"isOfficial\":true,\"name\":\"haha\",\"salary\":19865.32,\"sex\":0,\"telephone\":\"021-2546589\"}";
        EmployeeVo employeeVo = JSON.parseObject(jsonStr, EmployeeVo.class);
        log.info(employeeVo.toString());
    }

结果:EmployeeVo(id=1000, name=haha, sex=0, birthday=Tue Jun 23 08:34:01 CST 2020, telephone=021-2546589, salary=19865.32, isOfficial=true, hobby=null)


序列化个性化设置(SerializerFeature)

API接口:(JSON.toJSONString)

public static String toJSONString(Object object, SerializerFeature... features) {
     return toJSONString(object, DEFAULT_GENERATE_FEATURE, features);
}

常用的SerializerFeature在jar包中com.alibaba.fastjson.serializer.SerializerFeature枚举中都有定义,常用的有如配置:

序列化过滤器设置(SerializeFilter)

SerializeFilter可以序列化前和后做一些定制,如PropertyFilter,PropertyPreFilter,BeforeFilter,AfterFilter,LabelFilter,NameFilter,ValueFilter

使用:

    @Test
    public void propertyFilterTest() {
        EmployeeVo employeeVo = new EmployeeVo();
        employeeVo.setId(1000L).setName("haha").setBirthday(new Date()).setSalary(new BigDecimal("19865.32")).setIsOfficial(true).setSex(new Short("0")).setTelephone("021-2546589");
        SamplePropertyFilter samplePropertyFilter = new SamplePropertyFilter();
        SampleAfterFilter sampleAfterFilter = new SampleAfterFilter();
        String jsonStr1 = JSON.toJSONString(employeeVo, new SerializeFilter[]{samplePropertyFilter, sampleAfterFilter});
        // {"abc":"abc方法","birthday":1592873561942,"id":1000,"isOfficial":true,"name":"haha","salary":19865.32,"sex":0,"telephone":"021-2546589"}
        log.info(jsonStr1);
    }

自定义序列化、反序列化及@JSONField和@JSONType


泛型反序列化(TypeReference)

泛型反序列化是指反序列化结果的对象是一个泛型,如ResponseVO<BaseVo>

public class ResponseVO<T extends BaseVo> {
    private int resultCode;
    private BaseVo resultData;
}

public class EmployeeVo extends BaseVo {
    // ...
}

如果对:{"resultCode":100,"resultData":{"abc":"abc方法","birthday":1595040655594,"id":1000,"isOfficial":true,"name":"haha","salary":19865.32,"sex":0,"telephone":"021-2546589"}} JSON串反序列化成ResponseVO<EmployeeVo>对象则需要:

ResponseVO<EmployeeVo> response = JSON.parseObject(jsonString, new TypeReference<ResponseVO<EmployeeVo>>(){});

综合API及相关概念

最全的JSON.toJSONString()方法:

public static String toJSONString(Object object, // 对象
                                  SerializeConfig config, // 配置并记录每种Java类型对应的序列化类
                                  SerializeFilter[] filters, // 序列化过滤器
                                  String dateFormat, // 日期的格式化
                                  int defaultFeatures, // 默认的格式化设置
                                  SerializerFeature... features) {// 对于对输出的json做各种格式化设置
    // ...
}
public static <T> T parseObject(String input, //json串
                                Type clazz, //对象clazz
                                ParserConfig config, //保存各种ObjectDeserializer
                                ParseProcess processor,//定制反序列化,类似于SerializeFilter
                                int featureValues,
                                Feature... features) {//反序列化的特性
    // ...
}
上一篇 下一篇

猜你喜欢

热点阅读