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.
// 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.
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
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.