pub struct SafeJsonValue { /* private fields */ }Expand description
An owned JSON value whose clone, equality, debug formatting, and drop use protected tree operations.
This protection does not extend to arbitrary recursive operations on a
borrowed or extracted serde_json::Value. as_value
returns a shared borrow tied to this owner; callers remain responsible for
the stack behavior of recursive operations they invoke through that borrow.
into_value gives up the protected owner and transfers
responsibility for the resulting tree’s lifetime to the caller.
Serde interoperability uses stacker and still depends on the concrete
serializer or deserializer, which may impose its own recursion limits. The
owner does not enforce an exact heap-memory limit.
Its deserializer accepts i128 and u128 events when their values fit the
JSON integer range, maps None to null, and transparently unwraps Some
and newtype payloads.
The tree protection and the meaning of values produced by Deserialize
are separate concerns. A direct Serde bridge cannot identify whether a
map-shaped event came from an ordinary object or an upstream deserializer’s
private number representation. With serde_json’s arbitrary_precision
feature, number shapes produced by that bridge can therefore differ from
those produced without the feature. When decoding JSON text and preserving
its numeric meaning is important, use JsonDecoder::decode_value_str
(or the corresponding byte and reader methods) instead.
Direct deserialization also rejects duplicate object keys after decoding
their string escapes. It does not normalize Unicode. In contrast,
Self::new takes an existing serde_json::Value as-is: if an upstream
deserializer has already merged duplicate keys, this type cannot recover
them.
§Examples
use qubit_json::value::SafeJsonValue;
use serde_json::json;
let owner = SafeJsonValue::new(json!({"items": [1, 2]}));
let copy = owner.clone();
assert_eq!(owner, copy);
assert_eq!(owner.as_value()["items"][0], 1);
let shallow_raw = copy.into_value();
assert!(shallow_raw.is_object());
assert!(serde_json::from_str::<SafeJsonValue>(r#"{"a":1,"\u0061":2}"#).is_err());Implementations§
Source§impl SafeJsonValue
impl SafeJsonValue
Sourcepub fn as_value(&self) -> &Value
pub fn as_value(&self) -> &Value
Returns a shared borrow of the underlying JSON tree for interoperability.
The returned borrow is tied to this owner. Recursive operations performed through it are the caller’s responsibility and may use the backend’s stack behavior.
§Returns
The underlying tree borrowed for no longer than this owner.
Sourcepub fn into_value(self) -> Value
pub fn into_value(self) -> Value
Transfers the tree and its destruction responsibility to the caller.
The returned Value is no longer protected by this owner; recursive
operations and destruction are the caller’s responsibility.
§Returns
The previously owned JSON tree.
Trait Implementations§
Source§impl Clone for SafeJsonValue
impl Clone for SafeJsonValue
Source§impl Debug for SafeJsonValue
impl Debug for SafeJsonValue
Source§impl<'de> Deserialize<'de> for SafeJsonValue
impl<'de> Deserialize<'de> for SafeJsonValue
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserializes a JSON value through the supplied Serde backend.
The traversal grows the stack when needed. Duplicate object keys are rejected by the visitor; the backend’s own errors are propagated.
§Parameters
deserializer- Backend providing the input value.
§Type Parameters
D- Serde deserializer that defines the input and error types.
§Returns
A protected owner for the deserialized JSON tree.
§Errors
Returns the backend or visitor error if decoding fails.
Source§impl Drop for SafeJsonValue
impl Drop for SafeJsonValue
impl Eq for SafeJsonValue
Source§impl Serialize for SafeJsonValue
impl Serialize for SafeJsonValue
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serializes the protected JSON tree through the supplied Serde backend.
The traversal grows the stack when needed. Errors from scalar, container, or nested serializer operations are returned unchanged.
§Parameters
serializer- Backend that receives the serialized JSON value.
§Type Parameters
S- Serde serializer that defines the output and error types.
§Returns
The value produced by the serializer on success.
§Errors
Returns the backend’s error if any serialization operation fails.