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:
2026-08-17 13:41:16 +08:00
parent 00fea9703c
commit affadac6c2
5 changed files with 372 additions and 4 deletions
+29
View File
@@ -133,9 +133,38 @@ public class JsonReader {
case None => ()
}
}
// 父类字段(@JsonParent 宏生成的静态导入方法,沿继承链逐层回填)
readParentFields(value, ct, instance, options)
instance
}
/// 沿 superClass 链调用每层的 importJsonFields 静态方法回填父类字段。
/// 无宏的父类(hasImportFields 缓存判断)静默跳过;宏生成的方法内部按
/// if (k == "字段名") 忽略未知键,因此直接把全部 JSON 键传入即可。
private static func readParentFields(value: JsonValue, ct: ClassTypeInfo, instance: Any, options: JsonOption): Unit {
let jobj = value.asObject()
var parent = ct.superClass
while (let Some(p) <- parent) {
if (ReflectionCache.hasImportFields(p)) {
try {
let fn = p.getStaticFunction("importJsonFields",
[p, TypeInfo.of<HashMap<String, Any>>()])
var pjson = HashMap<String, Any>()
for ((k, jv) in jobj.getFields()) {
if (!(jv is JsonNull)) {
pjson[k] = jsonToAny(jv)
}
}
let args: Array<Any> = [instance, pjson]
fn.apply(p, args)
} catch (_: Exception) {
// 导入失败(如类型不匹配)不中断整体反序列化
}
}
parent = p.superClass
}
}
/// Array<X>:经 ArrayList<X> 构造 + add + toArray
private static func readArray(value: JsonValue, elemName: String, options: JsonOption, depth: Int64): Any {
let listTypeName = "std.collection.ArrayList<${elemName}>"
+92 -4
View File
@@ -13,6 +13,7 @@ package simapi_serialization.json
import std.collection.*
import std.unittest.*
import std.unittest.testmacro.*
import simapi_serialization.macros.*
enum Role {
| Admin
@@ -26,10 +27,16 @@ class Address {
public var zip: String = "" // 无下划线 → 原样输出 zip
}
// 继承测试:父类字段(_id/_createdAt)受仓颉反射「声明类严格校验」限制
// 继承测试:父类标注 @JsonParent 宏 → 自动补 open + 生成导出/导入静态方法,
// 父类字段(_id/_createdAt)经宏的静态类型访问绕开反射声明类校验
@JsonParent
open class BaseUser {
public var _id: String = ""
public var _createdAt: String = ""
@JsonIgnore
public var _temp: String = ""
@JsonPropertyName["parent_alias"]
public var _parentAlias: String = ""
}
class User <: BaseUser {
@@ -48,8 +55,27 @@ class User <: BaseUser {
public var temp: String = "secret" // 无下划线 + 忽略
}
// 多层继承:爷(标宏)→ 父(不标宏)→ 子
@JsonParent
open class GrandParent {
public var _gp: String = ""
}
open class MidParent <: GrandParent {
public var _mp: String = ""
}
class LeafChild <: MidParent {
public var _lc: String = ""
public init() {}
}
func makeUser(): User {
let u = User()
u._id = "u-1"
u._createdAt = "2025-01-01"
u._temp = "secret-parent"
u._parentAlias = "parent-xiaoming"
u._name = "alice"
u._age = 30
u.score = 88.5
@@ -117,9 +143,14 @@ public class JsonSerializerTests {
// 嵌套对象
@Expect(json.contains("\"_city\": \"beijing\""), true)
@Expect(json.contains("\"zip\": \"100000\""), true)
// 继承限制:父类字段不序列化
@Expect(json.contains("_id"), false)
@Expect(json.contains("_createdAt"), false)
// 父类字段(@JsonParent 宏):_id/_createdAt 输出,@JsonIgnore 父类字段不输出
@Expect(json.contains("\"_id\": \"u-1\""), true)
@Expect(json.contains("\"_createdAt\": \"2025-01-01\""), true)
// 父类 @JsonPropertyName["parent_alias"] 生效
@Expect(json.contains("\"parent_alias\": \"parent-xiaoming\""), true)
@Expect(json.contains("_parentAlias"), false)
// 父类 @JsonIgnore _temp 不输出
@Expect(json.contains("secret-parent"), false)
}
/// 对象反序列化往返
@@ -135,6 +166,10 @@ public class JsonSerializerTests {
@Expect(u2.addr.zip, "100000")
@Expect(u2._alias, "xiaoming")
@Expect(u2.temp, "secret") // @JsonIgnore:反序列化不改动默认值
// 父类字段往返(@JsonParent 宏)
@Expect(u2._id, "u-1")
@Expect(u2._createdAt, "2025-01-01")
@Expect(u2._parentAlias, "parent-xiaoming")
// Option 往返
let optVal: ?String = u2.opt
@Expect(optVal.isSome(), true)
@@ -286,4 +321,57 @@ public class JsonSerializerTests {
}
@Expect(threw, true)
}
/// 父类字段序列化/反序列化(@JsonParent 宏):已并入 objectSerialize/objectRoundTrip
/// 此处验证宏生成的静态方法可直接调用
@TestCase
public func parentMacroMethods() {
let u = User()
u._id = "m-1"
u._createdAt = "2025-02-02"
u._parentAlias = "alias-m"
// 宏生成的导出方法:子类实例作父类参数
let m = BaseUser.exportJsonFields(u)
@Expect(toStr(m["_id"]), "m-1")
@Expect(toStr(m["_createdAt"]), "2025-02-02")
@Expect(toStr(m["parent_alias"]), "alias-m")
// @JsonIgnore 父类字段不导出
@Expect(m.contains("_temp"), false)
// 宏生成的导入方法
var json = HashMap<String, Any>()
json["_id"] = "m-2"
json["parent_alias"] = "alias-m2"
BaseUser.importJsonFields(u, json)
@Expect(u._id, "m-2")
@Expect(u._parentAlias, "alias-m2")
@Expect(u._createdAt, "2025-02-02") // 未传的键保持原值
}
/// 多层继承:爷标宏、中间层不标宏 → 爷字段导出,中间层字段不导出
@TestCase
public func multiLevelInheritance() {
let leaf = LeafChild()
leaf._gp = "gp-v"
leaf._mp = "mp-v"
leaf._lc = "lc-v"
let json = JsonSerializer.Serialize(leaf)
@Expect(json.contains("\"_gp\": \"gp-v\""), true) // 爷字段(宏)导出
@Expect(json.contains("_mp"), false) // 中间层无宏不导出
@Expect(json.contains("\"_lc\": \"lc-v\""), true) // 子字段(反射)导出
let back = JsonSerializer.Deserialize<LeafChild>(json)
@Expect(back._gp, "gp-v")
@Expect(back._lc, "lc-v")
@Expect(back._mp, "") // 中间层字段保持默认值
}
/// 宏自动补 openGrandParent 未显式写 open@JsonParent 自动补),继承链可正常编译
@TestCase
public func parentAutoOpen() {
let leaf = LeafChild()
leaf._gp = "auto"
let json = JsonSerializer.Serialize(leaf)
@Expect(json.contains("\"_gp\": \"auto\""), true)
}
}
+27
View File
@@ -103,6 +103,7 @@ public class JsonWriter {
private static func writeObject(value: Any, ct: ClassTypeInfo, options: JsonOption, depth: Int64): JsonValue {
let obj = JsonObject()
// 1) 子类自身字段(反射)
let fields = ReflectionCache.getFields(ct, options)
for (f in fields) {
// 只要不是 @JsonIgnore 的字段一律输出;None 字段输出 null
@@ -112,9 +113,35 @@ public class JsonWriter {
let raw = f.variable.getOrThrow().getValue(value)
obj.put(f.jsonName, writeValue(raw, options, depth + 1))
}
// 2) 父类字段(@JsonParent 宏生成的静态导出方法,沿继承链逐层调用)
writeParentFields(value, ct, obj, options, depth)
obj
}
/// 沿 superClass 链调用每层的 exportJsonFields 静态方法并合并进 JSON。
/// 无宏的父类(hasExportFields 缓存判断)静默跳过,继续向祖父层查找。
private static func writeParentFields(value: Any, ct: ClassTypeInfo, obj: JsonObject, options: JsonOption,
depth: Int64): Unit {
var parent = ct.superClass
while (let Some(p) <- parent) {
if (ReflectionCache.hasExportFields(p)) {
try {
let f = p.getStaticFunction("exportJsonFields", [p])
let args: Array<Any> = [value] // 子类实例作父类类型参数
let m = f.apply(p, args)
if (let h: HashMap<String, Any> <- m) {
for ((k, v) in h) {
obj.put(k, writeValue(v, options, depth + 1))
}
}
} catch (_: Exception) {
// 调用失败(不应发生)→ 跳过该层
}
}
parent = p.superClass
}
}
/// 反射迭代集合:优先 toArray 转数组再 get(index);无 toArray(如 Array)直接用原集合
private static func forEachElement(collection: Any, action: (Any) -> Unit): Unit {
let ti = TypeInfo.of(collection)
+38
View File
@@ -40,6 +40,44 @@ public class ReflectionCache {
private static let _cache = HashMap<String, ArrayList<FieldMetadata>>()
/// 类型 → 是否有 @JsonParent 宏生成的 exportJsonFieldsBool
private static let _exportCache = HashMap<String, Bool>()
/// 类型 → 是否有 @JsonParent 宏生成的 importJsonFieldsBool
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 目标类类型。