fastjson序列化和反序列化理解

json序列化直接根据get 和set方法进行序列化
json反序列化则有以下几个步骤
  1. 没有构造函数的类,java中有默认无参构造函数,此时反序列化会根据get set方法的名称进行属性赋值,使用没问题
  2. 当类中存在一个自定义有参构造函数的时候,此时java默认无参构造函数就没有了,此时反序列化是以有参构造函数的参数名称进行注入的,与类型无关
  3. 当类中存在多个自定义有参构造函数的时候,此时反序列化将以参数长的为优先进行反序列化
  4. 当类中存在多个自定义有参构造函数,且参数个数一致,参数类型不一致,此时反序列化将以构造函数的先后顺序进行
针对以上四种情况,以代码举例如下:
当只有一个有参构造函数的时候
private String name;
private String education;
public Student(String name) {
   this.name = name;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}
@Override
public String toString() {
    return 
    "Student{" +
    " name='" + name + '\'' +
    ", education='" + education + '\'' +
    '}';
}
测试类如下:
Student student = new Student("张三");
student.setEducation("大学");
String jsonStr = JSON.toJSONString(student);
Student student1 = JSON.parseObject(jsonStr,Student.class);
System.out.println(student1);
输出结果如下,尽管设置了education的值,但是反序列化的时候,由于构造方法里没有这个字段,所以也不会赋值:
{"name":"张三","education":"大学"}
Student{ name='张三',education='null'}
当同时存在一个无参构造函数和一个有参构造函数的时候
private String name;
private String education;
public Student(String name) {
   this.name = name;
}
public Student() {
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}
@Override
public String toString() {
    return 
    "Student{" +
    " name='" + name + '\'' +
    ", education='" + education + '\'' +
    '}';
}
测试类如下:
Student student = new Student("张三");
student.setEducation("大学");
String jsonStr = JSON.toJSONString(student);
Student student1 = JSON.parseObject(jsonStr,Student.class);
System.out.println(student1);
输出结果如下,两个字段数据都没有丢失:
{"name":"张三","education":"大学"}
Student{name='张三', education='大学'}
当存在多个构造函数的时候
private String name;
private String education;
public Student(String name) {
   this.name = name;
}
public Student(String name, String education) {
    this.name = name;
    this.education = education;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}
@Override
public String toString() {
    return 
    "Student{" +
    " name='" + name + '\'' +
    ", education='" + education + '\'' +
    '}';
}
测试类如下:
Student student = new Student("张三");
student.setEducation("大学");
String jsonStr = JSON.toJSONString(student);
Student student1 = JSON.parseObject(jsonStr,Student.class);
System.out.println(student1);
输出结果如下,由于存在一个更长的构造方法,以更长的为准,两个字段数据都不会丢失:
{"name":"张三","education":"大学"}
Student{name='张三', education='大学'}
当存在多个相同个数的构造函数时
private String name;
private String education;
public Student(int name) {
    this.name = "name";
}
public Student(String name) {
    this.name = name;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEducation() {
    return education;
}
public void setEducation(String education) {
    this.education = education;
}
@Override
public String toString() {
    return 
    "Student{" +
    " name='" + name + '\'' +
    ", education='" + education + '\'' +
    '}';
}
测试类如下:
Student student = new Student("张三");
student.setEducation("大学");
String jsonStr = JSON.toJSONString(student);
Student student1 = JSON.parseObject(jsonStr,Student.class);
System.out.println(student1);
输出结果如下,由于整型在前,字符串的构造函数在后,优先以在前的函数进行反序列化,就会报错:
Exception in thread "main" com.alibaba.fastjson.JSONException: parseInt error, field : name
    at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:99)
    at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.parseField(JavaBeanDeserializer.java:1078)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:773)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:271)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:267)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:661)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
    at Main.main(Main.java:39)
Caused by: java.lang.NumberFormatException: For input string: "张三"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.alibaba.fastjson.util.TypeUtils.castToInt(TypeUtils.java:588)
    at com.alibaba.fastjson.serializer.IntegerCodec.deserialze(IntegerCodec.java:95)
    ... 10 more

你可能感兴趣的:(java,java)