fastjson常用场景

fastjson使用说明

1. 无构造函数序列化

在反序列化时需要使用构造函数,如果只有一个有参的构造函数会导致属性返写不全的问题。因此需要一个无参的构造函数。
可以通过 lombok的注解添加 @NoArgsConstructor

构造函数说明
2. 序列化默认规则

数字默认填充0,
字符串默认""
对象null 默认不参与序列化

3. 注解JSONField

在一个java 对象中部分字段不参与序列化

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(deserialize = false, serialize = false)  //不参与序列化,不参与反序列化
private int name;

序列化与反序列名字建立隐射

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(name = "task_id")  //
private int id;

格式时间

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(format = "yyyyMMdd HH:mm:ss")  //
private Date date;

序列化时操作,比如不填充默认值

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(serialzeFeatures = SerializerFeature.NotWriteDefaultValue)  //
private int id;

JSONField注解SerializerFeature的属性,是用来做序列化时,一系列默认值填充的操作,具体可以直接查看https://blog.csdn.net/u010246789/article/details/52539576

Feature反序列化操作,比如字段排序

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.Feature;

JSONObject reqJSON = JSONObject.parseObject(body, Feature.OrderedField);

@JSONField(Feature = Feature.OrderedField)
private int id;

JSONField注解SerializerFeature的属性,是用来做序列化时,一系列默认值填充的操作,具体可以直接查看https://blog.csdn.net/u010246789/article/details/52539576

添加默认值

import com.alibaba.fastjson.annotation.JSONField;
@JSONField(defaultValue = 1)
private int id;

别名功能,label,通过别名getTaskId能获取到id,也能通过getId获取到id;

import com.alibaba.fastjson.annotation.JSONField;
@JSONField(label = "taskId")
private int id;

unwrapped 将对象打平成一维,默认是false 不打平

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONPOJOBuilder;

@JSONField(unwrapped = false)
private int id;

保留对象中的空值SerializerFeature.WriteMapNullValue

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONPOJOBuilder;

@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)
private int id;

保留list空数组SerializerFeature.WriteNullListAsEmpty

@JSONField(serialzeFeatures = SerializerFeature.WriteNullListAsEmpty)
private List<String> friendNames;
4. 常用转换
import com.alibaba.fastjson.JSON;//转数组
import com.alibaba.fastjson.JSONObject;

List<Student> students = JSON.parseArray(json, Student.class);

//转java 对象
Student student = JSON.parseObject(json, Student.class);

//装JSONObject
JSONObject object = JSON.parseObject(json);
Object a = object.get("asd");
5.场景报错解决
1. 最常见问题

无 无参数的构造函数

2. get方法序列化问题
com.alibaba.fastjson.JSONException: write javaBean error, fastjson version 1.2.83, class ranger.policy.RangerHivePolicy, method : getEliminatedPolicy

序列化成字符串时,会查询所有get开头的方法,如果有get方法没有字段时会报错。因此被序列化的对象尽量不要有get开头的工作方法。同理在反序列化时也不能有set开头的工作方法。
在序列化字符串时,可以通过 SerializerFeature.IgnoreNonFieldGetter解决
demo

String policyJson = JSONObject.toJSONString(policy, SerializerFeature.IgnoreNonFieldGetter);
3. 数组反序列化空值问题

如果是空数组,在反序列化时会反成数组,如果是null,在fastjson 1版本会直接null,要注意判断该值

String tmp = "{\"age\":1,\"friendNames\":[],\"test\":null}";
FastJsonTest1 fastJsonTest1 = JSON.parseObject(json);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容