@@ -17,7 +17,7 @@ import simapi_serialization.*
import simapi_serialization.macros.*
enum Role {
| Admin
| Us er
| Memb er
| Guest
}
@@ -44,7 +44,7 @@ class User <: BaseUser {
public var _age: Int64 = 0 // 下划线字段
public var score: Float64 = 0.0 // 无下划线
public var active: Bool = true // 无下划线
public var _role: Role = Role.Us er // 下划线字段(枚举)
public var _role: Role = Role.Memb er // 下划线字段(枚举)
public var opt: ?String = None // 无下划线(Option=Some)
public var remark: ?String = None // 无下划线(Option=None → null)
public var _tags: Array<String> = [] // 下划线字段(集合)
@@ -109,7 +109,7 @@ func makeUser(): User {
func roleStr(r: Role): String {
match (r) {
case Role.Admin => "Admin"
case Role.Us er => "User"
case Role.Memb er => "User"
case Role.Guest => "Guest"
}
}
@@ -124,7 +124,7 @@ func toStr(x: Any): String {
public class JsonSerializerTests {
/// 基础类型序列化
@TestCase
public func basicTypes() {
public func basicTypes(): Unit {
@Expect(JsonSerializer.Serialize(Int64(42)), "42")
@Expect(JsonSerializer.Serialize(Int32(7)), "7")
@Expect(JsonSerializer.Serialize(Int16(3)), "3")
@@ -137,7 +137,7 @@ public class JsonSerializerTests {
/// 对象序列化:下划线保留、注解生效、忽略生效、None→null、父类字段不输出
@TestCase
public func objectSerialize() {
public func objectSerialize(): Unit {
let json = JsonSerializer.Serialize(makeUser())
// 下划线字段原样输出
@Expect(json.contains("\"_name\": \"alice\""), true)
@@ -171,7 +171,7 @@ public class JsonSerializerTests {
/// 对象反序列化往返
@TestCase
public func objectRoundTrip() {
public func objectRoundTrip(): Unit {
let json = JsonSerializer.Serialize(makeUser())
let u2 = JsonSerializer.Deserialize<User>(json)
@Expect(u2._name, "alice")
@@ -203,7 +203,7 @@ public class JsonSerializerTests {
/// Array 往返
@TestCase
public func arrayRoundTrip() {
public func arrayRoundTrip(): Unit {
let json = JsonSerializer.Serialize(["x", "y", "z"])
let arrBack = JsonSerializer.Deserialize<Array<String>>(json)
@Expect(arrBack.size, Int64(3))
@@ -213,7 +213,7 @@ public class JsonSerializerTests {
/// ArrayList 往返
@TestCase
public func arrayListRoundTrip() {
public func arrayListRoundTrip(): Unit {
var list = ArrayList<String>()
list.add("a")
list.add("b")
@@ -226,7 +226,7 @@ public class JsonSerializerTests {
/// HashSet 往返
@TestCase
public func hashSetRoundTrip() {
public func hashSetRoundTrip(): Unit {
var set = HashSet<String>()
set.add("x")
set.add("y")
@@ -238,7 +238,7 @@ public class JsonSerializerTests {
/// HashMap<String, V> 往返
@TestCase
public func hashMapRoundTrip() {
public func hashMapRoundTrip(): Unit {
let m = HashMap<String, Any>()
m["k1"] = "v1"
m["k2"] = Int64(42)
@@ -250,7 +250,7 @@ public class JsonSerializerTests {
/// 命名策略:NONE / CAMEL / SNAKE 对当前模型(全小写+下划线)输出一致
@TestCase
public func namingPolicies() {
public func namingPolicies(): Unit {
let optNone = JsonOption()
optNone.propertyNamingPolicy = PropertyNamingPolicy.Keep
let jsonNone = JsonSerializer.Serialize(makeUser(), optNone)
@@ -270,7 +270,7 @@ public class JsonSerializerTests {
/// 命名策略转换规则(直接单测)
@TestCase
public func applyNamingPolicy() {
public func applyNamingPolicy(): Unit {
// SnakeCase:大写转 _小写
@Expect(ReflectionCache.applyNamingPolicy("userName", PropertyNamingPolicy.SnakeCase), "user_name")
@Expect(ReflectionCache.applyNamingPolicy("userNameAge", PropertyNamingPolicy.SnakeCase), "user_name_age")
@@ -285,7 +285,7 @@ public class JsonSerializerTests {
/// 枚举序列化/反序列化
@TestCase
public func enumSerialize() {
public func enumSerialize(): Unit {
@Expect(JsonSerializer.Serialize(Role.Admin), "\"Admin\"")
@Expect(JsonSerializer.Serialize(Role.Guest), "\"Guest\"")
let u = User()
@@ -296,7 +296,7 @@ public class JsonSerializerTests {
/// Option 字段:null → None,值 → Some
@TestCase
public func optionField() {
public func optionField(): Unit {
let none: ?String = JsonSerializer.Deserialize<?String>("null")
@Expect(none.isNone(), true)
let some: ?String = JsonSerializer.Deserialize<?String>("\"hi\"")
@@ -308,7 +308,7 @@ public class JsonSerializerTests {
/// 宽松类型转换:JSON 数字 ↔ 字符串字段互转
@TestCase
public func looseConversion() {
public func looseConversion(): Unit {
// 数字 → String 字段
let s: String = JsonSerializer.Deserialize<String>("42")
@Expect(s, "42")
@@ -325,7 +325,7 @@ public class JsonSerializerTests {
/// 深度限制:超过 maxDepth 抛异常
@TestCase
public func maxDepth() {
public func maxDepth(): Unit {
let opt = JsonOption()
opt.maxDepth = 0
let u = User()
@@ -341,7 +341,7 @@ public class JsonSerializerTests {
/// 父类字段序列化/反序列化(@SerializerParent 宏):已并入 objectSerialize/objectRoundTrip,
/// 此处验证宏生成的静态方法可直接调用
@TestCase
public func parentMacroMethods() {
public func parentMacroMethods(): Unit {
let u = User()
u._id = "m-1"
u._createdAt = "2025-02-02"
@@ -365,7 +365,7 @@ public class JsonSerializerTests {
/// 多层继承:爷标宏、中间层不标宏 → 爷字段导出,中间层字段不导出
@TestCase
public func multiLevelInheritance() {
public func multiLevelInheritance(): Unit {
let leaf = LeafChild()
leaf._gp = "gp-v"
leaf._mp = "mp-v"
@@ -384,7 +384,7 @@ public class JsonSerializerTests {
/// 宏自动补 open: GrandParent 未显式写 open( @SerializerParent 自动补),继承链可正常编译
@TestCase
public func parentAutoOpen() {
public func parentAutoOpen(): Unit {
let leaf = LeafChild()
leaf._gp = "auto"
let json = JsonSerializer.Serialize(leaf)
@@ -393,7 +393,7 @@ public class JsonSerializerTests {
/// 三级继承(全标宏):爷/父/子 每层字段都导出、反序列化全部回填
@TestCase
public func threeLevelAllMacro() {
public func threeLevelAllMacro(): Unit {
let leaf = LeafAll()
leaf._ga = "ga-v"
leaf._ma = "ma-v"
@@ -412,7 +412,7 @@ public class JsonSerializerTests {
/// 三级继承:每层字段互不遮蔽(字段名不同),导出顺序无关,值各自正确
@TestCase
public func threeLevelFieldSeparation() {
public func threeLevelFieldSeparation(): Unit {
let leaf = LeafAll()
leaf._ga = "G"
leaf._ma = "M"