pub enum Value {
    Null,
    Boolean(bool),
    Number(NumberBuf<SmallVec<[u8; 16]>>),
    String(SmallString<[u8; 16]>),
    Array(Vec<Value>),
    Object(Object),
}
Expand description

JSON Value.

§Parsing

You can parse a Value by importing the Parse trait providing a collection of parsing functions.

§Example

use json_syntax::{Value, Parse, CodeMap};
let (value, code_map) = Value::parse_str("{ \"key\": \"value\" }").unwrap();

The code_map value of type CodeMap contains code-mapping information about all the fragments of the JSON value (their location in the source text).

§Comparison

This type implements the usual comparison traits PartialEq, Eq, PartialOrd and Ord. However by default JSON object entries ordering matters, meaning that { "a": 0, "b": 1 } is not equal to { "b": 1, "a": 0 }. If you want to do comparisons while ignoring entries ordering, you can use the Unordered type (combined with the UnorderedPartialEq trait). Any T reference can be turned into an Unordered<T> reference at will using the BorrowUnordered::as_unordered method.

§Example

use json_syntax::{json, Unordered, BorrowUnordered};

let a = json!({ "a": 0, "b": 1 });
let b = json!({ "b": 1, "a": 0 });

assert_ne!(a, b); // not equals entries are in a different order.
assert_eq!(a.as_unordered(), b.as_unordered()); // equals modulo entry order.
assert_eq!(Unordered(a), Unordered(b)); // equals modulo entry order.

§Printing

The Print trait provide a highly configurable printing method.

§Example

use json_syntax::{Value, Parse, Print};

let value = Value::parse_str("[ 0, 1, { \"key\": \"value\" }, null ]").unwrap().0;

println!("{}", value.pretty_print()); // multi line, indent with 2 spaces
println!("{}", value.inline_print()); // single line, spaces
println!("{}", value.compact_print()); // single line, no spaces

let mut options = json_syntax::print::Options::pretty();
options.indent = json_syntax::print::Indent::Tabs(1);
println!("{}", value.print_with(options)); // multi line, indent with tabs

Variants§

§

Null

null.

§

Boolean(bool)

Boolean true or false.

§

Number(NumberBuf<SmallVec<[u8; 16]>>)

Number.

§

String(SmallString<[u8; 16]>)

String.

§

Array(Vec<Value>)

Array.

§

Object(Object)

Object.

Implementations§

source§

impl Value

source

pub fn get_fragment(&self, index: usize) -> Result<FragmentRef<'_>, usize>

source

pub fn kind(&self) -> Kind

source

pub fn is_kind(&self, kind: Kind) -> bool

source

pub fn is_null(&self) -> bool

source

pub fn is_boolean(&self) -> bool

source

pub fn is_number(&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 is_empty_array_or_object(&self) -> bool

Checks if the value is either an empty array or an empty object.

source

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

source

pub fn as_boolean_mut(&mut self) -> Option<&mut bool>

source

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

source

pub fn as_number_mut(&mut self) -> Option<&mut NumberBuf<SmallVec<[u8; 16]>>>

source

pub fn as_string(&self) -> Option<&str>

source

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

Alias for as_string.

source

pub fn as_string_mut(&mut self) -> Option<&mut SmallString<[u8; 16]>>

source

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

source

pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>

source

pub fn force_as_array(&self) -> &[Value]

Return the given value as an array, even if it is not an array.

Returns the input value as is if it is already an array, or puts it in a slice with a single element if it is not.

source

pub fn as_object(&self) -> Option<&Object>

source

pub fn as_object_mut(&mut self) -> Option<&mut Object>

source

pub fn into_boolean(self) -> Option<bool>

source

pub fn into_number(self) -> Option<NumberBuf<SmallVec<[u8; 16]>>>

source

pub fn into_string(self) -> Option<SmallString<[u8; 16]>>

source

pub fn into_array(self) -> Option<Vec<Value>>

source

pub fn into_object(self) -> Option<Object>

source

pub fn traverse(&self) -> Traverse<'_>

source

pub fn count(&self, f: impl FnMut(usize, FragmentRef<'_>) -> bool) -> usize

Recursively count the number of values for which f returns true.

source

pub fn volume(&self) -> usize

Returns the volume of the value.

The volume is the sum of all values and recursively nested values included in self, including self (the volume is at least 1).

This is equivalent to value.traverse().filter(|(_, f)| f.is_value()).count().

source

pub fn take(&mut self) -> Value

Move and return the value, leaves null in its place.

Trait Implementations§

source§

impl Clone for Value

source§

fn clone(&self) -> Value

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Compare for Value

source§

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

source§

impl Debug for Value

source§

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

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

impl Display for Value

source§

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

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

impl<'n> From<&'n Number> for Value

source§

fn from(n: &'n Number) -> Value

Converts to this type from the input type.
source§

impl<'s> From<&'s str> for Value

source§

fn from(s: &'s str) -> Value

Converts to this type from the input type.
source§

impl From<NumberBuf<SmallVec<[u8; 16]>>> for Value

source§

fn from(n: NumberBuf<SmallVec<[u8; 16]>>) -> Value

Converts to this type from the input type.
source§

impl From<Object> for Value

source§

fn from(o: Object) -> Value

Converts to this type from the input type.
source§

impl From<SmallString<[u8; 16]>> for Value

source§

fn from(s: SmallString<[u8; 16]>) -> Value

Converts to this type from the input type.
source§

impl From<String> for Value

source§

fn from(s: String) -> Value

Converts to this type from the input type.
source§

impl From<Vec<Value>> for Value

source§

fn from(a: Vec<Value>) -> Value

Converts to this type from the input type.
source§

impl From<bool> for Value

source§

fn from(b: bool) -> Value

Converts to this type from the input type.
source§

impl From<i16> for Value

source§

fn from(n: i16) -> Value

Converts to this type from the input type.
source§

impl From<i32> for Value

source§

fn from(n: i32) -> Value

Converts to this type from the input type.
source§

impl From<i64> for Value

source§

fn from(n: i64) -> Value

Converts to this type from the input type.
source§

impl From<i8> for Value

source§

fn from(n: i8) -> Value

Converts to this type from the input type.
source§

impl From<u16> for Value

source§

fn from(n: u16) -> Value

Converts to this type from the input type.
source§

impl From<u32> for Value

source§

fn from(n: u32) -> Value

Converts to this type from the input type.
source§

impl From<u64> for Value

source§

fn from(n: u64) -> Value

Converts to this type from the input type.
source§

impl From<u8> for Value

source§

fn from(n: u8) -> Value

Converts to this type from the input type.
source§

impl FromStr for Value

§

type Err = Error

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

fn from_str(s: &str) -> Result<Value, <Value as FromStr>::Err>

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

impl Hash for Value

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Value

source§

fn cmp(&self, other: &Value) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl Parse for Value

source§

fn parse_in<C, E>( parser: &mut Parser<C, E>, context: Context ) -> Result<Meta<Value, usize>, Error<E>>
where C: Iterator<Item = Result<DecodedChar, E>>,

source§

fn parse_slice(content: &[u8]) -> Result<(Self, CodeMap), Error>

source§

fn parse_slice_with( content: &[u8], options: Options ) -> Result<(Self, CodeMap), Error>

source§

fn parse_str(content: &str) -> Result<(Self, CodeMap), Error>

source§

fn parse_str_with( content: &str, options: Options ) -> Result<(Self, CodeMap), Error>

source§

fn parse_infallible_utf8<C>(chars: C) -> Result<(Self, CodeMap), Error>
where C: Iterator<Item = char>,

source§

fn parse_utf8_infallible_with<C>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error>
where C: Iterator<Item = char>,

source§

fn parse_utf8<C, E>(chars: C) -> Result<(Self, CodeMap), Error<E>>
where C: Iterator<Item = Result<char, E>>,

source§

fn parse_utf8_with<C, E>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error<E>>
where C: Iterator<Item = Result<char, E>>,

source§

fn parse_infallible<C>(chars: C) -> Result<(Self, CodeMap), Error>
where C: Iterator<Item = DecodedChar>,

source§

fn parse_infallible_with<C>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error>
where C: Iterator<Item = DecodedChar>,

source§

fn parse<C, E>(chars: C) -> Result<(Self, CodeMap), Error<E>>
where C: Iterator<Item = Result<DecodedChar, E>>,

source§

fn parse_with<C, E>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error<E>>
where C: Iterator<Item = Result<DecodedChar, E>>,

source§

impl PartialEq for Value

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Value

source§

fn partial_cmp(&self, other: &Value) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PrecomputeSize for Value

source§

fn pre_compute_size(&self, options: &Options, sizes: &mut Vec<Size>) -> Size

source§

impl Print for Value

source§

fn fmt_with( &self, f: &mut Formatter<'_>, options: &Options, indent: usize ) -> Result<(), Error>

source§

fn pretty_print(&self) -> Printed<'_, Self>

Print the value with Options::pretty options.
source§

fn compact_print(&self) -> Printed<'_, Self>

Print the value with Options::compact options.
source§

fn inline_print(&self) -> Printed<'_, Self>

Print the value with Options::inline options.
source§

fn print_with(&self, options: Options) -> Printed<'_, Self>

Print the value with the given options.
source§

impl PrintWithSize for Value

source§

fn fmt_with_size( &self, f: &mut Formatter<'_>, options: &Options, indent: usize, sizes: &[Size], index: &mut usize ) -> Result<(), Error>

source§

impl TryFrom<f32> for Value

§

type Error = TryFromFloatError

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

fn try_from(n: f32) -> Result<Value, <Value as TryFrom<f32>>::Error>

Performs the conversion.
source§

impl TryFrom<f64> for Value

§

type Error = TryFromFloatError

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

fn try_from(n: f64) -> Result<Value, <Value as TryFrom<f64>>::Error>

Performs the conversion.
source§

impl UnorderedPartialEq for Value

source§

fn unordered_eq(&self, other: &Value) -> bool

source§

impl Eq for Value

source§

impl StructuralPartialEq for Value

source§

impl UnorderedEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

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> At for T

source§

fn at<M>(self, metadata: M) -> Meta<T, M>

Wraps self inside a Meta<Self, M> using the given metadata. 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> BorrowStripped for T

source§

fn stripped(&self) -> &Stripped<T>

source§

impl<T> BorrowUnordered for T

source§

impl<T> CallHasher for T
where T: Hash + ?Sized,

source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64
where H: Hash + ?Sized, B: BuildHasher,

source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, C> FromWithContext<T, C> for T

source§

fn from_with(value: T, _context: &C) -> T

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, U, C> IntoWithContext<U, C> for T
where U: FromWithContext<T, C>,

source§

fn into_with(self, context: &C) -> U

source§

impl<T> ToOwned for T
where T: Clone,

§

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§

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

§

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, C> TryFromWithContext<U, C> for T
where U: IntoWithContext<T, C>,

§

type Error = Infallible

source§

fn try_from_with( value: U, context: &C ) -> Result<T, <T as TryFromWithContext<U, C>>::Error>

source§

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

§

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

impl<T, U, C> TryIntoWithContext<U, C> for T
where U: TryFromWithContext<T, C>,

§

type Error = <U as TryFromWithContext<T, C>>::Error

source§

fn try_into_with( self, context: &C ) -> Result<U, <T as TryIntoWithContext<U, C>>::Error>

source§

impl<T> WithContext for T
where T: ?Sized,

source§

fn with<C>(&self, context: C) -> Contextual<&T, C>

source§

fn into_with<C>(self, context: C) -> Contextual<T, C>