refactor: 移除 ignoreNull 选项——非 @JsonIgnore 字段一律输出,None 输出 null

- JsonOption 删除 ignoreNull
- JsonWriter.writeObject 不再按 null 跳过字段(只跳过 @JsonIgnore)
- 删除不再使用的 isNoneValue
- 测试:remark(None)→null 且保留;temp(@JsonIgnore) 唯一被排除
This commit is contained in:
2026-08-17 11:21:56 +08:00
parent ff1bd51f52
commit 67da604294
2 changed files with 1 additions and 15 deletions
-3
View File
@@ -24,9 +24,6 @@ public class JsonOption {
/// 属性命名策略(默认 CamelCase) /// 属性命名策略(默认 CamelCase)
public var propertyNamingPolicy: PropertyNamingPolicy = PropertyNamingPolicy.CamelCase public var propertyNamingPolicy: PropertyNamingPolicy = PropertyNamingPolicy.CamelCase
/// 序列化时是否忽略 null/None 字段(默认 falseNone 输出 null
public var ignoreNull: Bool = false
/// 枚举序列化为名字(默认 true;false 暂按名字输出,数字模式后续支持) /// 枚举序列化为名字(默认 true;false 暂按名字输出,数字模式后续支持)
public var enumAsString: Bool = true public var enumAsString: Bool = true
+1 -12
View File
@@ -105,13 +105,11 @@ public class JsonWriter {
let obj = JsonObject() let obj = JsonObject()
let fields = ReflectionCache.getFields(ct, options) let fields = ReflectionCache.getFields(ct, options)
for (f in fields) { for (f in fields) {
// 只要不是 @JsonIgnore 的字段一律输出;None 字段输出 null
if (f.ignore) { if (f.ignore) {
continue continue
} }
let raw = f.variable.getOrThrow().getValue(value) let raw = f.variable.getOrThrow().getValue(value)
if (options.ignoreNull && isNoneValue(raw)) {
continue
}
obj.put(f.jsonName, writeValue(raw, options, depth + 1)) obj.put(f.jsonName, writeValue(raw, options, depth + 1))
} }
obj obj
@@ -151,15 +149,6 @@ public class JsonWriter {
} }
} }
/// 判断 Any 值是否为 NoneOption 枚举的 None 构造器;非 Option 值返回 false
private static func isNoneValue(v: Any): Bool {
if (let et: EnumTypeInfo <- TypeInfo.of(v)) {
let (ctor, _) = et.destruct(v)
return ctor.name == "None"
}
false
}
/// 若值为 Option 的 Some(x) 返回 Some(x)None 或非 Option 返回 None /// 若值为 Option 的 Some(x) 返回 Some(x)None 或非 Option 返回 None
private static func unwrapOptionValue(v: Any): ?Any { private static func unwrapOptionValue(v: Any): ?Any {
if (let et: EnumTypeInfo <- TypeInfo.of(v)) { if (let et: EnumTypeInfo <- TypeInfo.of(v)) {