简体   繁体   中英

How to include only certain properties and fields of my object to be serialized by Jackson?

I have a class I need to serialize and it contains a lot of properties and fields, but I only need a small subset to be serialized, not the entire class. I also want to guarantee that I won't inadvertently add fields in the future which would be serialized and break my expectation of what gets serialized by suddenly including something new.

So is there any annotation I can use to basically specify what gets serialize only?

I'd need like the opposite of @JsonIgnoreProperties. Something that acts as a whitelist instead of a blacklist.

Haven't tested this, but you should be able to disable JsonAutoDetect, then annotate each property manually.

See http://wiki.fasterxml.com/JacksonFeatureAutoDetect

没有这样的注释,您必须在@JsonIgnoreProperties的属性上使用@JsonIgnoreProperties

You can introduce a new class from your class.

MyClass Your original class

MyClassForJson Fork of your class that has properties of your choice.

Use second class for only serialization. Maybe you can cast just before serialization. This way no unexpected changes will happen to your json.

So far I have preferred using wrapper objects to achieve this whitelist effect (in my case the additional complication is that the POJO I want to selectively serialize is part of an object hierarchy, so using Views is not possible).

If MyClass is the original class and MyClassForJson is the wrapper, this is how I use it:

public class MyClassForJson {

    private MyClass wrapped;

    public MyClassForJson(MyClass toBeWrapped) {
        this.wrapped = toBeWrapped;
    }

    /**
     * Delegate method exposing a property
     */
    public String getExposedProperty() {
        return this.wrapped.getExposedProperty();
    }
}

Now only properties I create delegate getter methods for will ever be exposed to Jackson when serializing, irrespective of how many properties I add to MyClass . Delegate methods can be auto-generated by most IDEs today for selected properties of the wrapped class.

If I want to have different sets of whitelisted properties for serializing MyClass in different ways, all I have to do is create different wrapper classes.

I was able to get this to work using the @JsonView annotation.

JsonView

Annotation used for indicating view(s) that the property that is defined by method or field annotated is part of.

You could use any class for the view, but I prefer them created in one spot:

public class JsonViews {
    public static class Small {}
    public static class Medium extends Small {}
}

Then in the Class:

public class User {
    @JsonView(JsonViews.Small.class)
    private Integer id;
    @JsonView(JsonViews.Medium.class)
    private String name;
    // email does not have a view, by default it will be in any view
    private String email;

    ...
}

Then we can pass the view class into the ObjectMapper

mapper.writerWithView(clazz);

If you do not want properties without a view defined to be in the JSON make sure to configure the ObjectMapper

mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

It´s an old question, but if anyone is still searching for the answer:

The feature you were searching for is implemented since version 2.12 and is called @JsonIncludeProperties.

More information: https://fasterxml.github.io/jackson-annotations/javadoc/2.12/com/fasterxml/jackson/annotation/JsonIncludeProperties.html

For anyone who wants to do this for specific class.

As pointed out in another answer @JsonAutoDetect annotation is the way.

Then annotate properties you want with @JsonProperty to include them in serialization.

Example bellow, note the isGetterVisibility = Visibility.NONE parameter for boolean properties if you are, like me, using lombok or some other "getter generating" tool.

@JsonAutoDetect(getterVisibility = Visibility.NONE, isGetterVisibility = Visibility.NONE)
public class MyClass {
    @JsonProperty
    private String id;

    //list of properties without @JsonProperty, which will not be serialized
}

There are more options for @JsonAutoDetect see documenation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM