refactor: 全框架 JSON 操作迁移到 simapi_serialization
- 依赖:新增 simapi_serialization path 依赖(保留 soulsoft_serialization 供 soulsoft web 栈传递使用) - SimApiJson.json / SimApiUtil.json → JsonSerializer.Serialize - SimApiUtil.fromJson / base64DecodeTo → JsonSerializer.Deserialize(去 ISerialization 约束) - SimApiHttpClient signQuery/aesQuery/aesSignQuery/query → 去约束,反序列化换新库 - SimApiBaseResponse/SimApiDataResponse/SimApiLoginItem/AesBodyRequest/AuthSDK DTO → 去 @Serialization 宏/去 soulsoft 接口,改纯 POCO(simapi_serialization 反射) - SimApiBaseResponse 标 @SerializerParent:子类响应序列化包含 _code/_message - SimApiResultWriter / SimApiExceptionMiddleware → 响应输出走 simapi_serialization(统一 _code/_message 格式) - SimApiRequestLogMiddleware 截断 → Deserialize<HashMap<String,Any>> + 重序列化 - SimApiAuthDto JsonValue 字段 → HashMap<String,Any>;SimApiCache.get<T> 去约束 - webdemo:AuthController readFromJson 换 Deserialize;cjpm.toml 补依赖 - 验证:webdemo 启动正常,GET/POST 接口响应格式统一,S3 存储全链路 OK
This commit is contained in:
@@ -5,23 +5,17 @@
|
||||
|
||||
package simapi.communications
|
||||
|
||||
import std.collection.*
|
||||
import stdx.encoding.json.*
|
||||
import soulsoft_serialization.*
|
||||
|
||||
/**
|
||||
* 带任意对象数据的响应(非泛型版,供响应自动封装使用)。
|
||||
*
|
||||
* 与 SimApiResponse<T> 的区别:data 为 Any(运行时类型不定),用于
|
||||
* SimApiRequestDelegateFactory 派发结果时统一包装 DTO/数组/动态结构。
|
||||
*
|
||||
* 序列化规则(对齐 SimApiResponse<T>):
|
||||
* - data 实现了 ISerializable(@Serialization DTO、Array<T> 等)→ data 内嵌为对象
|
||||
* - data 为动态结构(如 HashMap<String, Any>,不满足泛型 ISerialization 约束)
|
||||
* → 经 SimApiJson 序列化后解析内嵌为对象(而非字符串)
|
||||
* 因此 data 在 JSON 中始终是对象/数组/标量,不会是"JSON 字符串"。
|
||||
* 序列化/反序列化由 simapi_serialization 反射处理:
|
||||
* - data 为对象/数组/标量,反射递归
|
||||
* - data 为动态结构(HashMap<String, Any>)同样反射支持
|
||||
*/
|
||||
public class SimApiDataResponse <: SimApiBaseResponse & ISerialization<SimApiDataResponse> {
|
||||
public class SimApiDataResponse <: SimApiBaseResponse {
|
||||
public var _data: ?Any = None
|
||||
|
||||
public init() {
|
||||
@@ -41,48 +35,4 @@ public class SimApiDataResponse <: SimApiBaseResponse & ISerialization<SimApiDat
|
||||
super(code, message)
|
||||
this._data = Some(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化为 DataModel:code + message + data(data 为对象)。
|
||||
*/
|
||||
public override func serializeObject(options: JsonSerializerOptions): DataModel {
|
||||
let dms = DataModelStruct()
|
||||
dms.add(Field("code", DataModelInt(_code)))
|
||||
dms.add(Field("message", DataModelString(_message)))
|
||||
if (let Some(data) <- _data) {
|
||||
if (let ser: ISerializable <- data) {
|
||||
dms.add(Field("data", ser.serializeObject(options)))
|
||||
} else {
|
||||
// 动态结构(HashMap<String, Any> 等):经 SimApiJson 序列化后解析内嵌为对象
|
||||
let json = SimApiJson.json(Some(data))
|
||||
dms.add(Field("data", DataModel.fromJson(JsonValue.fromStr(json))))
|
||||
}
|
||||
}
|
||||
dms
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 DataModel 反序列化:code + message + data(data 保留原始 DataModel)。
|
||||
*/
|
||||
public static func deserializeObject(dm: DataModel, options: JsonSerializerOptions): SimApiDataResponse {
|
||||
let resp = SimApiDataResponse()
|
||||
if (let dms: DataModelStruct <- dm) {
|
||||
for (field in dms.getFields()) {
|
||||
match (field.getName()) {
|
||||
case "code" =>
|
||||
if (let v: DataModelInt <- field.getData()) {
|
||||
resp._code = v.getValue()
|
||||
}
|
||||
case "message" =>
|
||||
if (let v: DataModelString <- field.getData()) {
|
||||
resp._message = v.getValue()
|
||||
}
|
||||
case "data" =>
|
||||
resp._data = Some(field.getData())
|
||||
case _ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
resp
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user