Interface PropertyFilter

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface PropertyFilter
Property Filter - Dynamic Property Filtering for JSON Serialization 属性过滤器 - JSON 序列化的动态属性过滤

This interface provides dynamic filtering of properties during JSON serialization. It supports @JsonFilter style filtering where properties can be included or excluded based on name, value, or declaring class.

此接口提供 JSON 序列化期间的动态属性过滤。它支持 @JsonFilter 风格的过滤,其中属性可以根据名称、值或声明类进行包含或排除。

Example | 示例:

// Include only specific properties
PropertyFilter filter = PropertyFilter.include("name", "email");

// Exclude specific properties
PropertyFilter filter = PropertyFilter.exclude("password", "secret");

// Exclude null values
PropertyFilter filter = PropertyFilter.includeNonNull();

// Custom filter
PropertyFilter filter = (name, value, clazz) ->
    !name.startsWith("_");

Features | 主要功能:

  • Dynamic property inclusion/exclusion - 动态属性包含/排除
  • Static factory methods for common filters - 常见过滤器的静态工厂方法
  • Composable with lambda expressions - 可通过 lambda 表达式组合

Security | 安全性:

  • Thread-safe: Yes (stateless or immutable) - 线程安全: 是(无状态或不可变)
  • Null-safe: Handles null values gracefully - 空值安全: 优雅处理 null 值
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • includeProperty

      boolean includeProperty(String propertyName, Object value, Class<?> declaringClass)
      Determines whether a property should be included in the serialized output. 确定属性是否应包含在序列化输出中。
      Parameters:
      propertyName - the property name - 属性名称
      value - the property value (may be null) - 属性值(可能为 null)
      declaringClass - the class that declares the property - 声明该属性的类
      Returns:
      true if the property should be included - 如果应包含该属性则返回 true
    • includeAll

      static PropertyFilter includeAll()
      Returns a filter that includes all properties. 返回包含所有属性的过滤器。
      Returns:
      a filter that always returns true - 始终返回 true 的过滤器
    • excludeAll

      static PropertyFilter excludeAll()
      Returns a filter that excludes all properties. 返回排除所有属性的过滤器。
      Returns:
      a filter that always returns false - 始终返回 false 的过滤器
    • include

      static PropertyFilter include(String... properties)
      Returns a filter that includes only the specified properties (whitelist). 返回仅包含指定属性的过滤器(白名单)。
      Parameters:
      properties - the property names to include - 要包含的属性名称
      Returns:
      a whitelist filter - 白名单过滤器
      Throws:
      NullPointerException - if properties is null - 如果 properties 为 null
    • exclude

      static PropertyFilter exclude(String... properties)
      Returns a filter that excludes the specified properties (blacklist). 返回排除指定属性的过滤器(黑名单)。
      Parameters:
      properties - the property names to exclude - 要排除的属性名称
      Returns:
      a blacklist filter - 黑名单过滤器
      Throws:
      NullPointerException - if properties is null - 如果 properties 为 null
    • includeNonNull

      static PropertyFilter includeNonNull()
      Returns a filter that excludes properties with null values. 返回排除 null 值属性的过滤器。
      Returns:
      a non-null filter - 非空过滤器