javaserializationjacksonjackson-databindjson-serialization

Jackson custom filter not triggered


I have a JSON object as:

{"username":"johndoe","email":"john.doe@example.com",
"secretToken":"token123","secretKey":"password","phoneNumber":"518-555-1234","active":true}

Using Jackson and Java, I would like to:

  1. remove all fields which are starting with secret.
  2. remove all fields which are boolean and true.

Basically get:

{"username":"johndoe","email":"john.doe@example.com",
"phoneNumber":"518-555-1234"}

Here is my custom filter:

  public static class CustomStaticBeanPropertyFilter extends SimpleBeanPropertyFilter {

    @Override
    public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, BeanPropertyWriter writer)
        throws Exception {
      System.out.println("hello");
      // Custom filter logic
      String fieldName = writer.getName();
      Object fieldValue = writer.get(pojo);

      // Exclude fields starting with "secret"
      if (fieldName.startsWith("secret")) {
        return;
      }

      // Exclude fields with null values
      if (fieldValue == null) {
        return;
      }

      // Exclude fields with false values (only applicable to Boolean
      // fields)
      if (fieldValue instanceof Boolean && (Boolean) fieldValue) {
        return;
      }

      // Serialize all other fields
      writer.serializeAsField(pojo, jgen, provider);
    }

    @Override
    protected boolean include(BeanPropertyWriter writer) {
      return true; // Default inclusion
    }

    @Override
    protected boolean include(PropertyWriter writer) {
      return true; // Default inclusion
    }
  }

Here is my Pojo:

  @JsonFilter("customFilter")
  public static class User {
    private String username;
    private String email;
    private String secretToken;
    private String secretKey;
    private String phoneNumber;
    private boolean isActive;

    // Constructors
    public User() {
    }

    public User(String username, String email, String secretToken, String secretKey, String phoneNumber, boolean isActive) {
      this.username = username;
      this.email = email;
      this.secretToken = secretToken;
      this.secretKey = secretKey;
      this.phoneNumber = phoneNumber;
      this.setActive(isActive);
    }

    // Getters and Setters
    public String getUsername() {
      return username;
    }

    public void setUsername(String username) {
      this.username = username;
    }

    public String getEmail() {
      return email;
    }

    public void setEmail(String email) {
      this.email = email;
    }

    public String getSecretToken() {
      return secretToken;
    }

    public void setSecretToken(String secretToken) {
      this.secretToken = secretToken;
    }

    public String getSecretKey() {
      return secretKey;
    }

    public void setSecretKey(String secretKey) {
      this.secretKey = secretKey;
    }

    public String getPhoneNumber() {
      return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
      this.phoneNumber = phoneNumber;
    }

    @JsonFilter("customFilter")
    public boolean isActive() {
      return isActive;
    }

    public void setActive(boolean isActive) {
      this.isActive = isActive;
    }
  }

And here is how I call Jackson:

  public static void main(String[] args) throws IOException {
    // Create a User object
    User user = new User("johndoe", "john.doe@example.com", "token123", "password", "518-555-1234", true);

    // Initialize ObjectMapper
    ObjectMapper mapper = new ObjectMapper();

    // Step 3: Use the custom filter with SimpleFilterProvider
    SimpleFilterProvider filterProvider = new SimpleFilterProvider()
        .addFilter("customFilter", new CustomStaticBeanPropertyFilter());

    // Step 4: Serialize the User object with the custom filter
    mapper.setFilterProvider(filterProvider);
    String serializedUser = mapper.writeValueAsString(user);

    // Output the serialized JSON
    System.out.println(serializedUser);
  }

I tried:

filterProvider = new SimpleFilterProvider().addFilter(
     "customFilter",
     SimpleBeanPropertyFilter.serializeAllExcept("secretKey"));

It works, but is not viable in my use case.

I also replaced:

    mapper.setFilterProvider(filterProvider);
    String serializedUser = mapper.writeValueAsString(user);

by:

    String serializedUser = mapper.writer(filterProvider).writeValueAsString(user);

but nothing changed...

Using Jackson 2.15.2 and Java 8. I can be flexible on both.


Solution

  • In your class CustomStaticBeanPropertyFilter, you're overriding the wrong method.

    The serailizeAsField method with signature (Object, JsonGenerator, SerializerProvider, BeanPropertyWriter) is deprecated, and instead it should be favored the overloaded version with signature (Object, JsonGenerator, SerializerProvider, PropertyWriter).

    Your filter class should look something like this:

    public static class CustomStaticBeanPropertyFilter extends SimpleBeanPropertyFilter {
    
        @Override
        public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception {
            // Custom filter logic
            String fieldName = writer.getName();
            Object fieldValue = writer.getMember().getValue(pojo);
    
            // Exclude fields starting with "secret"
            if (fieldName.startsWith("secret")) {
                return;
            }
    
            // Exclude fields with null values
            if (fieldValue == null) {
                return;
            }
    
            // Exclude fields with false values (only applicable to Boolean fields)
            if (fieldValue instanceof Boolean && (Boolean) fieldValue) {
                return;
            }
    
            // Serialize all other fields
            writer.serializeAsField(pojo, jgen, provider);
        }
    
        @Override
        protected boolean include(BeanPropertyWriter writer) {
            return true; // Default inclusion
        }
    
        @Override
        protected boolean include(PropertyWriter writer) {
            return true; // Default inclusion
        }
    }
    

    Here is also a demo at OneCompiler using the same Jackson version you do.