Skip to main content

SafeJsonValue

Struct SafeJsonValue 

Source
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

Source

pub fn new(value: Value) -> Self

Takes ownership of a JSON tree without traversing it or admitting it to a budget.

§Parameters
  • value - JSON tree whose ownership is transferred to this owner.
§Returns

An owner for the supplied tree. Construction does not copy or allocate.

Source

pub fn copy_from(value: &Value) -> Self

Copies an entire JSON tree using an explicit traversal stack.

This operation allocates work-stack storage and result storage for the copied tree; it does not impose a budget on the source tree.

§Parameters
  • value - JSON tree to copy.
§Returns

An independently owned copy of the complete tree.

Source

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.

Source

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

Source§

fn clone(&self) -> Self

Copies the complete JSON tree through the owner’s guarded traversal.

1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SafeJsonValue

Source§

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

Formats the complete JSON tree through the owner’s stack-safe formatter.

§Parameters
  • formatter - Formatter that receives the tree’s debug representation.
§Returns

The formatter’s result, including any formatting error.

Source§

impl<'de> Deserialize<'de> for SafeJsonValue

Source§

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

Source§

fn drop(&mut self)

Destroys the owned tree iteratively, with an allocation-free fallback.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for SafeJsonValue

Source§

impl PartialEq for SafeJsonValue

Source§

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

Compares two complete JSON trees without recursive descent.

§Parameters
  • other - JSON owner whose tree is compared with this owner’s tree.
§Returns

true when both trees contain equal JSON values.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for SafeJsonValue

Source§

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.

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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, 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.