pub enum JsonValue {
Null,
Bool(bool),
Integer(i64),
U64(u64),
String(Cow<'static, str>),
Array(Vec<JsonValue>),
Object(BTreeMap<String, JsonValue>),
}Expand description
JSON 值类型(确定性实现)
§设计决策
- 无
Float类型(形式化验证障碍) - 使用
BTreeMap保证确定性迭代顺序 - 使用
Vec而非im::Vector Integer为i64(Kani 友好)U64为u64全域通道(v0.9.0):Integer无法无损承载 u64 高位值 (如审计链规则集锚定事实的FactId(u64::MAX-1)哨兵),凡值域可能 超出i64的来源一律走U64,禁止as i64cast 回绕- 使用
Cow减少克隆开销
§不实现 PartialEq 的自动派生
手动实现以保证跨语言一致性
§示例
extern crate alloc;
use evorule_tcb::JsonValue;
use alloc::collections::BTreeMap;
// 通过构造函数构造
let name = JsonValue::string("Alice");
let age = JsonValue::Integer(30);
assert!(name.is_string());
assert_eq!(age.as_i64(), Some(30));
// 通过 From trait 构造
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));
// 构造嵌套对象
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)
布尔值
Integer(i64)
整数(i64)
U64(u64)
无符号整数(u64 全域;与 JsonValue::Integer 形态严格区分,
不做跨形态数值相等——见 PartialEq / Ord 实现)
String(Cow<'static, str>)
字符串(使用 Cow 减少克隆)
Array(Vec<JsonValue>)
数组(有序列表)
Object(BTreeMap<String, JsonValue>)
对象(键值对,使用 BTreeMap 保证确定性顺序)
Implementations§
Source§impl JsonValue
impl JsonValue
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
检查是否为整数类型
Sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
获取值的长度(数组元素个数或对象键值对数)
§示例
use evorule_tcb::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>
尝试转换为 u64(仅 JsonValue::U64 命中;与 as_i64 形态严格对称)
Sourcepub fn as_array_mut(&mut self) -> Option<&mut [JsonValue]>
pub fn as_array_mut(&mut self) -> Option<&mut [JsonValue]>
尝试转换为 &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>>
尝试转换为 &mut ObjectMap
Sourcepub fn get(&self, key: &str) -> Option<&JsonValue>
pub fn get(&self, key: &str) -> Option<&JsonValue>
获取对象字段(若存在)
§示例
extern crate alloc;
use evorule_tcb::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);
// 在非对象上调用总是返回 None(不会 panic)
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>
插入或更新对象字段
§示例
use evorule_tcb::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>
尝试插入(如果 key 已存在则返回错误)
§示例
use evorule_tcb::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());§为什么显式 allow(clippy::map_entry) 而不是改用 Entry API
Clippy 的 map_entry 建议改写为 BTreeMap::entry,但本函数的类型
由 cfg 决定(见 ObjectMap):
cfg(not(kani))→BTreeMap,有EntryAPI;cfg(kani)→ [KaniMap](有序Vec),没有EntryAPI—— 它的存在正是为了绕开 CBMC 对红黑树插入的状态爆炸。
两条分支都必须编译同一份源码,所以不能使用任一后端专有的 API。
这里保留 contains_key + get/insert 是唯一后端无关的写法,
且零性能代价(contains_key 与 get 走的是同一个 BTreeMap 查找路径,
差别仅在返回类型)。不要“修好“它。
Source§impl JsonValue
impl JsonValue
Sourcepub const fn u64(v: u64) -> JsonValue
pub const fn u64(v: u64) -> JsonValue
构造无符号整数(u64 全域通道)
§示例
use evorule_tcb::JsonValue;
let v = JsonValue::u64(u64::MAX - 1);
assert!(v.is_u64());
assert_eq!(v.as_u64(), Some(u64::MAX - 1));
// 形态严格:U64 与 Integer 不做跨形态相等
assert_ne!(JsonValue::u64(5), JsonValue::Integer(5));Sourcepub fn string<S>(s: S) -> JsonValue
pub fn string<S>(s: S) -> JsonValue
构造字符串
§示例
use evorule_tcb::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
构造数组
§示例
use evorule_tcb::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
构造对象
§示例
extern crate alloc;
use evorule_tcb::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
从键值对构造对象(转移所有权版本)
比 object_from_pairs 更高效,因为不需要克隆值。
§示例
use evorule_tcb::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
从键值对构造对象(借用版本,会克隆值)
注意:此方法会克隆所有值。如需避免克隆,请使用 object_from_pairs_owned。
§示例
use evorule_tcb::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
构造空对象
§示例
use evorule_tcb::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
构造空数组
§示例
use evorule_tcb::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 Ord for JsonValue
impl Ord for JsonValue
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialOrd for JsonValue
impl PartialOrd for JsonValue
Auto Trait Implementations§
impl Forget for JsonValue
impl Freeze for JsonValue
impl Move for JsonValue
impl RefUnwindSafe for JsonValue
impl Send for JsonValue
impl Sync for JsonValue
impl Unpin for JsonValue
impl UnsafeUnpin for JsonValue
impl UnwindSafe for JsonValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more