pub mod ext;
pub mod types;
#[cfg(feature = "serde_json")]
mod serde_json;
#[cfg(feature = "serde_json")]
pub use serde_json::SerdeJson;
use std::borrow::Cow;
use ::serde_json::Value;
use crate::types::JsonType;
pub trait Json: Sized + Send + Sync + 'static {
type Node<'a>: JsonNode<'a, Self>;
type PreparedKey: Send + Sync;
fn prepare_key(key: &str) -> Self::PreparedKey;
}
pub trait JsonNode<'a, F: Json>: Clone {
type Object: JsonObjectAccess<'a, F, Node = Self>;
type Array: JsonArrayAccess<'a, F, Node = Self>;
fn as_object(&self) -> Option<Self::Object>;
fn as_array(&self) -> Option<Self::Array>;
fn as_string(&self) -> Option<Cow<'a, str>>;
fn as_number(&self) -> Option<Cow<'a, ::serde_json::Number>>;
fn as_boolean(&self) -> Option<bool>;
fn is_null(&self) -> bool;
fn is_number(&self) -> bool {
self.as_number().is_some()
}
fn is_string(&self) -> bool {
self.json_type() == JsonType::String
}
fn json_type(&self) -> JsonType;
fn string_length(&self) -> Option<u64>;
fn equals_value(&self, expected: &Value) -> bool;
fn to_value(&self) -> Cow<'a, Value>;
fn cache_key(&self) -> Option<usize>;
fn container_cache_key(&self) -> Option<usize> {
if matches!(self.json_type(), JsonType::Object | JsonType::Array) {
self.cache_key()
} else {
None
}
}
}
pub trait JsonObjectAccess<'a, F: Json> {
type Node: JsonNode<'a, F>;
type MemberName: AsRef<str> + Into<Cow<'a, str>>;
type MembersIter: Iterator<Item = (Self::MemberName, Self::Node)>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn get(&self, key: &F::PreparedKey) -> Option<Self::Node>;
fn members(&self) -> Self::MembersIter;
}
#[allow(clippy::len_without_is_empty)]
pub trait JsonArrayAccess<'a, F: Json> {
type Node: JsonNode<'a, F>;
type ElementsIter: Iterator<Item = Self::Node>;
fn len(&self) -> usize;
fn elements(&self) -> Self::ElementsIter;
fn is_unique(&self) -> bool;
}