pub enum JsonValue {
Null,
Bool(bool),
Integer(i64),
U64(u64),
String(Cow<'static, str>),
Array(Vec<JsonValue>),
Object(BTreeMap<String, JsonValue>),
}Expand description
JSON value type (deterministic implementation).
§Design decisions
- No
Floatvariant (formal-verification obstacle) BTreeMapfor deterministic iteration orderVecinstead ofim::VectorIntegerisi64(Kani-friendly)U64is the full-rangeu64channel (v0.9.0):Integercannot carry high u64 values losslessly (e.g. the audit-chain rule-set anchorFactId(u64::MAX-1)sentinel); any source whose value range may exceedi64must go throughU64—as i64cast wrap-around is forbiddenCowto reduce clone overhead
§Why PartialEq is not derived
Implemented manually to guarantee cross-language consistency.
§Examples
extern crate alloc;
use evorule_primitives::JsonValue;
use alloc::collections::BTreeMap;
// Construct via constructors
let name = JsonValue::string("Alice");
let age = JsonValue::Integer(30);
assert!(name.is_string());
assert_eq!(age.as_i64(), Some(30));
// Construct via From impls
let from_str: JsonValue = "hello".into();
let from_int: JsonValue = 42i64.into();
assert_eq!(from_str.as_str(), Some("hello"));
assert_eq!(from_int.as_i64(), Some(42));
// Construct a nested object
let mut map = BTreeMap::new();
map.insert("items".to_string(), JsonValue::array(vec![JsonValue::Integer(1), JsonValue::Integer(2)]));
let v = JsonValue::object(map);
assert!(v.is_object());Variants§
Null
JSON null
Bool(bool)
Boolean value
Integer(i64)
Integer (i64)
U64(u64)
Unsigned integer (full-range u64 channel; strictly distinguished from
JsonValue::Integer — no cross-shape numeric equality, see the
PartialEq / Ord implementations)
String(Cow<'static, str>)
String (uses Cow to reduce cloning)
Array(Vec<JsonValue>)
Array (ordered list)
Object(BTreeMap<String, JsonValue>)
Object (key-value pairs; BTreeMap guarantees deterministic order)
Implementations§
Source§impl JsonValue
impl JsonValue
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Check whether the value is an integer
Sourcepub fn is_u64(&self) -> bool
pub fn is_u64(&self) -> bool
Check whether the value is an unsigned integer (full-range u64 channel)
Sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
Get the value’s length (array element count or object entry count)
§Examples
use evorule_primitives::JsonValue;
let arr = JsonValue::array(vec![JsonValue::Integer(1), JsonValue::Integer(2)]);
assert_eq!(arr.len(), Some(2));
let obj = JsonValue::empty_object();
assert_eq!(obj.len(), Some(0));
let null = JsonValue::Null;
assert_eq!(null.len(), None);Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
Try to convert to u64 (matches JsonValue::U64 only; strictly
symmetric with as_i64 shape-wise)
Sourcepub fn as_array_mut(&mut self) -> Option<&mut [JsonValue]>
pub fn as_array_mut(&mut self) -> Option<&mut [JsonValue]>
Try to convert to &mut JsonValue
Sourcepub fn as_object_mut(&mut self) -> Option<&mut BTreeMap<String, JsonValue>>
pub fn as_object_mut(&mut self) -> Option<&mut BTreeMap<String, JsonValue>>
Try to convert to &mut ObjectMap
Sourcepub fn get(&self, key: &str) -> Option<&JsonValue>
pub fn get(&self, key: &str) -> Option<&JsonValue>
Get an object field (if present)
§Examples
extern crate alloc;
use evorule_primitives::JsonValue;
use alloc::collections::BTreeMap;
let mut map = BTreeMap::new();
map.insert("a".to_string(), JsonValue::Integer(1));
let obj = JsonValue::object(map);
assert_eq!(obj.get("a").and_then(|v| v.as_i64()), Some(1));
assert_eq!(obj.get("missing"), None);
// Calling on a non-object always returns None (never panics)
let n = JsonValue::Null;
assert_eq!(n.get("anything"), None);Sourcepub fn insert(&mut self, key: String, value: JsonValue) -> Option<JsonValue>
pub fn insert(&mut self, key: String, value: JsonValue) -> Option<JsonValue>
Insert or update an object field
§Examples
use evorule_primitives::JsonValue;
let mut obj = JsonValue::empty_object();
let old = obj.insert("k".to_string(), JsonValue::Integer(1));
assert_eq!(old, None);
let old = obj.insert("k".to_string(), JsonValue::Integer(2));
assert_eq!(old.and_then(|v| v.as_i64()), Some(1));
assert_eq!(obj.get("k").and_then(|v| v.as_i64()), Some(2));Sourcepub fn try_insert(
&mut self,
key: String,
value: JsonValue,
) -> Result<(), &JsonValue>
pub fn try_insert( &mut self, key: String, value: JsonValue, ) -> Result<(), &JsonValue>
Try to insert (returns an error if the key already exists)
§Examples
use evorule_primitives::JsonValue;
let mut obj = JsonValue::empty_object();
assert!(obj.try_insert("k".to_string(), JsonValue::Integer(1)).is_ok());
assert!(obj.try_insert("k".to_string(), JsonValue::Integer(2)).is_err());§Why an explicit allow(clippy::map_entry) instead of the Entry API
Clippy’s map_entry lint suggests rewriting to BTreeMap::entry, but
the type of this function is cfg-determined (see ObjectMap):
cfg(not(kani))->BTreeMap, which has anEntryAPI;cfg(kani)-> [KaniMap] (orderedVec), which has noEntryAPI — its whole point is to avoid CBMC’s state explosion on red-black tree insertion.
Both branches must compile from the same source, so no backend-
specific API can be used. Keeping contains_key + get/insert is
the only backend-independent formulation, at zero performance cost
(contains_key and get follow the same BTreeMap lookup path; only
the return type differs). Do not “fix” this.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all contents of the object or array
§Examples
use evorule_primitives::JsonValue;
let mut arr = JsonValue::array(vec![JsonValue::Integer(1), JsonValue::Integer(2)]);
arr.clear();
assert_eq!(arr.len(), Some(0));
// Calling on a non-container type is a no-op
let mut null = JsonValue::Null;
null.clear();Source§impl JsonValue
impl JsonValue
Sourcepub const fn u64(v: u64) -> JsonValue
pub const fn u64(v: u64) -> JsonValue
Construct an unsigned integer (full-range u64 channel)
§Examples
use evorule_primitives::JsonValue;
let v = JsonValue::u64(u64::MAX - 1);
assert!(v.is_u64());
assert_eq!(v.as_u64(), Some(u64::MAX - 1));
// Shape-strict: U64 and Integer are never cross-shape equal
assert_ne!(JsonValue::u64(5), JsonValue::Integer(5));Sourcepub fn string<S>(s: S) -> JsonValue
pub fn string<S>(s: S) -> JsonValue
Construct a string
§Examples
use evorule_primitives::JsonValue;
let v = JsonValue::string("hello");
assert_eq!(v.as_str(), Some("hello"));Sourcepub fn array(v: Vec<JsonValue>) -> JsonValue
pub fn array(v: Vec<JsonValue>) -> JsonValue
Construct an array
§Examples
use evorule_primitives::JsonValue;
let arr = JsonValue::array(vec![JsonValue::Integer(1), JsonValue::Integer(2)]);
assert!(arr.is_array());
assert_eq!(arr.as_array().map(|a| a.len()), Some(2));Sourcepub fn object(map: BTreeMap<String, JsonValue>) -> JsonValue
pub fn object(map: BTreeMap<String, JsonValue>) -> JsonValue
Construct an object
§Examples
extern crate alloc;
use evorule_primitives::JsonValue;
use alloc::collections::BTreeMap;
let mut map = BTreeMap::new();
map.insert("key".to_string(), JsonValue::Integer(42));
let obj = JsonValue::object(map);
assert!(obj.is_object());
assert_eq!(obj.get("key").and_then(|v| v.as_i64()), Some(42));Sourcepub fn object_from_pairs_owned(pairs: Vec<(String, JsonValue)>) -> JsonValue
pub fn object_from_pairs_owned(pairs: Vec<(String, JsonValue)>) -> JsonValue
Construct an object from key-value pairs (owning version).
More efficient than object_from_pairs because values are not cloned.
§Examples
use evorule_primitives::JsonValue;
let pairs = vec![
("a".to_string(), JsonValue::Integer(1)),
("b".to_string(), JsonValue::Integer(2)),
];
let obj = JsonValue::object_from_pairs_owned(pairs);
assert_eq!(obj.get("a").and_then(|v| v.as_i64()), Some(1));Sourcepub fn object_from_pairs(pairs: &[(&str, JsonValue)]) -> JsonValue
pub fn object_from_pairs(pairs: &[(&str, JsonValue)]) -> JsonValue
Construct an object from key-value pairs (borrowed version; clones values).
Note: this method clones all values. Use object_from_pairs_owned to
avoid cloning.
§Examples
use evorule_primitives::JsonValue;
let obj = JsonValue::object_from_pairs(&[
("a", JsonValue::Integer(1)),
("b", JsonValue::Integer(2)),
]);
assert_eq!(obj.get("a").and_then(|v| v.as_i64()), Some(1));Sourcepub fn empty_object() -> JsonValue
pub fn empty_object() -> JsonValue
Construct an empty object
§Examples
use evorule_primitives::JsonValue;
let obj = JsonValue::empty_object();
assert!(obj.is_object());
assert_eq!(obj.as_object().map(|m| m.len()), Some(0));Sourcepub fn empty_array() -> JsonValue
pub fn empty_array() -> JsonValue
Construct an empty array
§Examples
use evorule_primitives::JsonValue;
let arr = JsonValue::empty_array();
assert!(arr.is_array());
assert_eq!(arr.as_array().map(|a| a.len()), Some(0));Trait Implementations§
impl Eq for JsonValue
Source§impl From<u64> for JsonValue
Construct an unsigned integer from u64 (full-range channel, no i64 cast wrap-around)
impl From<u64> for JsonValue
Construct an unsigned integer from u64 (full-range channel, no i64 cast wrap-around)