Skip to main content

JsonValue

Enum JsonValue 

Source
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 Float variant (formal-verification obstacle)
  • BTreeMap for deterministic iteration order
  • Vec instead of im::Vector
  • Integer is i64 (Kani-friendly)
  • U64 is the full-range u64 channel (v0.9.0): Integer cannot carry high u64 values losslessly (e.g. the audit-chain rule-set anchor FactId(u64::MAX-1) sentinel); any source whose value range may exceed i64 must go through U64 — as i64 cast wrap-around is forbidden
  • Cow to 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

Source

pub fn is_object(&self) -> bool

Check whether the value is an object

Source

pub fn is_array(&self) -> bool

Check whether the value is an array

Source

pub fn is_integer(&self) -> bool

Check whether the value is an integer

Source

pub fn is_u64(&self) -> bool

Check whether the value is an unsigned integer (full-range u64 channel)

Source

pub fn is_string(&self) -> bool

Check whether the value is a string

Source

pub fn is_bool(&self) -> bool

Check whether the value is a boolean

Source

pub fn is_null(&self) -> bool

Check whether the value is null

Source

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);
Source

pub fn is_empty(&self) -> bool

Check whether the value is empty (array or object)

Source

pub fn as_i64(&self) -> Option<i64>

Try to convert to i64

Source

pub fn as_u64(&self) -> Option<u64>

Try to convert to u64 (matches JsonValue::U64 only; strictly symmetric with as_i64 shape-wise)

Source

pub fn as_str(&self) -> Option<&str>

Try to convert to &str

Source

pub fn as_bool(&self) -> Option<bool>

Try to convert to bool

Source

pub fn as_array(&self) -> Option<&[JsonValue]>

Try to convert to &JsonValue

Source

pub fn as_array_mut(&mut self) -> Option<&mut [JsonValue]>

Try to convert to &mut JsonValue

Source

pub fn as_object(&self) -> Option<&BTreeMap<String, JsonValue>>

Try to convert to &ObjectMap

Source

pub fn as_object_mut(&mut self) -> Option<&mut BTreeMap<String, JsonValue>>

Try to convert to &mut ObjectMap

Source

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);
Source

pub fn get_mut(&mut self, key: &str) -> Option<&mut JsonValue>

Get an object field (mutable)

Source

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));
Source

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 an Entry API;
  • cfg(kani) -> [KaniMap] (ordered Vec), which has no Entry API — 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.

Source

pub fn remove(&mut self, key: &str) -> Option<JsonValue>

Remove a field from the object

Source

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

Source

pub const fn null() -> JsonValue

Construct null

Source

pub const fn bool(b: bool) -> JsonValue

Construct a boolean

Source

pub const fn integer(i: i64) -> JsonValue

Construct an integer

Source

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));
Source

pub fn string<S>(s: S) -> JsonValue
where S: Into<String>,

Construct a string

§Examples
use evorule_primitives::JsonValue;

let v = JsonValue::string("hello");
assert_eq!(v.as_str(), Some("hello"));
Source

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));
Source

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));
Source

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));
Source

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));
Source

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));
Source

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§

Source§

impl Clone for JsonValue

Source§

fn clone(&self) -> JsonValue

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for JsonValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for JsonValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for JsonValue

Source§

impl From<&str> for JsonValue

Construct a string from a &str

Source§

fn from(s: &str) -> JsonValue

Converts to this type from the input type.
Source§

impl From<BTreeMap<String, JsonValue>> for JsonValue

Construct an object from a map

Source§

fn from(map: BTreeMap<String, JsonValue>) -> JsonValue

Converts to this type from the input type.
Source§

impl From<Cow<'static, str>> for JsonValue

Construct a string from a Cow<’static, str>

Source§

fn from(s: Cow<'static, str>) -> JsonValue

Converts to this type from the input type.
Source§

impl From<String> for JsonValue

Construct a string from a String

Source§

fn from(s: String) -> JsonValue

Converts to this type from the input type.
Source§

impl From<Vec<JsonValue>> for JsonValue

Construct an array from a Vec

Source§

fn from(v: Vec<JsonValue>) -> JsonValue

Converts to this type from the input type.
Source§

impl From<bool> for JsonValue

Construct a boolean from bool

Source§

fn from(b: bool) -> JsonValue

Converts to this type from the input type.
Source§

impl From<i64> for JsonValue

Construct an integer from i64

Source§

fn from(i: i64) -> JsonValue

Converts to this type from the input type.
Source§

impl From<u64> for JsonValue

Construct an unsigned integer from u64 (full-range channel, no i64 cast wrap-around)

Source§

fn from(v: u64) -> JsonValue

Converts to this type from the input type.
Source§

impl Ord for JsonValue

Source§

fn cmp(&self, other: &JsonValue) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for JsonValue

Source§

fn eq(&self, other: &JsonValue) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for JsonValue

Source§

fn partial_cmp(&self, other: &JsonValue) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.