| 123456789101112131415161718192021222324252627282930313233343536373839 |
- /**
- * SuperJSON 配置 - 用于 JSON 序列化/反序列化
- * 处理 Next.js Server Components 和 Client Components 之间的数据传输
- */
- import { SuperJSON } from 'superjson';
- export const superjson = SuperJSON;
- /**
- * 将数据序列化为 JSON 字符串(用于 Server -> Client 传输)
- */
- export function serialize<T>(data: T): string {
- return superjson.stringify(data);
- }
- /**
- * 从 JSON 字符串反序列化数据(用于 Client -> Server 传输)
- */
- export function deserialize<T>(json: string): T {
- return superjson.parse(json);
- }
- /**
- * Server Actions 返回值的序列化包装
- */
- export function createSerializedResponse<T>(data: T) {
- return {
- data: superjson.serialize(data).json,
- };
- }
- /**
- * 解析从 Server 传来的序列化数据
- */
- export function parseSerializedResponse<T>(response: { data: unknown }): T {
- return superjson.deserialize({
- json: response.data as any,
- }) as T;
- }
|