Skip to main content

OwnedDataValue

Enum OwnedDataValue 

Source
pub enum OwnedDataValue {
    Null,
    Bool(bool),
    Number(NumberValue),
    String(String),
    Array(Vec<OwnedDataValue>),
    Object(Vec<(String, OwnedDataValue)>),
}
Expand description

Heap-owned JSON value tree. Variants mirror DataValue one-for-one; no lifetime parameter.

Variants§

§

Null

§

Bool(bool)

§

Number(NumberValue)

§

String(String)

§

Array(Vec<OwnedDataValue>)

§

Object(Vec<(String, OwnedDataValue)>)

Implementations§

Source§

impl OwnedDataValue

Source

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

Append the compact JSON encoding of this value to out. See DataValue::write_json_into; this is the owned-side mirror.

Source

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

Pretty-print wrapper; see DataValue::pretty.

use datavalue_rs::OwnedDataValue;

let v: OwnedDataValue = r#"{"a":1}"#.parse().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 OwnedDataValue

Source

pub fn from_json(input: &str) -> Result<Self, ParseError>

Parse JSON into an OwnedDataValue. Internally parses into a throwaway arena (using the fast hand-rolled parser) and deep-clones the result out — so JSON parsing speed matches DataValue::from_str, minus the deep-clone tail.

Also available via the std::str::FromStr trait.

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<&str>

Source

pub fn as_array(&self) -> Option<&[OwnedDataValue]>

Source

pub fn as_object(&self) -> Option<&[(String, OwnedDataValue)]>

Source

pub fn get<I: OwnedValueIndex>(&self, index: I) -> Option<&OwnedDataValue>

serde_json::Value::get-style lookup.

Source

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

Source

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

Source

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

Iterate array items. Returns an empty iterator if self is not an array.

Source

pub fn entries(&self) -> OwnedEntriesIter<'_>

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.

Source

pub fn to_arena<'a>(&self, arena: &'a Bump) -> DataValue<'a>

Borrow this owned tree into the given arena, returning a DataValue view. Strings are arena-allocated copies.

Array/Object use alloc_slice_fill_with rather than bumpalo::Vec::with_capacity_in + push: one pre-sized arena allocation and a tight write loop, skipping the Vec wrapper’s per-push capacity check and the Layout::array validation that RawVec::allocate_in re-runs for each nested allocation.

Source§

impl OwnedDataValue

Source

pub fn from_i64(i: i64) -> Self

Source

pub fn from_f64(f: f64) -> Self

Source

pub fn array<I, V>(items: I) -> Self
where I: IntoIterator<Item = V>, V: Into<OwnedDataValue>,

Build an OwnedDataValue::Array from anything iterable into values that already convert into OwnedDataValue (covers all the existing From<bool|i64|f64|String|&str|...> impls).

use datavalue_rs::OwnedDataValue;
let v = OwnedDataValue::array([1, 2, 3]);
assert_eq!(v[0].as_i64(), Some(1));
let v = OwnedDataValue::array(["a", "b"]);
assert_eq!(v[1].as_str(), Some("b"));
Source

pub fn object<I, K, V>(pairs: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<String>, V: Into<OwnedDataValue>,

Build an OwnedDataValue::Object from (key, value) pairs.

use datavalue_rs::OwnedDataValue;
let v = OwnedDataValue::object([("type", "NaN"), ("op", "+")]);
assert_eq!(v["type"].as_str(), Some("NaN"));
let v = OwnedDataValue::object([("count", 42)]);
assert_eq!(v["count"].as_i64(), Some(42));

Trait Implementations§

Source§

impl Clone for OwnedDataValue

Source§

fn clone(&self) -> OwnedDataValue

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 Debug for OwnedDataValue

Source§

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

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

impl Default for OwnedDataValue

Source§

fn default() -> OwnedDataValue

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

impl Display for OwnedDataValue

Source§

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

Compact JSON. Same shape as serde_json::to_string.

use datavalue_rs::OwnedDataValue;

let v: OwnedDataValue = r#"{"a":[1,2.5,"hi"]}"#.parse().unwrap();
assert_eq!(v.to_string(), r#"{"a":[1,2.5,"hi"]}"#);
Source§

impl<T: Into<OwnedDataValue> + Clone> From<&[T]> for OwnedDataValue

Source§

fn from(v: &[T]) -> Self

Converts to this type from the input type.
Source§

impl From<&String> for OwnedDataValue

Source§

fn from(s: &String) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for OwnedDataValue

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<OwnedDataValue>, const N: usize> From<[T; N]> for OwnedDataValue

Source§

fn from(v: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl From<()> for OwnedDataValue

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl<K: Into<String>, V: Into<OwnedDataValue>> From<BTreeMap<K, V>> for OwnedDataValue

Source§

fn from(m: BTreeMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl From<Cow<'_, str>> for OwnedDataValue

Source§

fn from(s: Cow<'_, str>) -> Self

Converts to this type from the input type.
Source§

impl<K: Into<String>, V: Into<OwnedDataValue>> From<HashMap<K, V>> for OwnedDataValue

Source§

fn from(m: HashMap<K, V>) -> Self

Note: HashMap iteration order is unspecified, so the resulting Object has unspecified key order. Equality is by key set, so this still round-trips correctly through PartialEq.

Source§

impl<T: Into<OwnedDataValue>> From<Option<T>> for OwnedDataValue

Source§

fn from(opt: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for OwnedDataValue

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<(String, OwnedDataValue)>> for OwnedDataValue

Source§

fn from(pairs: Vec<(String, OwnedDataValue)>) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<OwnedDataValue>> From<Vec<T>> for OwnedDataValue

Source§

fn from(v: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for OwnedDataValue

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for OwnedDataValue

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for OwnedDataValue

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for OwnedDataValue

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for OwnedDataValue

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for OwnedDataValue

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for OwnedDataValue

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for OwnedDataValue

Source§

fn from(v: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for OwnedDataValue

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for OwnedDataValue

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for OwnedDataValue

Source§

fn from(v: u64) -> Self

Values up to i64::MAX stay on the integer path; larger values fall back to f64 (matches the parser / serde visitor behaviour).

Source§

impl From<u8> for OwnedDataValue

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for OwnedDataValue

Source§

fn from(v: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for OwnedDataValue

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<I: OwnedValueIndex> Index<I> for OwnedDataValue

Source§

type Output = OwnedDataValue

The returned type after indexing.
Source§

fn index(&self, index: I) -> &OwnedDataValue

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

impl PartialEq for OwnedDataValue

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.

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