From 67da60429402fb8de5a863a698fa596b063eb38f Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 17 Aug 2026 11:21:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20ignoreNull=20?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E2=80=94=E2=80=94=E9=9D=9E=20@JsonIgnore=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E4=B8=80=E5=BE=8B=E8=BE=93=E5=87=BA=EF=BC=8C?= =?UTF-8?q?None=20=E8=BE=93=E5=87=BA=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - JsonOption 删除 ignoreNull - JsonWriter.writeObject 不再按 null 跳过字段(只跳过 @JsonIgnore) - 删除不再使用的 isNoneValue - 测试:remark(None)→null 且保留;temp(@JsonIgnore) 唯一被排除 --- src/json/JsonOption.cj | 3 --- src/json/JsonWriter.cj | 13 +------------ 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/src/json/JsonOption.cj b/src/json/JsonOption.cj index a12fa19..d7eb61a 100644 --- a/src/json/JsonOption.cj +++ b/src/json/JsonOption.cj @@ -24,9 +24,6 @@ public class JsonOption { /// 属性命名策略(默认 CamelCase) public var propertyNamingPolicy: PropertyNamingPolicy = PropertyNamingPolicy.CamelCase - /// 序列化时是否忽略 null/None 字段(默认 false:None 输出 null) - public var ignoreNull: Bool = false - /// 枚举序列化为名字(默认 true;false 暂按名字输出,数字模式后续支持) public var enumAsString: Bool = true diff --git a/src/json/JsonWriter.cj b/src/json/JsonWriter.cj index 1245541..f44b925 100644 --- a/src/json/JsonWriter.cj +++ b/src/json/JsonWriter.cj @@ -105,13 +105,11 @@ public class JsonWriter { let obj = JsonObject() let fields = ReflectionCache.getFields(ct, options) for (f in fields) { + // 只要不是 @JsonIgnore 的字段一律输出;None 字段输出 null if (f.ignore) { continue } let raw = f.variable.getOrThrow().getValue(value) - if (options.ignoreNull && isNoneValue(raw)) { - continue - } obj.put(f.jsonName, writeValue(raw, options, depth + 1)) } obj @@ -151,15 +149,6 @@ public class JsonWriter { } } - /// 判断 Any 值是否为 None(Option 枚举的 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 private static func unwrapOptionValue(v: Any): ?Any { if (let et: EnumTypeInfo <- TypeInfo.of(v)) {