feat: 实现 @JsonParent 宏——父类字段序列化/反序列化支持
- 新增 src/macros/JsonParentMacro.cj(macro package simapi_serialization.macros):
标注父类自动补 open,收集 public var 字段(跳过 @JsonIgnore,
@JsonPropertyName 用注解名),字符串拼接生成静态方法:
exportJsonFields(instance: T): HashMap<String, Any>
importJsonFields(instance: T, json: HashMap<String, Any>): Unit
- JsonWriter.writeParentFields:沿 superClass 链调用 exportJsonFields 合并父类字段
- JsonReader.readParentFields:沿 superClass 链调用 importJsonFields 回填父类字段
- ReflectionCache 增加 hasExportFields/hasImportFields 缓存(避免每次反射查找)
- 测试:16 用例全绿——新增 parentMacroMethods、multiLevelInheritance、
parentAutoOpen;更新 objectSerialize/objectRoundTrip 断言父类字段输出
This commit is contained in:
@@ -40,6 +40,44 @@ public class ReflectionCache {
|
||||
|
||||
private static let _cache = HashMap<String, ArrayList<FieldMetadata>>()
|
||||
|
||||
/// 类型 → 是否有 @JsonParent 宏生成的 exportJsonFields(Bool)
|
||||
private static let _exportCache = HashMap<String, Bool>()
|
||||
/// 类型 → 是否有 @JsonParent 宏生成的 importJsonFields(Bool)
|
||||
private static let _importCache = HashMap<String, Bool>()
|
||||
|
||||
/**
|
||||
* 该类型是否含 @JsonParent 宏生成的导出静态方法(含缓存)。
|
||||
*/
|
||||
public static func hasExportFields(typeInfo: ClassTypeInfo): Bool {
|
||||
hasParentMethod(typeInfo, "exportJsonFields", _exportCache)
|
||||
}
|
||||
|
||||
/**
|
||||
* 该类型是否含 @JsonParent 宏生成的导入静态方法(含缓存)。
|
||||
*/
|
||||
public static func hasImportFields(typeInfo: ClassTypeInfo): Bool {
|
||||
hasParentMethod(typeInfo, "importJsonFields", _importCache)
|
||||
}
|
||||
|
||||
private static func hasParentMethod(typeInfo: ClassTypeInfo, name: String, cache: HashMap<String, Bool>): Bool {
|
||||
let key = typeInfo.qualifiedName
|
||||
if (let Some(cached) <- cache.get(key)) {
|
||||
return cached
|
||||
}
|
||||
let found = try {
|
||||
if (name == "exportJsonFields") {
|
||||
typeInfo.getStaticFunction(name, [typeInfo])
|
||||
} else {
|
||||
typeInfo.getStaticFunction(name, [typeInfo, TypeInfo.of<HashMap<String, Any>>()])
|
||||
}
|
||||
true
|
||||
} catch (_: Exception) {
|
||||
false
|
||||
}
|
||||
cache[key] = found
|
||||
found
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型的字段元数据(含缓存)。
|
||||
* @param typeInfo 目标类类型。
|
||||
|
||||
Reference in New Issue
Block a user