refactor: 新增 common 子包——XML/JSON 共用部分 + 单 import 全量可用
- src/common/(package simapi_serialization.common): Annotations.cj(@SerializerPropertyName/@SerializerIgnore)、 ReflectionCache.cj(字段元数据 + 继承链缓存)、NamingPolicy.cj(PropertyNamingPolicy) - 根锚点 public import common.* + json.*:import simapi_serialization.* 即可用全部 - JsonOption.propertyNamingPolicy 引用 common 枚举;getFields 改收命名策略 - 枚举值 PropertyNamingPolicy.None → Keep(None 与内置 Option.None 构造器同名冲突) - JsonWriter/JsonReader 单类 import common,避免循环依赖 - 测试/独立项目验证:16 用例全绿;serialization_test 单 import 即可运行
This commit is contained in:
+11
-3
@@ -13,6 +13,8 @@ import std.collection.*
|
||||
import std.convert.*
|
||||
import std.reflect.*
|
||||
import stdx.encoding.json.*
|
||||
import simapi_serialization.common.ReflectionCache
|
||||
import simapi_serialization.common.FieldMetadata
|
||||
|
||||
/**
|
||||
* JsonValue → 对象 读取器。
|
||||
@@ -64,7 +66,8 @@ public class JsonReader {
|
||||
let placeholder = defaultValueFor(innerName)
|
||||
return ctor.apply([placeholder])
|
||||
}
|
||||
return None
|
||||
let noneVal: ?Any = None
|
||||
return noneVal
|
||||
}
|
||||
let innerName = JsonWriter.extractTypeArgs(typeName)[0]
|
||||
let innerVal = readValue(value, TypeInfo.get(innerName), options, depth + 1)
|
||||
@@ -114,7 +117,7 @@ public class JsonReader {
|
||||
|
||||
private static func readObject(value: JsonValue, ct: ClassTypeInfo, options: JsonOption, depth: Int64): Any {
|
||||
let instance = ct.construct([])
|
||||
let fields = ReflectionCache.getFields(ct, options)
|
||||
let fields = ReflectionCache.getFields(ct, options.propertyNamingPolicy)
|
||||
let jobj = value.asObject()
|
||||
for (f in fields) {
|
||||
if (f.ignore) {
|
||||
@@ -221,7 +224,7 @@ public class JsonReader {
|
||||
/// JsonValue → 通用 Any(动态结构)
|
||||
private static func jsonToAny(v: JsonValue): Any {
|
||||
match (v.kind()) {
|
||||
case JsNull => None
|
||||
case JsNull => noneAny()
|
||||
case JsBool => v.asBool().getValue()
|
||||
case JsInt => v.asInt().getValue()
|
||||
case JsFloat => v.asFloat().getValue()
|
||||
@@ -241,6 +244,11 @@ public class JsonReader {
|
||||
}
|
||||
}
|
||||
|
||||
private static func noneAny(): Any {
|
||||
let noneVal: ?Any = None
|
||||
noneVal
|
||||
}
|
||||
|
||||
// ===== 宽松类型转换 =====
|
||||
|
||||
private static func readString(v: JsonValue): String {
|
||||
|
||||
Reference in New Issue
Block a user