Search code examples
javajsonjackson

How can I ignore a specific value of a property?


I'm trying to figure out how I can ignore a specific value of a property using Mixin.

For example, there is a class:

class MyClass {

  String name;   
}

I want to use a MixIn to ignore names with a value set to "ignoreThis" or "ignoreThat". Is there a way to ignore these specific values?

interface MixIn {

  @JsonIgnore
  String getName();   
}

Solution

  • I'm not sure this will work but you can give try:

    Step 1: You need to create Custom Serializer and in that you need to Serialize only if the value is not ignored

    eg: if (!"ignoreThis".equals(value) && !"ignoreThat".equals(value)) {
    

    // code }

    Step 2: You can replace add this snippet to your interface :

    @JsonSerialize(using = NameSerializer.class) String getName();

    then Apply the mixin to MyClass

    then Test serialization

    May be it will work