[英]Change JSON property name if Jackson has decided to use ObjectIdentity for output
Given a class 上一堂课
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class User {
public int id;
public User other;
}
If Jackson matches a previously seen User
object, it will use the ObjectIdGenerator
to output the id instead of the actual object. 如果Jackson匹配先前看到的
User
对象,它将使用ObjectIdGenerator
输出id而不是实际对象。
An example output: 输出示例:
{
"id": 1257,
"other" : {
"id": 411,
"other": 1257
}
}
However, I'd like to use a different property name if the object id is triggered, so the output looks like 但是,如果对象ID被触发,我想使用其他属性名称,因此输出看起来像
{
"id": 1257,
"other" : {
"id": 411,
"otherRef": 1257
}
}
Will a custom serializer achieve this? 定制的序列化程序可以实现吗? I can find plenty of examples where the property value is changed by a custom serializer, but none where the property name is changed based on the property value.
我可以找到很多示例,这些示例中的属性值是由自定义序列化程序更改的,而没有一个示例,其中属性名称是根据属性值更改的。
You could use a @JsonGetter
to do this. 您可以使用
@JsonGetter
进行此操作。 Mark the field with @JsonIgnore
and then create a method with the name getOtherRef
marked with @JsonGetter
. 用
@JsonIgnore
标记该字段,然后创建一个名称为getOtherRef
标有@JsonGetter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.