RPC JSON Serializer
This serializer handles JSON payloads for the RPC Protocol and supports native data types.
Extending Native Data Types
Extend native types by creating your own StandardRPCCustomJsonSerializer and adding it to the customJsonSerializers option.
Define Your Custom Serializer
tsimport type { StandardRPCCustomJsonSerializer } from '@orpc/client/standard' export class { constructor( public readonly : string, public readonly : string, public readonly : string, public readonly : number, ) {} () { return { : this., : this., : this., : this., } } } export const : StandardRPCCustomJsonSerializer = { : 21, : => instanceof , : => .toJSON(), : => new (.id, .name, .email, .age), }WARNING
Ensure the
typeis unique and greater than20to avoid conflicts with built-in types in the future.Use Your Custom Serializer
tsconst = new (, { : [], }) const = new ({ : 'https://example.com/rpc', : [], })
Overriding Built-in Types
You can override built-in types by matching their type with the built-in types.
For example, oRPC represents undefined only in array items and ignores it in objects. To override this behavior:
ts
import { StandardRPCCustomJsonSerializer } from '@orpc/client/standard'
export const : StandardRPCCustomJsonSerializer = {
: 3, // Match the built-in undefined type.
: => === ,
: => null, // JSON cannot represent undefined, so use null.
: => ,
}