import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import lombok.Data;
import java.io.IOException;
import java.util.HashMap;
public class Test {
public static void main(String[] args) throws IOException {
IndexData indexData = new IndexData();
String json = JSON.toJSONString(indexData);
//序列化后的结果 {"param1":{},"unit":{"param1":{"$ref":"$.param1"}}}
System.out.println(json);
indexData = JSON.parseObject(json, new TypeReference(){});
//反序列化的结果未反序列化完成,indexData.unit 的值为:{"$ref":"$.param1"}
//而非期望引用的对象param1
System.out.println(indexData);
}
}
@DaTa
class IndexData{
HashMap<String,String> param1 = Maps.newHashMap();
HashMap<String,HashMap<String,String>> unit = Maps.newHashMap();
{
unit.put("param1",param1);
}
}
2条答案
按热度按时间93ze6v8z1#
fastjson版本:1.2.51
java版本:1.8.0_181
hgb9j2n62#
Use
SerializerFeature.DisableCircularReferenceDetect
.See: https://blog.csdn.net/qq_35873847/article/details/78850528