The Docs.

Prim+RPC is in prerelease mode and may be unstable until official release.
All Plugins

JSON Handlers

Plugin Details
  • Plugin Type:
    JSON Handler
  • Transport:
    Inapplicable

Prim+RPC by default will serialize messages passed to it using the default JSON “stringify” method to serialize RPC and the destr library to deserialize RPC. This JSON handler implementation can be swapped out with any other library to support advanced types like Dates, Maps, Sets, or others.

We can provide our own JSON handler that resembles the default JSON handler (with a few optional additions). See the configuration page for the JSON Handler for more details on setting up your own or browse popular examples on this page.

In all cases, the JSON handler must be the same on both the client and server. This list is non-exhaustive and you may choose to implement your own JSON handler.

SuperJSON

SuperJSON allows additional types to be used with Prim+RPC. The JSON handler will look like this:

import jsonHandler from "superjson"
 
createPrimClient({ jsonHandler })
createPrimServer({ jsonHandler })

MsgPack

MsgPack supports additional types including ArrayBuffer types. While it is not JSON, it can be used with Prim+RPC. There are also several implementations of MsgPack and you may choose one that best suits your needs.

We’ll add a few additional options to the handler to allow Prim+RPC plugins to better work with binary data:

import { encode as stringify, decode as parse } from "@msgpack/msgpack"
 
const jsonHandler = {
	stringify,
	parse,
	mediaType: "application/octet-stream",
	binary: true,
}
 
createPrimClient({ jsonHandler })
createPrimServer({ jsonHandler })

devalue

Like the SuperJSON example, devalue may be used to support additional types with Prim+RPC. The JSON handler can be set up like so:

import jsonHandler from "devalue"
 
createPrimClient({ jsonHandler })
createPrimServer({ jsonHandler })

YAML

YAML may be used with Prim+RPC and may be a more readable alternative to JSON. The JSON-like handler can be set up like so:

import yaml from "yaml"
 
const jsonHandler = {
	parse: yaml.parse,
	stringify: yaml.stringify,
	mediaType: "application/yaml",
}
 
createPrimClient({ jsonHandler })
createPrimServer({ jsonHandler })

Others

These are some popular examples but Prim+RPC can be configured to use other JSON or JSON-like handlers.

Report an Issue