9

I am trying to do something which was quite easy in gson. Since I switched to Jackson as serializer, I couldn't figure out how to implement this:

I want to serialize only fields that have been marked by an Annotation. GSON code would be:

class Foo {
    @Expose
    public String sometext="Hello World";
    @Expose
    public int somenumber=30;
    public float noop=1.0;
    ...
 }

which should result in (JSON)

 {
    Foo: {
        sometext:'Hello World',
        somenumber: 30
    }
 }

(Syntax errors may be ignored - source is just for demonstration)

So what's the Jackson counterpart for gson's @Expose and new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();?

4 Answers 4

12

If you want it just for a specific type you could also just use annotations:

import com.fasterxml.jackson.annotation.JsonAutoDetect;


@JsonAutoDetect(
    fieldVisibility = JsonAutoDetect.Visibility.NONE,
    setterVisibility = JsonAutoDetect.Visibility.NONE,
    getterVisibility = JsonAutoDetect.Visibility.NONE,
    isGetterVisibility = JsonAutoDetect.Visibility.NONE,
    creatorVisibility = JsonAutoDetect.Visibility.NONE
)
public class Foo  {
    @JsonProperty
    public String sometext="Hello World";
    @JsonProperty
    public int somenumber=30;
    // noop won't get serialized
    public float noop= 1.0f;
}
1
5

The counterpart for new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); is to initialise an ObjectMapper as follows:

...
ObjectMapper objectMapper = new ObjectMapper();        
objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS,
            MapperFeature.AUTO_DETECT_FIELDS,
            MapperFeature.AUTO_DETECT_GETTERS,
            MapperFeature.AUTO_DETECT_IS_GETTERS);
...

The counterpart for @Expose is then @JsonProperty. Using your above example bean:

class Foo {
    @JsonProperty
    public String sometext="Hello World";
    @JsonProperty
    public int somenumber=30;
    public float noop=1.0;
    ...
 }

See this answer to a very similar question.

4

There seems to be a way to configure ObjectMapper to ignore all non annotated fields.

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(getSerializationConfig().getDefaultVisibilityChecker()
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.NONE)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE));

Source

1
  • 1
    This method is deprecated since 2.6
    – uranix
    Oct 23, 2020 at 21:23
2

In Jackson, you do the inverse. Annotate fields you don't want with @JsonIgnore.

Marker annotation that indicates that the annotated method or field is to be ignored by introspection-based serialization and deserialization functionality. That is, it should not be consider a "getter", "setter" or "creator".

3
  • 4
    I know that, the problem is that my classes have more fields to be ignored, than to be serialized. e.g. 50 fields where only one should be serialized. So the other way round would be the method of choice.
    – gorootde
    Mar 26, 2014 at 17:23
  • @k_wave As far as I know, there is no such feature out of the box. Mar 26, 2014 at 17:29
  • @k_wave You can play with visibility if you're willing to change the access modifiers on your fields/getters/setters. Mar 26, 2014 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.