I have class with @JsonIgnore-d
field:
public class MyClass {
...
@JsonIgnore
private SomeType myfield;
...
// getters & setters
}
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
.
The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.
The @JsonIgnoreProperties annotation is used at the class level to ignore fields during serialization and deserialization. The properties that are declared in this annotation will not be mapped to the JSON content.
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
@JsonIgnore is to ignore fields used at field level. while, @JsonIgnoreProperties is used at class level, used for ignoring a list of fields. Annotation can be applied both to classes and to properties.
It is possible to configure ObjectMapper
to disable a JsonIgnore function. Following are some possible solution you can try with:
1. Disable JsonIgnore function for a particular annotated field.
You can create a custom JsonIgnore annotation and a custom JacksonAnnotationIntrospector
to remove the annotation from mapper context.
Following are the ideas:
Annotate @MyJsonIgnore
to the fields that should be ignored while serialization:
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class MyClass {
@MyJsonIgnore
private SomeType myField;
}
@MyJsonIgnore
is a simple custom annotation that wrap @JsonIgnore
:
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonIgnore
public @interface 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);
}
}
After that, you can set the introspector on a ObjectMapper
during configuration:
ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new DisablingMyJsonIgnoreIntrospector());
It results that the fields annotated with @MyJsonIgnore
can be marshaled properly.
2. Disable JsonIgnore function for the mapper
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
is to check whether there is annotation to ignore json property. Return false
will disable the JsonIngore function.
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);
This simply disable all annotations.
Hope this can help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With