From c6269df95746dfb2b640083e60084db0321ec8bf Mon Sep 17 00:00:00 2001 From: xRain Date: Tue, 18 Aug 2026 00:27:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20TypeInfo=20?= =?UTF-8?q?=E7=89=88=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E5=85=A5=E5=8F=A3?= =?UTF-8?q?=E2=80=94=E2=80=94=E6=8C=89=E8=BF=90=E8=A1=8C=E6=97=B6=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - JsonSerializer.Deserialize(typeInfo, jsonString[, options]): Any - JsonReader.read(typeInfo, value, options): Any - 供框架运行时模型绑定等场景使用(无需编译期泛型) --- cjpm.toml | 3 +-- src/json/JsonReader.cj | 11 +++++++++++ src/json/JsonSerializer.cj | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/cjpm.toml b/cjpm.toml index c4ce4cb..927dc93 100644 --- a/cjpm.toml +++ b/cjpm.toml @@ -2,8 +2,7 @@ cjc-version = "1.1.3" name = "simapi_serialization" description = "SimApi 全反射 JSON 序列化库(对齐 .NET JsonSerializer.Serialize/Deserialize + SerializerPropertyName/SerializerIgnore 特性)" - version = "1.0.0" - org = "simcu" + version = "1.0.3" target-dir = "" output-type = "static" diff --git a/src/json/JsonReader.cj b/src/json/JsonReader.cj index b37cfbf..fd08750 100644 --- a/src/json/JsonReader.cj +++ b/src/json/JsonReader.cj @@ -30,6 +30,17 @@ public class JsonReader { throw Exception("JsonSerializer: 反序列化结果类型不匹配") } + /** + * 按运行时类型信息反序列化(供框架运行时绑定等场景使用,无需编译期泛型)。 + * @param typeInfo 目标类型(运行时)。 + * @param value JSON 值。 + * @param options 序列化选项。 + * @return 反序列化后的实例(Any)。 + */ + public static func read(typeInfo: TypeInfo, value: JsonValue, options: JsonOption): Any { + readValue(value, typeInfo, options, 0) + } + private static func readValue(value: JsonValue, typeInfo: TypeInfo, options: JsonOption, depth: Int64): Any { if (depth > options.maxDepth) { throw Exception("JsonSerializer: 超过最大递归深度 ${options.maxDepth}") diff --git a/src/json/JsonSerializer.cj b/src/json/JsonSerializer.cj index aff2d9f..e6e05ee 100644 --- a/src/json/JsonSerializer.cj +++ b/src/json/JsonSerializer.cj @@ -9,6 +9,7 @@ package simapi_serialization.json +import std.reflect.* import stdx.encoding.json.* /** @@ -51,4 +52,23 @@ public class JsonSerializer { public static func Deserialize(jsonString: String): T { Deserialize(jsonString, JsonOption.instance) } + + /** + * 按运行时类型信息反序列化(供框架运行时绑定等场景使用,无需编译期泛型)。 + * @param typeInfo 目标类型(运行时)。 + * @param jsonString JSON 字符串。 + * @param options 序列化选项。 + * @return 反序列化后的实例(Any)。 + */ + public static func Deserialize(typeInfo: TypeInfo, jsonString: String, options: JsonOption): Any { + let value = JsonValue.fromStr(jsonString) + JsonReader.read(typeInfo, value, options) + } + + /** + * 按运行时类型信息反序列化(默认选项)。 + */ + public static func Deserialize(typeInfo: TypeInfo, jsonString: String): Any { + Deserialize(typeInfo, jsonString, JsonOption.instance) + } }