Annotation Interface JsonSerialize


@Target({TYPE,METHOD,FIELD}) @Retention(RUNTIME) @Documented public @interface JsonSerialize
JSON Serialize - Specifies Custom Serializer JSON 序列化 - 指定自定义序列化器

This annotation specifies a custom serializer for a field, method, or class. It allows fine-grained control over how values are converted to JSON.

此注解为字段、方法或类指定自定义序列化器。 它允许对值如何转换为 JSON 进行细粒度控制。

Example | 示例:

public class Order {
    @JsonSerialize(using = MoneyAdapter.class)
    private Money totalPrice;

    @JsonSerialize(contentUsing = ItemAdapter.class)
    private List<Item> items;

    @JsonSerialize(keyUsing = CurrencyAdapter.class)
    private Map<Currency, BigDecimal> balances;

    @JsonSerialize(as = CharSequence.class)
    private StringBuilder description;
}

Features | 主要功能:

  • Custom serializer for values - 值的自定义序列化器
  • Custom serializer for collection elements - 集合元素的自定义序列化器
  • Custom serializer for map keys - Map 键的自定义序列化器
  • Type coercion via as/contentAs/keyAs - 通过 as/contentAs/keyAs 进行类型转换

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:
  • Element Details

    • using

      Class<? extends JsonTypeAdapter<?>> using
      The serializer class to use for the annotated value. 用于注解值的序列化器类。
      Returns:
      the serializer class, or JsonTypeAdapter.None.class for default - 序列化器类,JsonTypeAdapter.None.class 表示使用默认
      Default:
      cloud.opencode.base.json.adapter.JsonTypeAdapter.None.class
    • contentUsing

      Class<? extends JsonTypeAdapter<?>> contentUsing
      The serializer class to use for collection/array elements. 用于集合/数组元素的序列化器类。
      Returns:
      the content serializer class - 内容序列化器类
      Default:
      cloud.opencode.base.json.adapter.JsonTypeAdapter.None.class
    • keyUsing

      Class<? extends JsonTypeAdapter<?>> keyUsing
      The serializer class to use for map keys. 用于 Map 键的序列化器类。
      Returns:
      the key serializer class - 键序列化器类
      Default:
      cloud.opencode.base.json.adapter.JsonTypeAdapter.None.class
    • as

      Class<?> as
      The type to serialize the value as (supertype or interface). 将值序列化为的类型(超类型或接口)。
      Returns:
      the target type, or Void.class for default - 目标类型,Void.class 表示使用默认
      Default:
      java.lang.Void.class
    • contentAs

      Class<?> contentAs
      The type to serialize collection/array elements as. 将集合/数组元素序列化为的类型。
      Returns:
      the content target type - 内容目标类型
      Default:
      java.lang.Void.class
    • keyAs

      Class<?> keyAs
      The type to serialize map keys as. 将 Map 键序列化为的类型。
      Returns:
      the key target type - 键目标类型
      Default:
      java.lang.Void.class