Annotation Interface JsonTypeInfo
JSON Type Info - Polymorphic Type Handling Configuration
JSON 类型信息 - 多态类型处理配置
This annotation configures how polymorphic type information is serialized and deserialized. It defines the type identifier mechanism and the inclusion strategy for type metadata in JSON.
此注解配置多态类型信息的序列化和反序列化方式。它定义了类型标识机制以及 JSON 中类型元数据的包含策略。
Example | 示例:
@JsonTypeInfo(id = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
@JsonSubTypes.Type(value = Cat.class, name = "cat")
})
public abstract class Animal {
private String name;
}
Features | 主要功能:
- Multiple type identifier strategies (class name, logical name, custom) - 多种类型标识策略(类名、逻辑名、自定义)
- Flexible inclusion modes (property, wrapper, external) - 灵活的包含模式(属性、包装器、外部)
- Default implementation fallback for unknown types - 未知类型的默认实现回退
- Visibility control for type identifier - 类型标识的可见性控制
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 ClassesModifier and TypeClassDescriptionstatic enumType information inclusion strategy 类型信息包含策略static enumType identifier mechanism 类型标识机制 -
Required Element Summary
Required ElementsModifier and TypeRequired ElementDescriptionThe type identifier mechanism to use. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionClass<?> The default implementation class to use when the type identifier is unknown or missing.The inclusion strategy for type information.The property name used for the type identifier when using property-based inclusion.booleanWhether the type identifier property should remain visible to the deserializer after type resolution.
-
Element Details
-
id
The type identifier mechanism to use. 要使用的类型标识机制。- Returns:
- the type identifier - 类型标识
-
include
JsonTypeInfo.As includeThe inclusion strategy for type information. 类型信息的包含策略。- Returns:
- the inclusion strategy, default is
JsonTypeInfo.As.PROPERTY- 包含策略,默认为JsonTypeInfo.As.PROPERTY
- Default:
PROPERTY
-
property
String propertyThe property name used for the type identifier when using property-based inclusion. 使用基于属性的包含方式时,类型标识所使用的属性名。- Returns:
- the property name, default is "@type" - 属性名,默认为 "@type"
- Default:
"@type"
-
defaultImpl
-
visible
boolean visibleWhether the type identifier property should remain visible to the deserializer after type resolution. 类型解析后,类型标识属性是否对反序列化器保持可见。- Returns:
- true if visible, default is false - 如果可见则返回 true,默认为 false
- Default:
false
-