如何使用杰克逊反序列化循环引用(由Newtonsoft.Json序列化)



我必须使用循环引用反序列化JSON。对象的首次出现具有:

"$id":"1"

引用如下所示:

{"$ref":"1"}

有了@JsonIdentityInfo,我可以使$id可读,但杰克逊不会阅读参考文献。如果我手动删除"$ref"的东西,反序列化有效,并且 JSON 字符串中只有引用键本身。

如何让杰克逊处理"$ref"样式的引用?

我尝试了自己的反序列化程序,但对于这个非常简单的问题来说,这似乎很复杂。

我要反序列化的类得到:

@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="$id")

我采用整个 JSON 字符串并用正则表达式替换 ref 内容。

String regex = "\{\"\$ref\":\"(\d{1,})*\"\}";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = pattern.matcher(jsonString);
String resultString="";
if (matcher.find())
{
    resultString = matcher.replaceAll("$1");
}

更换字符串几乎不需要时间。

相关内容

最新更新