Skip to main content

to_json

Function to_json 

Source
pub fn to_json<'msg>(reader: impl Into<Reader<'msg>>) -> Result<String>
Expand description

Encode a Cap’n Proto struct as a JSON string.

reader accepts anything that converts into a capnp::dynamic_value::Reader — typically a struct reader obtained from message::Reader::get_root() or message::Builder::reborrow_as_reader(). The value must be a struct, since the top-level JSON value is an object.

The mapping from Cap’n Proto values to JSON, and the effect of the $Json.* annotations, are described in the module documentation; all of it matches the C++ capnp::JsonCodec. The output is compact, with no insignificant whitespace.

This is Codec::new().encode(reader). Use a Codec directly to register custom FieldCodecs — which is required for messages containing AnyPointer or interface fields.

let json: String = capnp_json::to_json(root.reborrow_as_reader())?;