[BUG]反序列化报错 JSONException
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
通过fastjson2将数据序列化写到redis中,后续再读出来就报错,错误信息如下:
```
Caused by: com.alibaba.fastjson2.JSONException: illegal fieldName input:, offset 132, character :, line 1, column 132, fastjson-version 2.0.46 {"@type":"com.zzwtec.interfaces.ResultObject","code":"0","data":{"@type":"[com.zzwtec.interfaces.basedata.dto.DoorCardDTO","@value":[]},"msg":"success","requestId":"zzwandroid-tdHVEBu9b1VGPSaps29x3Dk2W3UxQnMm","success":true}
at com.alibaba.fastjson2.JSONReaderUTF8.readFieldNameHashCode(JSONReaderUTF8.java:2726)
at com.alibaba.fastjson2.reader.ORG_4_4_ResultObject.readObject(Unknown Source)
at com.alibaba.fastjson2.reader.ObjectReaderImplObject.readObject(ObjectReaderImplObject.java:129)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:806)
```
我是重写了序列化类,代码如下
```
package com.zzwtec.common.json;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;
import org.springframework.data.redis.serializer.SerializationException;
import java.lang.reflect.Type;
public class MyGenericFastJsonRedisSerializer extends GenericFastJsonRedisSerializer {
@Override
public byte[] serialize(Object object) throws SerializationException {
if (object == null) {
return new byte[0];
} else {
try {
JSONWriter.Context context = JSON.createWriteContext(SerializeConfig.global, JSON.DEFAULT_GENERATE_FEATURE, new SerializerFeature[]{SerializerFeature.WriteClassName});
context.config(JSONWriter.Feature.NotWriteNumberClassName);
try {
JSONWriter writer = JSONWriter.ofUTF8(context);
byte[] var11;
try {
if (object == null) {
writer.writeNull();
} else {
writer.setRootObject(object);
Class valueClass = object.getClass();
ObjectWriter objectWriter = context.getObjectWriter(valueClass, valueClass);
objectWriter.write(writer, object, (Object)null, (Type)null, 0L);
}
var11 = writer.getBytes();
} catch (Throwable var7) {
if (writer != null) {
try {
writer.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}
throw var7;
}
if (writer != null) {
writer.close();
}
return var11;
} catch (JSONException var8) {
Throwable cause = var8.getCause() != null ? var8.getCause() : var8;
throw new com.alibaba.fastjson.JSONException("toJSONBytes error", (Throwable)cause);
} catch (RuntimeException var9) {
throw new com.alibaba.fastjson.JSONException("toJSONBytes error", var9);
}
} catch (Exception var3) {
throw new SerializationException("Could not serialize: " + var3.getMessage(), var3);
}
}
}
}
```
### 环境信息
- OS信息: [e.g.:CentOS Linux 7 (Core)]
- JDK信息: [e.g.:Openjdk 1.8.0_252]
- 版本信息:[e.g.:Fastjson2 2.0.46]
```
### 期待的正确结果
能正常解析不报错
Contributor guide
Research direction
Start with the reported MyGenericFastJsonRedisSerializer implementation and the stack trace at JSONReaderUTF8.java:2726, then inspect how the serialized Redis value is passed to JSON.parseObject. Reproduce the failure using the shown ResultObject payload and fastjson2 2.0.46. Done means the serialized value can be read back without JSONException, with the required environment details and a regression test documenting the case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, redis
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100