Files
18xchat/assets/Scripts/chat18x/network/codec/JsonCodec.ts
T

14 lines
469 B
TypeScript
Raw Normal View History

2025-08-15 00:36:38 +08:00
export const JsonCodec = {
/** POST/PUT 的请求体直接用对象,让传输层序列化为 JSON */
encode<T = any>(obj: T): any {
return obj ?? {};
},
/** 根据传输层返回的类型做解码:string → JSON.parse,其他直接返回 */
decode<T = any>(resp: any): T {
if (typeof resp === "string") {
try { return JSON.parse(resp) as T; } catch { /* 可能是纯文本 */ }
}
return resp as T;
}
};