feat: SimApiSerialization 全反射 JSON 序列化库 v1
对齐 .NET System.Text.Json: - JsonSerializer.Serialize(obj, JsonOption) / Deserialize<T>(jsonString, JsonOption) - @JsonPropertyName / @JsonIgnore 特性(运行时注解反射) - JsonOption:PropertyNamingPolicy(None/CamelCase/SnakeCase)、ignoreNull、maxDepth - 全反射实现:基础类型/枚举/Option/Array/ArrayList/HashSet/HashMap/嵌套对象 - 仓颉特性适配: * Option 是泛型枚举 → EnumTypeInfo.destruct/construct 处理 * 集合 get(i) 返回 Option<T> → Some/None 解包迭代 * 集合泛型不变 → 反射方法调用(toArray/get/add/keys)而非 cast * as 返回 Option → ?? 解包;JsonKind 用 is 判断 - 已知限制:反序列化需无参构造+var 字段;父类字段不序列化(平台严格类型校验); 枚举按名字;循环引用用深度上限保护 - 已实测:User 完整回环 + 数组/HashMap 回环 + 特性生效
This commit is contained in:
@@ -0,0 +1,243 @@
|
||||
/*
|
||||
* Copyright (c) 2025 SimcuTeam. All rights reserved.
|
||||
* JsonReader:JsonValue → 对象(全反射)。
|
||||
*
|
||||
* - 目标类型来自泛型 T(TypeInfo.of<T>())
|
||||
* - 反序列化要求:无参构造 + var 字段(let 只读字段跳过)
|
||||
* - 类型不匹配做宽松转换(JsonInt/JsonFloat/JsonString 互转)
|
||||
*/
|
||||
|
||||
package simapi_serialization
|
||||
|
||||
import std.collection.*
|
||||
import std.convert.*
|
||||
import std.reflect.*
|
||||
import stdx.encoding.json.*
|
||||
|
||||
/**
|
||||
* JsonValue → 对象 读取器。
|
||||
*/
|
||||
public class JsonReader {
|
||||
private init() {}
|
||||
|
||||
public static func read<T>(value: JsonValue, options: JsonOption): T {
|
||||
let result = readValue(value, TypeInfo.of<T>(), options, 0)
|
||||
if (let t: T <- result) {
|
||||
return t
|
||||
}
|
||||
throw Exception("JsonSerializer: 反序列化结果类型不匹配")
|
||||
}
|
||||
|
||||
private static func readValue(value: JsonValue, typeInfo: TypeInfo, options: JsonOption, depth: Int64): Any {
|
||||
if (depth > options.maxDepth) {
|
||||
throw Exception("JsonSerializer: 超过最大递归深度 ${options.maxDepth}")
|
||||
}
|
||||
let typeName = typeInfo.toString()
|
||||
|
||||
// 基本类型
|
||||
match (typeName) {
|
||||
case "String" => return readString(value)
|
||||
case "Bool" => return readBool(value)
|
||||
case "Int64" => return readInt64(value)
|
||||
case "Int32" => return Int32(readInt64(value))
|
||||
case "Int16" => return Int16(readInt64(value))
|
||||
case "Int8" => return Int8(readInt64(value))
|
||||
case "UInt8" => return UInt8(readInt64(value))
|
||||
case "UInt16" => return UInt16(readInt64(value))
|
||||
case "UInt32" => return UInt32(readInt64(value))
|
||||
case "UInt64" => return UInt64(readInt64(value))
|
||||
case "Float64" => return readFloat64(value)
|
||||
case "Float32" => return Float32(readFloat64(value))
|
||||
case "Rune" => return readChar(value)
|
||||
case _ => ()
|
||||
}
|
||||
|
||||
// Option<X>:null → None;否则读取内层并用枚举构造器包 Some
|
||||
if (typeName.startsWith("Option<")) {
|
||||
if (value is JsonNull) {
|
||||
return None
|
||||
}
|
||||
let innerName = JsonWriter.extractTypeArgs(typeName)[0]
|
||||
let innerVal = readValue(value, TypeInfo.get(innerName), options, depth + 1)
|
||||
if (let et: EnumTypeInfo <- typeInfo) {
|
||||
let ctor = et.getConstructor("Some", argsCount: 1)
|
||||
return ctor.apply([innerVal])
|
||||
}
|
||||
return Some(innerVal)
|
||||
}
|
||||
|
||||
// 集合
|
||||
if (typeName.startsWith("Array<")) {
|
||||
let elemName = JsonWriter.extractTypeArgs(typeName)[0]
|
||||
return readArray(value, elemName, options, depth)
|
||||
}
|
||||
if (typeName.startsWith("std.collection.ArrayList<")) {
|
||||
let elemName = JsonWriter.extractTypeArgs(typeName)[0]
|
||||
return readArrayList(value, elemName, options, depth)
|
||||
}
|
||||
if (typeName.startsWith("std.collection.HashSet<")) {
|
||||
let elemName = JsonWriter.extractTypeArgs(typeName)[0]
|
||||
return readHashSet(value, elemName, options, depth)
|
||||
}
|
||||
if (typeName.startsWith("std.collection.HashMap<")) {
|
||||
let args = JsonWriter.extractTypeArgs(typeName)
|
||||
return readHashMap(value, args[0], args[1], options, depth)
|
||||
}
|
||||
|
||||
// Any / Object → 通用读取
|
||||
if (typeName == "Any" || typeName == "Object") {
|
||||
return jsonToAny(value)
|
||||
}
|
||||
|
||||
// 枚举:按构造器名创建
|
||||
if (let et: EnumTypeInfo <- typeInfo) {
|
||||
let name = readString(value)
|
||||
return et.construct(name, [])
|
||||
}
|
||||
|
||||
// 对象:反射创建 + 赋值
|
||||
if (let ct: ClassTypeInfo <- typeInfo) {
|
||||
return readObject(value, ct, options, depth)
|
||||
}
|
||||
|
||||
throw Exception("JsonSerializer: 不支持的目标类型 ${typeName}")
|
||||
}
|
||||
|
||||
private static func readObject(value: JsonValue, ct: ClassTypeInfo, options: JsonOption, depth: Int64): Any {
|
||||
let instance = ct.construct([])
|
||||
let fields = ReflectionCache.getFields(ct, options)
|
||||
let jobj = value.asObject()
|
||||
for (f in fields) {
|
||||
if (f.ignore) {
|
||||
continue
|
||||
}
|
||||
if (!f.mutable) {
|
||||
continue
|
||||
}
|
||||
match (jobj.get(f.jsonName)) {
|
||||
case Some(jv) =>
|
||||
if (jv is JsonNull) {
|
||||
continue // null:跳过,保持字段默认值
|
||||
}
|
||||
let fieldVal = readValue(jv, f.typeInfo.getOrThrow(), options, depth + 1)
|
||||
f.variable.getOrThrow().setValue(instance, fieldVal)
|
||||
case None => ()
|
||||
}
|
||||
}
|
||||
instance
|
||||
}
|
||||
|
||||
/// Array<X>:经 ArrayList<X> 构造 + add + toArray
|
||||
private static func readArray(value: JsonValue, elemName: String, options: JsonOption, depth: Int64): Any {
|
||||
let listTypeName = "std.collection.ArrayList<${elemName}>"
|
||||
let listCt = (TypeInfo.get(listTypeName) as ClassTypeInfo) ?? throw Exception("JsonSerializer: 无法解析类型 ${listTypeName}")
|
||||
let list = listCt.construct([])
|
||||
let addFunc = listCt.getInstanceFunction("add", [TypeInfo.get(elemName)])
|
||||
let jarr = value.asArray()
|
||||
for (item in jarr.getItems()) {
|
||||
let elem = readValue(item, TypeInfo.get(elemName), options, depth + 1)
|
||||
addFunc.apply(list, [elem])
|
||||
}
|
||||
let toArrayFunc = listCt.getInstanceFunction("toArray", [])
|
||||
toArrayFunc.apply(list, [])
|
||||
}
|
||||
|
||||
private static func readArrayList(value: JsonValue, elemName: String, options: JsonOption, depth: Int64): Any {
|
||||
let ct = (TypeInfo.get("std.collection.ArrayList<${elemName}>") as ClassTypeInfo) ?? throw Exception("JsonSerializer: 无法解析 ArrayList<${elemName}>")
|
||||
let list = ct.construct([])
|
||||
let addFunc = ct.getInstanceFunction("add", [TypeInfo.get(elemName)])
|
||||
let jarr = value.asArray()
|
||||
for (item in jarr.getItems()) {
|
||||
let elem = readValue(item, TypeInfo.get(elemName), options, depth + 1)
|
||||
addFunc.apply(list, [elem])
|
||||
}
|
||||
list
|
||||
}
|
||||
|
||||
private static func readHashSet(value: JsonValue, elemName: String, options: JsonOption, depth: Int64): Any {
|
||||
let ct = (TypeInfo.get("std.collection.HashSet<${elemName}>") as ClassTypeInfo) ?? throw Exception("JsonSerializer: 无法解析 HashSet<${elemName}>")
|
||||
let set = ct.construct([])
|
||||
let addFunc = ct.getInstanceFunction("add", [TypeInfo.get(elemName)])
|
||||
let jarr = value.asArray()
|
||||
for (item in jarr.getItems()) {
|
||||
let elem = readValue(item, TypeInfo.get(elemName), options, depth + 1)
|
||||
addFunc.apply(set, [elem])
|
||||
}
|
||||
set
|
||||
}
|
||||
|
||||
private static func readHashMap(value: JsonValue, keyName: String, valName: String, options: JsonOption,
|
||||
depth: Int64): Any {
|
||||
let ct = (TypeInfo.get("std.collection.HashMap<${keyName}, ${valName}>") as ClassTypeInfo) ?? throw Exception("JsonSerializer: 无法解析 HashMap<${keyName}, ${valName}>")
|
||||
let map = ct.construct([])
|
||||
let addFunc = ct.getInstanceFunction("add", [TypeInfo.get(keyName), TypeInfo.get(valName)])
|
||||
let jobj = value.asObject()
|
||||
for ((k, jv) in jobj.getFields()) {
|
||||
let keyVal = readValue(JsonString(k), TypeInfo.get(keyName), options, depth + 1)
|
||||
let valVal = readValue(jv, TypeInfo.get(valName), options, depth + 1)
|
||||
addFunc.apply(map, [keyVal, valVal])
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
/// JsonValue → 通用 Any(动态结构)
|
||||
private static func jsonToAny(v: JsonValue): Any {
|
||||
match (v.kind()) {
|
||||
case JsNull => None
|
||||
case JsBool => v.asBool().getValue()
|
||||
case JsInt => v.asInt().getValue()
|
||||
case JsFloat => v.asFloat().getValue()
|
||||
case JsString => v.asString().getValue()
|
||||
case JsArray =>
|
||||
var list = ArrayList<Any>()
|
||||
for (item in v.asArray().getItems()) {
|
||||
list.add(jsonToAny(item))
|
||||
}
|
||||
list.toArray()
|
||||
case JsObject =>
|
||||
var map = HashMap<String, Any>()
|
||||
for ((k, val) in v.asObject().getFields()) {
|
||||
map[k] = jsonToAny(val)
|
||||
}
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 宽松类型转换 =====
|
||||
|
||||
private static func readString(v: JsonValue): String {
|
||||
if (let s: JsonString <- v) { return s.getValue() }
|
||||
if (let i: JsonInt <- v) { return "${i.getValue()}" }
|
||||
if (let f: JsonFloat <- v) { return "${f.getValue()}" }
|
||||
if (let b: JsonBool <- v) { return "${b.getValue()}" }
|
||||
throw Exception("JsonSerializer: 无法转换为 String")
|
||||
}
|
||||
|
||||
private static func readInt64(v: JsonValue): Int64 {
|
||||
if (let i: JsonInt <- v) { return i.getValue() }
|
||||
if (let f: JsonFloat <- v) { return Int64(f.getValue()) }
|
||||
if (let s: JsonString <- v) { return Int64.parse(s.getValue()) }
|
||||
throw Exception("JsonSerializer: 无法转换为 Int64")
|
||||
}
|
||||
|
||||
private static func readFloat64(v: JsonValue): Float64 {
|
||||
if (let f: JsonFloat <- v) { return f.getValue() }
|
||||
if (let i: JsonInt <- v) { return Float64(i.getValue()) }
|
||||
if (let s: JsonString <- v) { return Float64.parse(s.getValue()) }
|
||||
throw Exception("JsonSerializer: 无法转换为 Float64")
|
||||
}
|
||||
|
||||
private static func readBool(v: JsonValue): Bool {
|
||||
if (let b: JsonBool <- v) { return b.getValue() }
|
||||
if (let s: JsonString <- v) { return s.getValue() == "true" }
|
||||
throw Exception("JsonSerializer: 无法转换为 Bool")
|
||||
}
|
||||
|
||||
private static func readChar(v: JsonValue): Rune {
|
||||
let s = readString(v)
|
||||
for (r in s.runes()) {
|
||||
return r
|
||||
}
|
||||
throw Exception("JsonSerializer: 无法转换为 Char")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user