From 9a656e60d893eaf8802e91b5d3c1ada0f705328a Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 17 Aug 2026 11:00:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20@JsonPropertyName/@JsonIgnore=20tar?= =?UTF-8?q?get=20=E6=94=B6=E7=AA=84=E4=B8=BA=E4=BB=85=20MemberVariable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 序列化/反序列化只处理 var 成员变量(instanceVariables),不处理 prop 属性; 注解标到 prop 上现在直接编译报错,避免标了不生效的静默陷阱 --- src/attributes/JsonAttributes.cj | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/attributes/JsonAttributes.cj b/src/attributes/JsonAttributes.cj index 1fe429a..d45380c 100644 --- a/src/attributes/JsonAttributes.cj +++ b/src/attributes/JsonAttributes.cj @@ -4,6 +4,10 @@ * - [JsonPropertyName("xxx")] → @JsonPropertyName["xxx"] 指定字段的 JSON 名称 * - [JsonIgnore] → @JsonIgnore 序列化/反序列化时忽略该字段 * + * 设计决策:本库序列化/反序列化只处理「var 成员变量」(std.reflect 的 instanceVariables), + * 不处理 prop 属性(instanceProperties)。因此注解 target 仅限 MemberVariable: + * 标到 prop 上会直接编译报错,避免「标了但不生效」的静默陷阱。 + * * 参考仓颉官方注解用法(std.reflect 运行时读取): * https://docs.cangjie-lang.cn/en/docs/1.0.0/libs/std/reflect/reflect_samples/annotation.html */ @@ -14,7 +18,7 @@ package simapi_serialization.attributes * 指定字段的 JSON 名称(对齐 .NET JsonPropertyNameAttribute)。 * 用法:@JsonPropertyName["user_name"] public var _username: String = "" */ -@Annotation[target: [MemberVariable, MemberProperty]] +@Annotation[target: [MemberVariable]] public class JsonPropertyName { public let name: String @@ -27,7 +31,7 @@ public class JsonPropertyName { * 标记字段不做 JSON 处理(对齐 .NET JsonIgnoreAttribute)。 * 用法:@JsonIgnore public var _temp: String = "" */ -@Annotation[target: [MemberVariable, MemberProperty]] +@Annotation[target: [MemberVariable]] public class JsonIgnore { public const init() {} }