首页  

循环引用导致fastjson序列化与反序列化StackOverflowError     所属分类 java 浏览量 3385
fastjson版本 1.2.55

class Data1{
		public String name = "data1";
		public Data2 data;
}

class Data2{
		public String name = "data2";
		public Data1 data;
}


循环依赖 

不加 SerializerFeature.DisableCircularReferenceDetect 
序列化可以成功,但是结果会带上 $ref
{"data":{"data":{"$ref":".."},"name":"data2"},"name":"data1"}

反序列化时 java.lang.StackOverflowError


加上 SerializerFeature.DisableCircularReferenceDetect 
序列化时就 StackOverflowError


具体测试代码

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonStackOverflowTest {
	

	public static void main(final String[] args) {
				
		Data1 data1 = new Data1();
		
		Data2 data2 = new Data2();
		
		data1.data = data2;
		data2.data = data1;
		
		String str = JSON.toJSONString(data1);
		System.out.println(str);
		
		// java.lang.StackOverflowError
		try{
		    Data1 tmp = JSON.parseObject(str,Data1.class);
		    System.out.println(tmp);
		}catch(Throwable e){
			System.out.println("JSON.parseObject error,"+e);
		}

		
		str = JSON.toJSONString(data1,true);
		System.out.println(str);
		
		//
		str = null;
		
		try{
		    str = JSON.toJSONString(data1, SerializerFeature.DisableCircularReferenceDetect);
		}catch(Throwable e){
			// java.lang.StackOverflowError
			System.out.println(e);
		}
		
		System.out.println(str);


	    str = JSON.toJSONString(data1, SerializerFeature.DisableCircularReferenceDetect);

		
	}
	
	
	private static class Data1{
		public String name = "data1";
		public Data2 data;
		
		@Override
		public String toString(){
			return "name="+name+",data="+data;
		}
	}
	
	private static class Data2{
		public String name = "data2";
		public Data1 data;
		
		@Override
		public String toString(){
			return "name="+name+",data="+data;
		}
		
	}

}


运行结果

{"data":{"data":{"$ref":".."},"name":"data2"},"name":"data1"}
JSON.parseObject error,java.lang.StackOverflowError
{
	"data":{
		"data":{"$ref":".."},
		"name":"data2"
	},
	"name":"data1"
}
java.lang.StackOverflowError
null
Exception in thread "main" java.lang.StackOverflowError
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.writeReference(JavaBeanSerializer.java:548)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:185)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:160)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:324)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:454)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:160)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:324)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:454)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:160)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:324)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:454)
	at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:160)
	at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:324)

上一篇     下一篇
prometheus指标类型

prometheus java 客户端使用

prometheus使用jmx_exporter监控jvm

go语言defer机制

简单的清理脚本实战

prometheus配置