Expand description
A Cap’n Proto JSON codec, implementing the codec
defined in json.capnp.
The wire format is compatible with the C++ capnp::JsonCodec that ships
with Cap’n Proto: messages encoded by this crate can be decoded by the C++
codec, and vice-versa.
§Quick start
ⓘ
use capnp::message;
use capnp_json::{from_json, to_json};
let mut builder = message::Builder::new_default();
let root: my_schema_capnp::my_struct::Builder<'_> = builder.init_root();
// ... populate `root` ...
let json: String = to_json(root.reborrow_as_reader())?;
let mut decoded = message::Builder::new_default();
let decoded_root: my_schema_capnp::my_struct::Builder<'_> =
decoded.init_root();
from_json(&json, decoded_root)?;§JSON annotations
To use any of the JSON annotations defined in json.capnp (for example
$Json.name, $Json.flatten, $Json.discriminator, $Json.base64,
$Json.hex), tell capnpc to resolve references to the annotation schema
to this crate from your build.rs:
ⓘ
capnpc::CompilerCommand::new()
.crate_provides("capnp_json", [0x8ef99297a43a5e34])
.file("my_schema.capnp")
.run()
.expect("compiling schema");