Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize @JsonIgnore-d field

Tags:

java

jackson

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.

like image 438
Radek Postołowicz Avatar asked Jul 05 '16 16:07

Radek Postołowicz


People also ask

How do you ignore certain fields based on a serializing object to JSON?

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.

What does JsonIgnore annotation do?

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.

How do you tell Jackson to ignore a field during serialization?

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.

What is the difference between JsonIgnore and JsonIgnoreProperties?

@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.


1 Answers

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.

like image 170
Wilson Avatar answered Oct 03 '22 03:10

Wilson