Serialization

Override serialize and deserialize to use a custom serialization format instead of plain JSON.

Default

When not specified, the client uses JSON.stringify and JSON.parse.

ts
// Default: JSON.stringify / JSON.parse
const rpc = createRpcClient({
  baseUrl: '/api',
});

Custom (superjson)

Use a library like superjson to support Date, BigInt, Map, Set and other types not natively supported by JSON.

ts
import superjson from 'superjson';

// Custom serialization — e.g. superjson for Date, BigInt, Map, Set
const rpc = createRpcClient({
  baseUrl: '/api',
  serialize: (data) => superjson.stringify(data),
  deserialize: (text) => superjson.parse(text),
});

Type Signature

ts
interface RpcClientConfig {
  // ...
  serialize?: (data: unknown) => string;   // default: JSON.stringify
  deserialize?: (text: string) => unknown; // default: JSON.parse
}

Try it

Same endpoint, two clients. The default uses JSON.parse, the custom one uses lossless-json via the deserialize option.

visit GitHub to learn more about metaxy