Annotation Interface JsonFormat


@Target({FIELD,METHOD,TYPE}) @Retention(RUNTIME) @Documented public @interface JsonFormat
JSON Format - Specifies Serialization Format JSON 格式 - 指定序列化格式

This annotation specifies the format for serializing and deserializing values, particularly useful for dates and numbers.

此注解指定序列化和反序列化值的格式,特别适用于日期和数字。

Example | 示例:

public class Event {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private LocalDateTime eventTime;

    @JsonFormat(shape = Shape.STRING, pattern = "0.00")
    private BigDecimal price;

    @JsonFormat(shape = Shape.NUMBER)
    private Status status;  // Enum serialized as number
}

Features | 主要功能:

  • Date/time pattern formatting - 日期/时间模式格式化
  • Shape control for value output (string, number, array, etc.) - 值输出形状控制
  • Timezone and locale configuration - 时区和区域设置配置

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 
    Shape of the JSON value JSON 值的形状
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether lenient parsing is enabled.
    The locale for formatting.
    The pattern for formatting (e.g., date patterns).
    The shape of the serialized value.
    The timezone for date/time values.
  • Element Details

    • pattern

      String pattern
      The pattern for formatting (e.g., date patterns). 格式化模式(如日期模式)。
      Returns:
      the format pattern - 格式模式
      Default:
      ""
    • shape

      The shape of the serialized value. 序列化值的形状。
      Returns:
      the shape - 形状
      Default:
      ANY
    • timezone

      String timezone
      The timezone for date/time values. 日期/时间值的时区。
      Returns:
      the timezone ID (e.g., "UTC", "Asia/Shanghai") - 时区ID
      Default:
      ""
    • locale

      String locale
      The locale for formatting. 格式化的区域设置。
      Returns:
      the locale (e.g., "zh_CN", "en_US") - 区域设置
      Default:
      ""
    • lenient

      boolean lenient
      Whether lenient parsing is enabled. 是否启用宽松解析。
      Returns:
      true for lenient parsing - 如果宽松解析则返回 true
      Default:
      false