Annotation Interface JsonInclude


@Target({TYPE,METHOD,FIELD}) @Retention(RUNTIME) @Documented public @interface JsonInclude
JSON Include - Controls Property Inclusion Based on Value JSON 包含 - 基于值控制属性的包含策略

This annotation controls when properties are included during serialization based on their values. For example, null values or empty collections can be excluded from the serialized output.

此注解根据属性值控制序列化时是否包含该属性。例如,可以从序列化输出中 排除null值或空集合。

Example | 示例:

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
    private String name;
    private String email;    // excluded if null
    private List<String> tags; // excluded if null
}

@JsonInclude(value = JsonInclude.Include.NON_EMPTY,
             content = JsonInclude.Include.NON_NULL)
public class Config {
    private Map<String, String> settings; // excluded if empty, null values excluded
}

Features | 主要功能:

  • Exclude null values from serialization - 序列化时排除null值
  • Exclude empty strings, collections, maps - 排除空字符串、集合、映射
  • Exclude absent Optional values - 排除空的Optional值
  • Content-level inclusion for maps and collections - 映射和集合的内容级别包含策略
  • Custom filter support - 自定义过滤器支持

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: N/A - 空值安全: 不适用
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Inclusion strategy for property values 属性值的包含策略
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Inclusion strategy for content of maps and collections.
    Inclusion strategy for the property value during serialization.
  • Element Details

    • value

      Inclusion strategy for the property value during serialization. 序列化时属性值的包含策略。
      Returns:
      the inclusion strategy - 包含策略
      Default:
      ALWAYS
    • content

      Inclusion strategy for content of maps and collections. 映射和集合内容的包含策略。

      This controls inclusion of individual map values or collection elements, rather than the map/collection property itself.

      此策略控制映射中各个值或集合中各个元素的包含,而非映射/集合属性本身。

      Returns:
      the content inclusion strategy - 内容包含策略
      Default:
      ALWAYS