gpt4 book ai didi

java - 使用 Jackson 将对象转换为 JSON 时的无限递归

转载 作者:行者123 更新时间:2023-12-01 21:16:03 25 4
gpt4 key购买 nike

我有一个类,用于存储工具的首选项。此类使用 toString() 将自身转换为 JSON。当我将对象转换为 JSON 时,出现无限递归错误,但我不明白为什么。我没有任何循环引用,所以这让我有点困惑。任何帮助将不胜感激。

这是我的类结构:

public class ToolPrefs
{
private boolean isFloating;
private ToolPalettePlacement placement; //this is an enum
private boolean isSelected;
private int dockLocation;
private Point dialogLocation;
private Dimension dialogSize;

public ToolPrefs(){}

public boolean isFloating(){return isFloating; }

public void setFloating(boolean floating){ isFloating = floating; }

public ToolPalettePlacement getPlacement(){ return placement; }

public void setPlacement(ToolPalettePlacement placement){ this.placement = placement;}

public boolean isSelected(){ return isSelected;}

public void setSelected(boolean selected){isSelected = selected;}

public int getDockLocation(){return dockLocation;}

public void setDockLocation(int dockLocation){this.dockLocation = dockLocation; }

public Point getDialogLocation(){return dialogLocation;}

public void setDialogLocation(Point dialogLocation){ this.dialogLocation = dialogLocation;}

public Dimension getDialogSize(){ return dialogSize; }

public void setDialogSize(Dimension dialogSize){this.dialogSize = dialogSize;}


@JsonIgnore @Override
public String toString()
{
String json;
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try
{
json = ow.writeValueAsString(this);
}
catch (IOException e)
{
e.printStackTrace();
json = "";
}
return json;
}
}

这是我收到的异常片段:

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: java.awt.Point["location"]->java.awt.Point["location"]->java.awt.Point["location"]->java.awt.Point["location"]...) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:518) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:117) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:504) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:117) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:504) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:117) at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:464) at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:504) at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:117)

最佳答案

您面临的问题是由于您的对象嵌入的 Point 类造成的。

要解决此问题,您可以指示 Jackson 在第一层深度停止,要实现此解决方案,您可以按照以下步骤操作:

1)首先,您必须运行 2.x 版本的 Jackson,我已经用以下方法测试了解决方案:

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>

2) 当您初始化 ObjectMapper 的新实例时,请执行以下操作:

ObjectWriter ow = new ObjectMapper().configure(DEFAULT_VIEW_INCLUSION, true).writer().withDefaultPrettyPrinter();

3) 如果您有 Point 类型,请使用注释:

@JsonView(Point.class) Point dialogLocation;

jackson 将停在第一层,结果将是:

{
"dialogLocation" : {
"x" : 2.0,
"y" : 3.0
}
}

有关更多文档,您可以查看以下内容:

Limit Jackson output to only one level

Enable default view inclusion on Jackson 2.x ObjectMapper

我已经测试了该解决方案,只要您使用 Jackson 的 2.x 版本,它就可以工作。对于 1.x,我还无法让它运行。

关于java - 使用 Jackson 将对象转换为 JSON 时的无限递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40063954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com