序列化@ JsonIgnore-d字段 [英] Serialize @JsonIgnore-d field
问题描述
我上课了 @ JsonIgnore -d
字段:
public class MyClass {
...
@JsonIgnore
private SomeType myfield;
...
// getters & setters
}
是否可以配置 ObjectWriter
所以在序列化期间它包括 myfield
即使被加入?
理由:MyClass在很多地方被序列化,只有我想要的单个特定的 myfield
。
Is it possible to configure ObjectWriter
so that it includes myfield
during serialization even though being ingored?
Rationale: MyClass is serialized in many places and only in single specific one I want to have myfield
.
推荐答案
可以配置 ObjectMapper
来禁用JsonIgnore函数。以下是您可以尝试的一些可能的解决方案:
It is possible to configure ObjectMapper
to disable a JsonIgnore function. Following are some possible solution you can try with:
1。
禁用特定带注释字段的JsonIgnore函数。
您可以创建自定义JsonIgnore注释和自定义 JacksonAnnotationIntrospector
从映射器上下文中删除注释。
以下是这些想法:
You can create a custom JsonIgnore annotation and a custom JacksonAnnotationIntrospector
to remove the annotation from mapper context.
Following are the ideas:
注释 @MyJsonIgnore
到序列化时应忽略的字段:
Annotate @MyJsonIgnore
to the fields that should be ignored while serialization:
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class MyClass {
@MyJsonIgnore
private SomeType myField;
}
@MyJsonIgnore
是一个简单的自定义注释,包装 @JsonIgnore
:
@MyJsonIgnore
is a simple custom annotation that wrap @JsonIgnore
:
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonIgnore
public @interface MyJsonIgnore {
}
实现自定义 JacksonAnnotationIntrospector
以从mapper上下文中删除 @MyJsonIgnore
:
A custom JacksonAnnotationIntrospector
is implemented to remove @MyJsonIgnore
from mapper context:
public class DisablingMyJsonIgnoreIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean isAnnotationBundle(final Annotation ann) {
if (ann.annotationType().equals(MyJsonIgnore.class)) {
return false;
} else {
return super.isAnnotationBundle(ann);
}
}
之后,你可以在一个<上设置内省运动配置期间code> ObjectMapper :
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new DisablingMyJsonIgnoreIntrospector());
结果是用 @MyJsonIgnore
可以正确封送。
It results that the fields annotated with @MyJsonIgnore
can be marshaled properly.
2。
禁用映射器的JsonIgnore函数
您可以创建自定义 JacksonAnnotationIntrospector
并覆盖 hasIgnoreMarker
方法总是返回 false
:
Your can create a custom JacksonAnnotationIntrospector
and override hasIgnoreMarker
method to always return false
:
public static class DisablingJsonIgnoreIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean hasIgnoreMarker(final AnnotatedMember m) {
return false;
}
}
hasIgnoreMarker
是检查是否有注释忽略json属性。返回 false
将禁用JsonIngore函数。
hasIgnoreMarker
is to check whether there is annotation to ignore json property. Return false
will disable the JsonIngore function.
3。
禁用所有注释并指定为给定的 ObjectMapper
自动检测哪些属性:
3.
Disable all annotations and specify what kinds of properties are auto-detected for a given ObjectMapper
:
final ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
这只是禁用所有注释。
希望这可以提供帮助。
这篇关于序列化@ JsonIgnore-d字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!