Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ignore all properties of superclass from external library

I am developing using an ORM where I extend a base orm class to create tables.

For example:

public class Person extends DbItem {
    @JsonIgnore
    private String index;

    private String firstName;

    private String lastName;
}

Problem is that when I use ObjectMapper to serialize, it tries to serialize the members of the DbItem class. Is there any simple way to prevent this? For example with an annotation.

I had a look at a similar problem Jackson serialization: how to ignore superclass properties but I was hoping it could be done simpler, and I'm not sure if I could do it as I can't change the superclass since it is in an external library.

like image 602
Simen Russnes Avatar asked Apr 14 '15 14:04

Simen Russnes


People also ask

How do I ignore fields in ObjectMapper?

Jackson API provides two ways to ignore unknown fields, first at the class level using @JsonIgnoreProperties annotation and second at the ObjectMapper level using configure() method.

How do I ignore JSON property?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

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.

How do I ignore a field in JSON response Jackson?

Ignore All Fields by Type Finally, we can ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly: @JsonIgnoreType public class SomeType { ... } More often than not, however, we don't have control of the class itself.


1 Answers

You can use a Mix-in or @JsonIgnoreProperties

For the purposes of these examples, the base ORM class and extension are assumed to be:

public class DbItem {
    public String dbPropertyA;
    public String dbPropertyB;
}

and

public class Person extends DbItem {
    public String index;
    public String firstName;
    public String lastName;
}

respectively.

Using a Mix-in

A Mix-in is an abstraction of the de/serialization instructions that Jackson understands from an object itself. It is a way to customize de/serialization of 3rd party classes. In order to define a Mix-in, an abstract class must be created and registered with the ObjectMapper.

Example Mix-in Definition

public abstract class PersonMixIn {
    @JsonIgnore public String dbPropertyA;
    @JsonIgnore public String dbPropertyB;
    @JsonIgnore public String index;
}

Registering the Mix-in

@Test
public void serializePersonWithMixIn() throws JsonProcessingException {
    // set up test data including parent properties
    Person person = makeFakePerson();

    // register the mix in
    ObjectMapper om = new ObjectMapper()
            .addMixIn(Person.class, PersonMixIn.class);

    // translate object to JSON string using Jackson
    String json = om.writeValueAsString(person);

    assertFalse(json.contains("dbPropertyA"));
    assertFalse(json.contains("dbPropertyB"));
    assertFalse(json.contains("index"));
    System.out.println(json);
}

@JsonIgnoreProperties

If you want to avoid creating a class and configuring the ObjectMapper, the @JsonIgnoreProperties annotation can be utilized. Simply annotate the class you are serializing and list the properties to exclude.

Example Serializable Object

@JsonIgnoreProperties({"index", "dbPropertyA", "dbPropertyB"})
public class Person extends DbItem {
    public String index;
    public String firstName;
    public String lastName;
}

See It In Action

@Test
public void serializePersonWithIgnorePropertiesAnnotation() throws JsonProcessingException {
    // set up test data including parent properties
    Person person = makeFakePerson();

    ObjectMapper om = new ObjectMapper();

    // translate object to JSON string using Jackson
    String json = om.writeValueAsString(person);

    assertFalse(json.contains("dbPropertyA"));
    assertFalse(json.contains("dbPropertyB"));
    assertFalse(json.contains("index"));
    System.out.println(json);
}
like image 141
Sam Berry Avatar answered Oct 11 '22 22:10

Sam Berry