一个经典的fastjson反序列化问题记录

最近碰到一个问题,因为一些干扰因素导致排查的时候走了一段歧路,
报错信息是

1
Exception in thread "main" com.alibaba.fastjson.JSONException: scan null error

用一个简单的demo来复现下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public enum DemoEnum {

DEMO1("demo1", "desc");
private String code;

private String desc;

DemoEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public static DemoEnum getByCode(String code) {
for (DemoEnum demoEnum : values()) {
if (demoEnum.code.equals(code)) {
return demoEnum;
}
}
return null;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}
}

先定义一个枚举类,然后有个demo类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class FastJsonDemo implements Serializable {

private static final long serialVersionUID = 1131767138182111892L;
private String name;

private Map<DemoEnum, String> map = new HashMap<>();

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Map<DemoEnum, String> getMap() {
return map;
}

public void setMap(Map<DemoEnum, String> map) {
this.map = map;
}
}

其中map的key是上面定义的枚举类,然后在main方法中做一下序列化和反序列化

1
2
3
4
5
6
7
8
9
10
11
public class demo {

public static void main(String[] args) {
FastJsonDemo fastJsonDemo = new FastJsonDemo();
fastJsonDemo.setName("nick");
Map<DemoEnum, String> map = new HashMap<>();
map.put(DemoEnum.getByCode("code"), "null key value");
fastJsonDemo.setMap(map);
FastJsonDemo decodeFastJsonDemo = JSONObject.parseObject(JSONObject.toJSONString(fastJsonDemo), FastJsonDemo.class);
}
}

这样就会出现这个异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Exception in thread "main" com.alibaba.fastjson.JSONException: scan null error
at com.alibaba.fastjson.parser.JSONLexerBase.scanNullOrNew(JSONLexerBase.java:4531)
at com.alibaba.fastjson.parser.JSONLexerBase.nextToken(JSONLexerBase.java:154)
at com.alibaba.fastjson.parser.JSONLexerBase.nextToken(JSONLexerBase.java:358)
at com.alibaba.fastjson.parser.deserializer.MapDeserializer.parseMap(MapDeserializer.java:227)
at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:61)
at com.alibaba.fastjson.parser.deserializer.MapDeserializer.deserialze(MapDeserializer.java:41)
at com.alibaba.fastjson.parser.deserializer.FastjsonASMDeserializer_1_FastJsonDemo.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:269)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:671)
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 fastjson.demo.main(demo.java:23)

这个异常有两个原因,第一当然是在map中出现了null作为key的数据,第二就是这个key是复杂对象
在反序列化以后就会出现这个异常,因为最初出现这个异常是因为我改了另一个字符串字段,并且会反序列化成json就让我判断出现了误差
这边做一个记录。