fastjson反序列化异常解决⽅案(内部类反序列化失败问
题)java.lang.Ille。。。
⽬录
记录⼀次在使⽤FastJson反序列化内部类时遇到的问题。
错误信息如下:
com.alibaba.fastjson.JSONException: create instance error, st.json.v0.Parent$Daughter
at com.alibaba.fastjson.parser.ateInstance(JavaBeanDeserializer.java:276)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:845)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:300)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:296)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:857)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:300)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:296)
at com.alibaba.fastjson.parser.deserializer.DefaultFieldDeserializer.parseField(DefaultFieldDeserializer.java:86)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:857)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:300)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:698)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:395)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:299)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:572)
st.json.stDeserialize(JSONTest.java:37)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
wInstance0(Native Method)
wInstance(NativeConstructorAccessorImpl.java:62)
wInstance(DelegatingConstructorAccessorImpl.java:45)
at wInstance(Constructor.java:423)
at com.alibaba.fastjson.parser.ateInstance(JavaBeanDeserializer.java:271)
... 42 more
导致如上错误的代码如下:error parse new
import com.alibaba.fastjson.annotation.JSONField; import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Parent {
private String name;
private int age;
private Son son;
@Getter
@Setter
public class Son {
private String name;
private int height;
private Daughter daughter;
}
@Getter
@Setter
public class Daughter {
@JSONField(name = "name")
private String name;
}
}
测试代码如下:
import com.alibaba.fastjson.JSON;
import org.junit.Test;
public class JSONTest {
@Test
public void testSerialize() {
Parent p = new Parent();
p.setName("Tom");
p.setAge(120);
Parent.Son son = new Parent().new Son();
son.setName("Jerry");
son.setHeight(180);
p.setSon(son);
Parent.Daughter daughter = new Parent().new Daughter();
daughter.setName("Lisa");
daughter.setHobby("dancing");
son.setDaughter(daughter);
String person = JSONString(p);
System.out.println(person);
}
/**
* 该⽅法会出现异常
*/
@Test
public void testDeserialize() {
System.out.println("--- testDeserialize ---");
String json = "{\"age\":120,\"name\":\"Tom\",\"son\":{\"daughter\":{\"name\":\"dancing\"},\"height\":180,\"name\":\"Jerry\"}}";        Parent person = JSON.parseObject(json, Parent.class);
System.out.JSONString(person));
}
}
⽅案⼀:将内部类写成静态内部类,如下:
测试结果:
⽅案⼆:将内部类定义挪到外⾯定义(或直接定义成public的独⽴的java⽂件)
测试结果:
⽅案三:将内部类继续往⾥嵌套

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。