pub enum JsonValue {
Null,
Boolean(bool),
Number(f64),
String(String),
Array(Vec<JsonValue>),
Object(BTreeMap<String, JsonValue>),
// some variants omitted
}Expand description
An in-memory JSON value.
This is the currency of the FieldCodec trait: a codec produces a
JsonValue when encoding and is handed one when decoding. The codec itself
never deals with JSON syntax — serialising and parsing are handled by this
crate.
use capnp_json::JsonValue;
let value = JsonValue::Array(vec![
JsonValue::String("hello".into()),
JsonValue::Number(42.0),
JsonValue::Null,
]);
assert_eq!(value, value.clone());§Numbers
Number is an f64, matching JSON’s own numeric
model. A codec that needs the full 64-bit integer range should encode to
String, which is what this crate does for Int64
and UInt64 fields.
§Object ordering
Object is a BTreeMap, so its members are written
in sorted key order. JSON objects are unordered by definition, so no order
is more correct than another, but a deterministic one matters: the same
value must encode to the same bytes every time, or golden-file tests,
response caching and anything signing the output stop working. Sorted order
is also the canonical form specified by RFC 8785.
Members of schema structs never pass through this map — they are written
in schema declaration order — so this affects only the objects a custom
FieldCodec builds.
Duplicate keys are rejected when parsing, so an Object decoded by this
crate never loses a member.
Variants§
Null
JSON null. Also the encoding of a Cap’n Proto Void.
Boolean(bool)
JSON true or false.
Number(f64)
A JSON number. See the note on numbers.
String(String)
A JSON string, already unescaped.
Array(Vec<JsonValue>)
A JSON array.
Object(BTreeMap<String, JsonValue>)
A JSON object. See the note on ordering.