Annotation Interface JsonValue


@Target({METHOD,FIELD}) @Retention(RUNTIME) @Documented public @interface JsonValue
JSON Value - Marks a Method or Field as the Serialized Representation JSON 值 - 标记方法或字段作为序列化表示

This annotation indicates that the return value of the annotated method (or the value of the annotated field) should be used as the JSON representation of the object. Only one @JsonValue annotation is allowed per class.

此注解表示被标注方法的返回值(或被标注字段的值)应作为该对象的 JSON 表示。 每个类中只允许使用一个 @JsonValue 注解。

Example | 示例:

public enum Status {
    ACTIVE("active"),
    INACTIVE("inactive");

    private final String code;

    Status(String code) {
        this.code = code;
    }

    @JsonValue
    public String getCode() {
        return code;
    }
}

public class Wrapper {
    @JsonValue
    private final String rawValue;

    public Wrapper(String rawValue) {
        this.rawValue = rawValue;
    }
}

Features | 主要功能:

  • Custom serialization form for any class - 为任意类自定义序列化形式
  • Commonly used with enums for custom JSON values - 常用于枚举的自定义 JSON 值
  • Supports both methods and fields - 支持方法和字段
  • Can be disabled via value = false - 可通过 value = false 禁用

Constraints | 约束:

  • Only one @JsonValue annotation per class - 每个类只能有一个 @JsonValue 注解
  • The method return value becomes the entire JSON representation - 方法返回值成为整个 JSON 表示

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:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether this annotation is active.
  • Element Details

    • value

      boolean value
      Whether this annotation is active. 此注解是否生效。

      Set to false to disable a @JsonValue inherited from a superclass.

      设为 false 可禁用从父类继承的 @JsonValue

      Returns:
      true if enabled, false if disabled - 启用返回 true,禁用返回 false
      Default:
      true