refactor: 注解与宏改名——Serializer 前缀统一命名
- @JsonPropertyName → @SerializerPropertyName - @JsonIgnore → @SerializerIgnore - @JsonParent → @SerializerParent(宏文件 JsonParentMacro.cj → SerializerParentMacro.cj) - 同步更新:JsonAnnotations、ReflectionCache、宏内部注解判断、 测试、方案文档、serialization_test 独立项目 - 16 用例全绿
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||||
* JsonSerializer 单元测试(cjpm test)。
|
||||
* 覆盖:基础类型 / 对象字段输出 / 注解(JsonPropertyName、JsonIgnore)/ Option /
|
||||
* 覆盖:基础类型 / 对象字段输出 / 注解(SerializerPropertyName、SerializerIgnore)/ Option /
|
||||
* 集合(Array、ArrayList、HashSet)/ HashMap / 枚举 / 命名策略 /
|
||||
* 继承(平台限制)/ maxDepth / 宽松类型转换。
|
||||
*
|
||||
@@ -27,15 +27,15 @@ class Address {
|
||||
public var zip: String = "" // 无下划线 → 原样输出 zip
|
||||
}
|
||||
|
||||
// 继承测试:父类标注 @JsonParent 宏 → 自动补 open + 生成导出/导入静态方法,
|
||||
// 继承测试:父类标注 @SerializerParent 宏 → 自动补 open + 生成导出/导入静态方法,
|
||||
// 父类字段(_id/_createdAt)经宏的静态类型访问绕开反射声明类校验
|
||||
@JsonParent
|
||||
@SerializerParent
|
||||
open class BaseUser {
|
||||
public var _id: String = ""
|
||||
public var _createdAt: String = ""
|
||||
@JsonIgnore
|
||||
@SerializerIgnore
|
||||
public var _temp: String = ""
|
||||
@JsonPropertyName["parent_alias"]
|
||||
@SerializerPropertyName["parent_alias"]
|
||||
public var _parentAlias: String = ""
|
||||
}
|
||||
|
||||
@@ -49,14 +49,14 @@ class User <: BaseUser {
|
||||
public var remark: ?String = None // 无下划线(Option=None → null)
|
||||
public var _tags: Array<String> = [] // 下划线字段(集合)
|
||||
public var addr: Address = Address() // 无下划线(嵌套对象)
|
||||
@JsonPropertyName["user_alias"]
|
||||
@SerializerPropertyName["user_alias"]
|
||||
public var _alias: String = "" // 下划线 + 注解覆盖
|
||||
@JsonIgnore
|
||||
@SerializerIgnore
|
||||
public var temp: String = "secret" // 无下划线 + 忽略
|
||||
}
|
||||
|
||||
// 多层继承:爷(标宏)→ 父(不标宏)→ 子
|
||||
@JsonParent
|
||||
@SerializerParent
|
||||
open class GrandParent {
|
||||
public var _gp: String = ""
|
||||
}
|
||||
@@ -129,10 +129,10 @@ public class JsonSerializerTests {
|
||||
// 无下划线字段原样输出
|
||||
@Expect(json.contains("\"score\""), true)
|
||||
@Expect(json.contains("\"active\": true"), true)
|
||||
// @JsonPropertyName 覆盖字段名
|
||||
// @SerializerPropertyName 覆盖字段名
|
||||
@Expect(json.contains("\"user_alias\": \"xiaoming\""), true)
|
||||
@Expect(json.contains("\"_alias\""), false)
|
||||
// @JsonIgnore 字段不输出
|
||||
// @SerializerIgnore 字段不输出
|
||||
@Expect(json.contains("temp"), false)
|
||||
@Expect(json.contains("\"secret\""), false)
|
||||
// Option:Some→值,None→null
|
||||
@@ -143,13 +143,13 @@ public class JsonSerializerTests {
|
||||
// 嵌套对象
|
||||
@Expect(json.contains("\"_city\": \"beijing\""), true)
|
||||
@Expect(json.contains("\"zip\": \"100000\""), true)
|
||||
// 父类字段(@JsonParent 宏):_id/_createdAt 输出,@JsonIgnore 父类字段不输出
|
||||
// 父类字段(@SerializerParent 宏):_id/_createdAt 输出,@SerializerIgnore 父类字段不输出
|
||||
@Expect(json.contains("\"_id\": \"u-1\""), true)
|
||||
@Expect(json.contains("\"_createdAt\": \"2025-01-01\""), true)
|
||||
// 父类 @JsonPropertyName["parent_alias"] 生效
|
||||
// 父类 @SerializerPropertyName["parent_alias"] 生效
|
||||
@Expect(json.contains("\"parent_alias\": \"parent-xiaoming\""), true)
|
||||
@Expect(json.contains("_parentAlias"), false)
|
||||
// 父类 @JsonIgnore _temp 不输出
|
||||
// 父类 @SerializerIgnore _temp 不输出
|
||||
@Expect(json.contains("secret-parent"), false)
|
||||
}
|
||||
|
||||
@@ -165,8 +165,8 @@ public class JsonSerializerTests {
|
||||
@Expect(u2.addr._city, "beijing")
|
||||
@Expect(u2.addr.zip, "100000")
|
||||
@Expect(u2._alias, "xiaoming")
|
||||
@Expect(u2.temp, "secret") // @JsonIgnore:反序列化不改动默认值
|
||||
// 父类字段往返(@JsonParent 宏)
|
||||
@Expect(u2.temp, "secret") // @SerializerIgnore:反序列化不改动默认值
|
||||
// 父类字段往返(@SerializerParent 宏)
|
||||
@Expect(u2._id, "u-1")
|
||||
@Expect(u2._createdAt, "2025-01-01")
|
||||
@Expect(u2._parentAlias, "parent-xiaoming")
|
||||
@@ -322,7 +322,7 @@ public class JsonSerializerTests {
|
||||
@Expect(threw, true)
|
||||
}
|
||||
|
||||
/// 父类字段序列化/反序列化(@JsonParent 宏):已并入 objectSerialize/objectRoundTrip,
|
||||
/// 父类字段序列化/反序列化(@SerializerParent 宏):已并入 objectSerialize/objectRoundTrip,
|
||||
/// 此处验证宏生成的静态方法可直接调用
|
||||
@TestCase
|
||||
public func parentMacroMethods() {
|
||||
@@ -335,7 +335,7 @@ public class JsonSerializerTests {
|
||||
@Expect(toStr(m["_id"]), "m-1")
|
||||
@Expect(toStr(m["_createdAt"]), "2025-02-02")
|
||||
@Expect(toStr(m["parent_alias"]), "alias-m")
|
||||
// @JsonIgnore 父类字段不导出
|
||||
// @SerializerIgnore 父类字段不导出
|
||||
@Expect(m.contains("_temp"), false)
|
||||
// 宏生成的导入方法
|
||||
var json = HashMap<String, Any>()
|
||||
@@ -366,7 +366,7 @@ public class JsonSerializerTests {
|
||||
@Expect(back._mp, "") // 中间层字段保持默认值
|
||||
}
|
||||
|
||||
/// 宏自动补 open:GrandParent 未显式写 open(@JsonParent 自动补),继承链可正常编译
|
||||
/// 宏自动补 open:GrandParent 未显式写 open(@SerializerParent 自动补),继承链可正常编译
|
||||
@TestCase
|
||||
public func parentAutoOpen() {
|
||||
let leaf = LeafChild()
|
||||
|
||||
Reference in New Issue
Block a user