Annotation Interface JsonInject


@Target({FIELD,METHOD,PARAMETER}) @Retention(RUNTIME) @Documented public @interface JsonInject
JSON Inject - Injects Non-JSON Values During Deserialization JSON 注入 - 反序列化时注入非 JSON 值

This annotation marks a field or parameter whose value should be injected from an external source (not from the JSON input) during deserialization. This is useful for injecting contextual data such as request metadata, configuration values, or database connections.

此注解标记一个字段或参数,其值应在反序列化期间从外部来源(而非 JSON 输入)注入。 适用于注入上下文数据,如请求元数据、配置值或数据库连接。

Example | 示例:

public class AuditEntry {
    private String action;

    @JsonInject("currentUser")
    private String auditor;

    @JsonInject(value = "timestamp", useInput = false)
    private long createdAt;
}

Features | 主要功能:

  • Inject external values during deserialization - 反序列化时注入外部值
  • Optional injection id for named lookups - 可选的注入 ID 用于命名查找
  • Control whether JSON input can override injected value - 控制 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 JSON input can override the injected value.
    The injection identifier used to look up the value.
  • Element Details

    • value

      String value
      The injection identifier used to look up the value. 用于查找值的注入标识符。
      Returns:
      the injection id, or empty for type-based resolution - 注入 ID,空则基于类型解析
      Default:
      ""
    • useInput

      boolean useInput
      Whether JSON input can override the injected value. JSON 输入是否可以覆盖注入的值。

      If true, the JSON value takes precedence when present; if false, the injected value is always used.

      如果为 true,当 JSON 值存在时优先使用; 如果为 false,始终使用注入的值。

      Returns:
      true if JSON input can override - 如果 JSON 输入可覆盖则返回 true
      Default:
      true