#[non_exhaustive]pub enum JsonData {
Null,
Bool(bool),
Integer(i64),
Float(f64),
String(String),
Array(Vec<JsonData>),
Object(HashMap<String, JsonData>),
}Expand description
Domain-specific representation of JSON-like data This replaces serde_json::Value to maintain Clean Architecture principles
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Null
Null value
Bool(bool)
Boolean value
Integer(i64)
Integer value
Float(f64)
Float value (stored as f64 for simplicity)
String(String)
String value
Array(Vec<JsonData>)
Array of JsonData values
Object(HashMap<String, JsonData>)
Object with string keys and JsonData values
Implementations§
Source§impl JsonData
impl JsonData
Sourcepub fn float(value: f64) -> Result<JsonData, DomainError>
pub fn float(value: f64) -> Result<JsonData, DomainError>
Create a new float value.
Returns Err when value is NaN or infinite. JSON (RFC 8259 §6) does not
allow non-finite numbers, so a JsonData containing one could never be
serialized to valid JSON.
§Examples
use pjson_rs_domain::value_objects::JsonData;
assert!(JsonData::float(3.14).is_ok());
assert!(JsonData::float(f64::NAN).is_err());
assert!(JsonData::float(f64::INFINITY).is_err());Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Check if value is integer
Sourcepub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonData>>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<JsonData>>
Get mutable array value if this is an array
Sourcepub fn as_object(&self) -> Option<&HashMap<String, JsonData>>
pub fn as_object(&self) -> Option<&HashMap<String, JsonData>>
Get object value if this is an object
Sourcepub fn as_object_mut(&mut self) -> Option<&mut HashMap<String, JsonData>>
pub fn as_object_mut(&mut self) -> Option<&mut HashMap<String, JsonData>>
Get mutable object value if this is an object
Sourcepub fn set_path(&mut self, path: &str, value: JsonData) -> bool
pub fn set_path(&mut self, path: &str, value: JsonData) -> bool
Set nested value by path (dot notation)
Sourcepub fn memory_size(&self) -> usize
pub fn memory_size(&self) -> usize
Estimate memory size in bytes
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JsonData
Deserializes JSON input directly into JsonData, skipping the
intermediate serde_json::Value tree that From<serde_json::Value>
would otherwise require building and then walking a second time.
impl<'de> Deserialize<'de> for JsonData
Deserializes JSON input directly into JsonData, skipping the
intermediate serde_json::Value tree that From<serde_json::Value>
would otherwise require building and then walking a second time.
The implementation drives a Visitor through
Deserializer::deserialize_any, so it works with any self-describing
format (not just serde_json) and constructs each JsonData variant
in a single pass over the input.
§Examples
use pjson_rs_domain::value_objects::JsonData;
let data: JsonData = serde_json::from_str(r#"{"a": 1, "b": [true, null]}"#).unwrap();
assert!(data.is_object());
assert_eq!(data.get("a").and_then(JsonData::as_i64), Some(1));Source§fn deserialize<D>(
deserializer: D,
) -> Result<JsonData, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<JsonData, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
impl Eq for JsonData
Source§impl Serialize for JsonData
Serializes JsonData as a plain JSON value rather than as a Rust enum
(which would wrap every non-unit variant in a {"VariantName": ...}
tag). This keeps the wire representation identical to what a client
actually sent, and symmetric with the Deserialize impl below, which
expects plain JSON on the way in.
impl Serialize for JsonData
Serializes JsonData as a plain JSON value rather than as a Rust enum
(which would wrap every non-unit variant in a {"VariantName": ...}
tag). This keeps the wire representation identical to what a client
actually sent, and symmetric with the Deserialize impl below, which
expects plain JSON on the way in.
§Examples
use pjson_rs_domain::value_objects::JsonData;
use std::collections::HashMap;
let data = JsonData::object(HashMap::from([
("a".to_string(), JsonData::integer(1)),
]));
assert_eq!(serde_json::to_string(&data).unwrap(), r#"{"a":1}"#);Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl StructuralPartialEq for JsonData
Auto Trait Implementations§
impl Freeze for JsonData
impl RefUnwindSafe for JsonData
impl Send for JsonData
impl Sync for JsonData
impl Unpin for JsonData
impl UnsafeUnpin for JsonData
impl UnwindSafe for JsonData
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more