1//! The JSON value that the whole tree shares.2//!3//! Objects are `BTreeMap`s, so a document is a function of its content and not4//! of the order a peer happened to write the keys in. Two equal documents5//! serialise to the same bytes, which is what the web layer needs when a reply6//! is compared or cached.78use std::collections::BTreeMap;910/// One JSON value.11 #[derive(Clone, PartialEq, Debug)]12pubenumValue {13/// `null`.14Null,15/// `true` or `false`.16Bool(bool),17/// A number that fits `i64`. It always writes without a decimal point.18Int(i64),19/// A number written with a fraction or an exponent, or one too large for20/// `i64`. A whole value in here still writes as an integer, so it reads21/// back as [`Value::Int`].22Float(f64),23/// A string. It holds text, never a lone surrogate.24Str(String),25/// `[ ... ]`.26Array(Vec<Value>),27/// `{ ... }`. Keys are unique and sorted.28Object(BTreeMap<String, Value>),29 }3031implValue {32/// A string value.33pubfnstr(s: implInto<String>) -> Value {34Value::Str(s.into())35 }3637/// An integer value.38pubconstfnint(n: i64) -> Value {39Value::Int(n)40 }4142/// A float value.43pubconstfnfloat(x: f64) -> Value {44Value::Float(x)45 }4647/// A bool value.48pubconstfnbool(b: bool) -> Value {49Value::Bool(b)50 }5152/// An object under construction. `Object::build` finishes it.53pubfnobject() -> Object {54Object(BTreeMap::new())55 }5657/// The text of a string value.58pubfnas_str(&self) -> Option<&str> {59matchself {60Value::Str(s) => Some(s),61 _ => None,62 }63 }6465/// The value of an integer. A float is never read as an integer, so a66/// whole float stays a float.67pubfnas_i64(&self) -> Option<i64> {68matchself {69Value::Int(n) => Some(*n),70 _ => None,71 }72 }7374/// The value of a non-negative integer.75pubfnas_u64(&self) -> Option<u64> {76matchself {77Value::Int(n) => u64::try_from(*n).ok(),78 _ => None,79 }80 }8182/// The value as f64. An integer widens, and above 2^53 the widening is not83/// exact.84pubfnas_f64(&self) -> Option<f64> {85matchself {86Value::Int(n) => Some(*n as f64),87Value::Float(x) => Some(*x),88 _ => None,89 }90 }9192/// The value of a bool.93pubfnas_bool(&self) -> Option<bool> {94matchself {95Value::Bool(b) => Some(*b),96 _ => None,97 }98 }99100/// The items of an array.101pubfnas_array(&self) -> Option<&[Value]> {102matchself {103Value::Array(items) => Some(items),104 _ => None,105 }106 }107108/// The entries of an object, in sorted key order.109pubfnas_object(&self) -> Option<&BTreeMap<String, Value>> {110matchself {111Value::Object(map) => Some(map),112 _ => None,113 }114 }115116/// The value under a key, when this is an object.117pubfnget(&self, key: &str) -> Option<&Value> {118self.as_object()?.get(key)119 }120 }121122/// An object being built, so that a document reads as one expression.123 #[derive(Clone, PartialEq, Debug, Default)]124pubstructObject(BTreeMap<String, Value>);125126implObject {127/// An empty object.128pubfnnew() -> Object {129Object(BTreeMap::new())130 }131132/// Sets a key and hands the builder back, so calls chain. A key set twice133/// is replaced; the parser is the place that refuses a duplicate.134pubfnput(mutself, key: implInto<String>, value: Value) -> Object {135self.0.insert(key.into(), value);136self137 }138139/// The finished value. Keys are sorted because the map is a `BTreeMap`.140pubfnbuild(self) -> Value {141Value::Object(self.0)142 }143 }144145implFrom<Object> forValue {146fnfrom(object: Object) -> Value {147 object.build()148 }149 }