阿里巴巴fastjson @JSONField 应用

    技术2022-07-11  106

    依赖

    <dependency> ​ <groupId>com.alibaba</groupId> ​ <artifactId>fastjson</artifactId> ​ <version>1.2.47</version> </dependency>

    用法

    name : 给字段起别名

    ordinal :用来排序 输出json字符串中key属性的先后顺序 可以用 @JSONType (orders={})来代替

    format : 用来日期格式化

    serialize :指定字段不序列化

    deserialize :指定字段不序列化

    示例

    @Data public class WqTestEntity { @JSONField(name = "test_int") private int testInt; @JSONField(name = "test_double") private double testDouble; @JSONField(name = "test_string") private String testString; @JSONField(name = "test_Serializable", deserialize = false) private String testSerializable; @JSONField(format = "yyyyMMdd", name = "test_date") public Date testDate; }

     执行

    public static void main(String[] args) { WqTestEntity wqEntity = new WqTestEntity(); wqEntity.setTestInt(1); wqEntity.setTestString("aaa"); wqEntity.setTestDouble(1.01); wqEntity.setTestDate(new Date()); wqEntity.setTestSerializable("111"); System.out.println(JSON.toJSONString(wqEntity)); }

    结果

    {"test_date":"20200701","test_double":1.01,"test_int":1,"test_string":"aaa"}  

    Processed: 0.015, SQL: 10