import type { Constructor, JsonX, Result } from '../../../mod.js'; /** 1.构建SerializeError需要用到SerdeJSON,会导致循环引用 */ import { SerializeError } from '../../../mod.js'; /** [superjson文档](https://github.com/flightcontrolhq/superjson) + superjson是一个JSON序列化库,相比较于JS自带的JSON能支持更多的类型序列化为json */ /** ## `SerdeJsonSchema` : 自定义json转化器 */ type SerdeJsonSchema = { /** `is` : 判断是否是对应转化类型A */ is: (data: unknown) => data is A; /** `serialize` : 正序列化函数 `A`->`B` */ serialize: (data: A) => B; /** `deserialize` : 反序列化函数 `B`->`A` */ deserialize: (data: B) => A; }; /** ## `SerdeJSON` : 基于SuperJson的自定义json转化库 @category Ext - SerdeJSON */ declare class SerdeJSON { /** ### `register` : 全局注册自定义转换器 @tips 尽量使用一层自定义数据类型包裹,多层包裹会导致性能变差 @example Usage ```ts SerdeJSON.register('some_tag', some_tag) SerdeJSON.register('none_tag', none_tag) const option_json_schema: SerdeJsonSchema, { tag: 'some' | 'none'; value: string }> = { is: is_option, serialize: (option) => ({ tag: option.is_none ? 'none' : 'some', value: SerdeJSON.serialize(option.unwrap_or(null)), }), deserialize: (jsonx) => (jsonx.tag == 'none' ? None : Some(SerdeJSON.deserialize(jsonx.value).unwrap())), } // 注册Option类型转化 SerdeJSON.register('options', option_json_schema) ``` @param key 注册的key,确保全局唯一 */ static register(key: string, option: symbol | SerdeJsonSchema): void; /** ### `register_class` : 注册类 */ static register_class(c: Constructor, key: string): void; /** ### `serialize` : JSON序列化 `data` --> `json` @example Usage ```ts const info = { user: 'jiojio', age: 18 } const jsonx = SerdeJSON.serialize(info) assert(jsonx === '{"json":{"user":"jiojio","age":18}}') assert(jsonx === JSON.stringify({ json: info })) ``` */ static serialize(data: any): string; /** ### `deserialize` : JSON反序列化 `json` --> `data` @example Usage ```ts const info = { user: 'jiojio', age: 18 } const jsonx = SerdeJSON.serialize(info) const info_copy = SerdeJSON.deserialize(jsonx).unwrap() assert(equal(info, info_copy)) ``` */ static deserialize(json: string): Result; /** ### `parse` : JSON反序列化 `json` --> `data` */ static parse: typeof SerdeJSON.deserialize; /** ### `from_str` : JSON反序列化 `json` -> `data` */ static from_str: typeof SerdeJSON.deserialize; /** ### `stringify` : JSON序列化 `data` --> `json` */ static stringify: typeof SerdeJSON.serialize; /** ### `to_str` : JSON序列化 `data` --> `json` */ static to_str: typeof SerdeJSON.serialize; } export { SerdeJSON, type SerdeJsonSchema }; //# sourceMappingURL=class.d.ts.map