feat: 新增 TypeInfo 版反序列化入口——按运行时类型反序列化
- JsonSerializer.Deserialize(typeInfo, jsonString[, options]): Any - JsonReader.read(typeInfo, value, options): Any - 供框架运行时模型绑定等场景使用(无需编译期泛型)
This commit is contained in:
@@ -2,8 +2,7 @@
|
|||||||
cjc-version = "1.1.3"
|
cjc-version = "1.1.3"
|
||||||
name = "simapi_serialization"
|
name = "simapi_serialization"
|
||||||
description = "SimApi 全反射 JSON 序列化库(对齐 .NET JsonSerializer.Serialize/Deserialize + SerializerPropertyName/SerializerIgnore 特性)"
|
description = "SimApi 全反射 JSON 序列化库(对齐 .NET JsonSerializer.Serialize/Deserialize + SerializerPropertyName/SerializerIgnore 特性)"
|
||||||
version = "1.0.0"
|
version = "1.0.3"
|
||||||
org = "simcu"
|
|
||||||
target-dir = ""
|
target-dir = ""
|
||||||
output-type = "static"
|
output-type = "static"
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,17 @@ public class JsonReader {
|
|||||||
throw Exception("JsonSerializer: 反序列化结果类型不匹配")
|
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 {
|
private static func readValue(value: JsonValue, typeInfo: TypeInfo, options: JsonOption, depth: Int64): Any {
|
||||||
if (depth > options.maxDepth) {
|
if (depth > options.maxDepth) {
|
||||||
throw Exception("JsonSerializer: 超过最大递归深度 ${options.maxDepth}")
|
throw Exception("JsonSerializer: 超过最大递归深度 ${options.maxDepth}")
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
package simapi_serialization.json
|
package simapi_serialization.json
|
||||||
|
|
||||||
|
import std.reflect.*
|
||||||
import stdx.encoding.json.*
|
import stdx.encoding.json.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,4 +52,23 @@ public class JsonSerializer {
|
|||||||
public static func Deserialize<T>(jsonString: String): T {
|
public static func Deserialize<T>(jsonString: String): T {
|
||||||
Deserialize<T>(jsonString, JsonOption.instance)
|
Deserialize<T>(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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user