fastjson反序列化,内部Set结构异常
@wenshao 你好,想跟你请教个问题:fastjson1.2.7,反序列化错误,测试代码如下:
public class FastJsonTest {
@Test
public void jsonTest() {
try {
//FJStruct是一个简单的bean结构,三个私有属性:paramInt,paramString,paramSet,默认构造函数,标准属性访问器get,set
FJStruct myObj = new FJStruct();
myObj.setParamInt(1);
myObj.setParamString("fastjson");
Set<Long> paramSet = new HashSet<Long>();
paramSet.add(1L);
paramSet.add(2L);
myObj.setParamSet(paramSet);
//fastjson序列化,版本1.2.7
String jsonStr = JSON.toJSONString(myObj, SerializerFeature.WriteClassName, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.SkipTransientField, SerializerFeature.SortField);
// 加上以下注释的代码就能正常反序列化出来
// if (jsonStr.contains(":Set[")) {
// jsonStr = jsonStr.replace(":Set[", ":[");
// }
System.out.println(jsonStr);
FJStruct cloneObj = JSON.parseObject(jsonStr, FJStruct.class);
System.out.println(cloneObj);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------------
错误堆栈打印如下:
com.alibaba.fastjson.JSONException: syntax error, expect {, actual Set, pos 53
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.deserialze(JavaBeanDeserializer.java:232)
at com.alibaba.fastjson.parser.deserializer.ASMJavaBeanDeserializer.parseRest(ASMJavaBeanDeserializer.java:100)
at Fastjson_ASM_FJStruct_1.deserialze(Unknown Source)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:551)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:251)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:186)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:304)
at testcode.FastJsonTest.jsonTest(FastJsonTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
去掉
SerializerFeature.WriteClassName,就不会生成Set了