Skip to main content

DataValue

Enum DataValue 

Source
pub enum DataValue<'a> {
    Null,
    Bool(bool),
    Number(NumberValue),
    String(&'a str),
    Array(&'a [DataValue<'a>]),
    Object(&'a [(&'a str, DataValue<'a>)]),
}
Expand description

Arena-allocated JSON value tree. Mirrors serde_json::Value in shape and access surface, but every composite payload lives in a Bump.

Variants§

§

Null

§

Bool(bool)

§

Number(NumberValue)

§

String(&'a str)

§

Array(&'a [DataValue<'a>])

§

Object(&'a [(&'a str, DataValue<'a>)])

Implementations§

Source§

impl DataValue<'_>

Source

pub fn write_json_into(&self, out: &mut Vec<u8>)

Append the compact JSON encoding of this value to out. Useful when you want to amortize allocation across many values into a shared buffer. For one-shot string conversion, use the fmt::Display impl (v.to_string() / format!("{v}") / println!("{v}")).

Source

pub fn pretty(&self) -> Pretty<'_, DataValue<'_>>

Pretty-print wrapper. format!("{}", v.pretty()) produces the same two-space-indented layout as serde_json::to_string_pretty.

use bumpalo::Bump;
use datavalue_rs::DataValue;

let arena = Bump::new();
let v = DataValue::from_str(r#"{"a":1}"#, &arena).unwrap();
assert_eq!(v.pretty().to_string(), "{\n  \"a\": 1\n}");
Source

pub fn write_json_pretty_into(&self, out: &mut Vec<u8>)

Append the pretty JSON encoding of this value to out.

Source§

impl<'a> DataValue<'a>

Source

pub fn to_owned(&self) -> OwnedDataValue

Deep-clone this arena-bound tree into an OwnedDataValue that no longer references the arena.

Source§

impl<'a> DataValue<'a>

Source

pub fn from_str( input: &'a str, arena: &'a Bump, ) -> Result<DataValue<'a>, ParseError>

Parse a JSON document into a DataValue tree allocated in arena.

Strings without escape sequences are borrowed directly from input (the returned tree’s lifetime is the shorter of input and arena).

Source§

impl<'a> DataValue<'a>

Source

pub fn null() -> Self

Source

pub fn bool(b: bool) -> Self

Source

pub fn from_i64(i: i64) -> Self

Source

pub fn from_f64(f: f64) -> Self

Source

pub fn from_str_in(s: &str, arena: &'a Bump) -> Self

Source

pub fn from_borrowed_str(s: &'a str) -> Self

Wrap a string slice that already lives in the arena (or has the required lifetime). No allocation.

Source

pub fn is_null(&self) -> bool

Source

pub fn is_bool(&self) -> bool

Source

pub fn is_number(&self) -> bool

Source

pub fn is_i64(&self) -> bool

Source

pub fn is_f64(&self) -> bool

Source

pub fn is_string(&self) -> bool

Source

pub fn is_array(&self) -> bool

Source

pub fn is_object(&self) -> bool

Source

pub fn as_bool(&self) -> Option<bool>

Source

pub fn as_i64(&self) -> Option<i64>

Source

pub fn as_f64(&self) -> Option<f64>

Source

pub fn as_number(&self) -> Option<&NumberValue>

Source

pub fn as_str(&self) -> Option<&'a str>

Source

pub fn as_array(&self) -> Option<&'a [DataValue<'a>]>

Source

pub fn as_object(&self) -> Option<&'a [(&'a str, DataValue<'a>)]>

Source

pub fn get<I: ValueIndex>(&self, index: I) -> Option<&DataValue<'a>>

serde_json::Value::get-style lookup. Accepts &str for object keys or usize for array indices.

Source

pub fn len(&self) -> Option<usize>

Number of elements in an array / object. None for non-collections.

Source

pub fn is_empty(&self) -> Option<bool>

Source

pub fn members(&self) -> Iter<'_, DataValue<'a>>

Iterate array items. Returns an empty iterator if self is not an array — same convenience pattern as json-rust’s members.

Source

pub fn entries(&self) -> EntriesIter<'_, 'a>

Iterate object entries as (key, value) pairs in insertion order. Returns an empty iterator if self is not an object.

Source

pub fn to_json_string(&self) -> String

Serialise to a compact JSON string. Equivalent to format!("{self}") / self.to_string() — provided as the conventional name people reach for, and so callers don’t have to import std::fmt::Write.

Trait Implementations§

Source§

impl<'a> Clone for DataValue<'a>

Source§

fn clone(&self) -> DataValue<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for DataValue<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for DataValue<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for DataValue<'_>

Source§

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

Compact JSON. Same shape as serde_json::to_string.

use bumpalo::Bump;
use datavalue_rs::DataValue;

let arena = Bump::new();
let v = DataValue::from_str(r#"{"a":[1,2.5,"hi"]}"#, &arena).unwrap();
assert_eq!(v.to_string(), r#"{"a":[1,2.5,"hi"]}"#);
Source§

impl<'a, I: ValueIndex> Index<I> for DataValue<'a>

Source§

type Output = DataValue<'a>

The returned type after indexing.
Source§

fn index(&self, index: I) -> &DataValue<'a>

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> PartialEq for DataValue<'a>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for DataValue<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for DataValue<'a>

§

impl<'a> RefUnwindSafe for DataValue<'a>

§

impl<'a> Send for DataValue<'a>

§

impl<'a> Sync for DataValue<'a>

§

impl<'a> Unpin for DataValue<'a>

§

impl<'a> UnsafeUnpin for DataValue<'a>

§

impl<'a> UnwindSafe for DataValue<'a>

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.