hk6279
hk6279

Reputation: 1879

Jackson - way to define custom bean is empty for serialization

What is the correct way to define a custom bean is empty for serialization by Jackson?

I want to filter unwanted fields by Json->POJO->Json. I suppose the custom bean object does not appear when serialize to JSON if all variables inside the custom bean object are null. If any variables inside is not null, the custom bean object should appear.

Currently, I have an extra method to assign null value to custom bean if all variables belongs to it are null. What I am looking for is if this could be done by Jackson.

public class JsonFileContext {

    //...Some variables

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @JsonView(Views.In.class)
    private ContentRecognize contentRecognize;

    //...getter/setter method
}

ContentRecognize class

public class ContentRecognize {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String type;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private RecognizePattern targetId;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private RecognizePattern msgId;

    //...getter/setter method
}

Expected Output

{
    "var1":"var1Content",
    "var2":"var2Content"
}

AND

{
    "var1":"var1Content",
    "var2":"var2Content",
    "contentRecognize":{
        "type":"typeContent"
    }
}

Current Output

{
    "var1":"var1Content",
    "var2":"var2Content",
    "contentRecognize":{}
}

AND

{
    "var1":"var1Content",
    "var2":"var2Content",
    "contentRecognize":{
        "type":"typeContent"
    }
}

Upvotes: 3

Views: 890

Answers (1)

Michal Foksa
Michal Foksa

Reputation: 12034

You can use one of @JsonIgnoreProperties or @JsonIgnore annotations.

@JsonIgnoreProperties

Is used to ignore certain properties in serialization and deserialization regardless of its values:

to prevent specified fields from being serialized or deserialized (i.e. not include in JSON output; or being set even if they were included): @JsonIgnoreProperties({ "internalId", "secretKey" })

To ignore any unknown properties in JSON input without exception: @JsonIgnoreProperties(ignoreUnknown=true)

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

In addition, starting with Jackson 1.9, if this is the only annotation associated with a property, it will also cause cause the whole property to be ignored: that is, if setter has this annotation and getter has no annotations, getter is also effectively ignored. It is still possible for different accessors to use different annotations; so if only "getter" is to be ignored, other accessors (setter or field) would need explicit annotation to prevent ignoral (usually JsonProperty).

In your case I would either of:

1:

@JsonIgnoreProperties({ "contentRecognize" })
public class JsonFileContext {
    [...]
}

2:

public class JsonFileContext {
    //...Some variables

    @JsonIgnore
    private ContentRecognize contentRecognize;

    //...getter/setter method
}

3: annotate contentRecognize getter with @JsonIgnore

Upvotes: 1

Related Questions