capnp-json
A Cap'n Proto JSON codec for capnp-rust,
implementing the codec defined in
json.capnp.
It encodes a Cap'n Proto message to JSON, and decodes JSON into a Cap'n Proto message, using the schema's runtime type information. The wire format is compatible with the C++ JSON codec that ships with Cap'n Proto.
Usage
Add the dependency:
[]
= "0.25"
= "0.1"
Encoding a message reader to a JSON string, and decoding JSON back into a message builder:
use message;
use ;
#
If your schema uses any of the JSON annotations ($Json.name, $Json.flatten,
$Json.discriminator, $Json.base64, $Json.hex), import them by adding the
following to your build.rs, so that the generated code links against the
annotations defined in this crate:
And in your schema:
using Json = import "/capnp/compat/json.capnp";
struct MyStruct {
myField @0 :Text $Json.name("my_field");
}
Supported features
- All primitive Cap'n Proto types, including
Int64/UInt64encoded as JSON strings (matching the C++ codec). Float32/Float64NaN,Infinity, and-Infinityencoded as JSON strings.- Structs, lists, lists of lists, and lists of structs.
- Enums, encoded by name (or by ordinal if the enumerant is missing).
- Named and unnamed unions.
- Annotations:
$Json.name— rename a field, enumerant, method, group, or union member in the JSON representation.$Json.flatten— flatten a struct, group, or union into its parent.$Json.discriminator— encode a union's variant as a sibling discriminator field.$Json.base64/$Json.hex— encodeDatafields as Base64 or hex strings instead of arrays of bytes.
Not yet supported
- The
Value/Call/rawextensions fromjson.capnp. AnyPointerandCapabilityfields (these are returned as errors).- Custom encoder/decoder handlers for specific types — the C++ codec
exposes a
HandlerAPI; this crate does not yet. - Pretty-printed output.