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

Value.

The type parameter M is the type of metadata attached to each syntax node (values, sub values and object keys). The metadata is derived from the locspan::Span of the node in the source document using the metadata builder function passed to the parsing function (see the Parse trait for more details).

Parsing

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

Example

use json_syntax::{Value, Parse};
use locspan::{Meta, Span};

let value: Meta<Value<Span>, Span> = Value::parse_str("{ \"key\": \"value\" }", |span| span).unwrap();

You don’t need to specify the return type, here only shown to make it clear. Furthermore the MetaValue<Span> type alias can be used instead of Meta<Value<Span>, Span> to avoid repetition of the metadata type.

Comparison

This type implements the usual comparison traits PartialEq, Eq, PartialOrd and Ord. However these implementations will also compare the metadata. If you want to do comparisons while ignoring ths metadata, you can use the locspan::Stripped type (combined with the locspan::BorrowStripped trait).

Example

use json_syntax::{Value, Parse};
use locspan::BorrowStripped;

let a = Value::parse_str("null", |_| 0).unwrap();
let b = Value::parse_str("null", |_| 1).unwrap();

assert_ne!(a, b); // not equals because the metadata is different.
assert_eq!(a.stripped(), b.stripped()); // equals because the metadata is ignored.

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 ]", |span| span).unwrap();

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<Meta<Value<M>, M>, Global>)

Array.

§

Object(Object<M>)

Object.

Implementations§

source§

impl<M> Value<M>

source

pub fn from_serde_json( value: Value, f: impl Clone + Fn(SerdeJsonFragment<'_>) -> M ) -> Meta<Value<M>, M>

Converts a serde_json::Value into a Value.

The function f is used to annotate the output each sub value (passed as parameter).

source

pub fn into_serde_json(_: Meta<Value<M>, M>) -> Value

Converts a Value into a serde_json::Value, stripping the metadata.

source§

impl<M> Value<M>

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<&[Meta<Value<M>, M>]>

source

pub fn as_array_mut(&mut self) -> Option<&mut Vec<Meta<Value<M>, M>, Global>>

source

pub fn force_as_array( value: &Meta<Value<M>, M> ) -> Meta<&[Meta<Value<M>, M>], &M>

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

source

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

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<Meta<Value<M>, M>, Global>>

source

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

source

pub fn traverse(&self) -> TraverseStripped<'_, M>

source

pub fn count(&self, f: impl FnMut(StrippedFragmentRef<'_, M>) -> 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(StrippedFragmentRef::is_value).count().

source

pub fn map_metadata<N>(self, f: impl FnMut(M) -> N) -> Value<N>

Recursively maps the metadata inside the value.

source

pub fn try_map_metadata<N, E>( self, f: impl FnMut(M) -> Result<N, E> ) -> Result<Value<N>, E>

Tries to recursively maps the metadata inside the value.

source

pub fn take(&mut self) -> Value<M>

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

Trait Implementations§

source§

impl<M> Clone for Value<M>where M: Clone,

source§

fn clone(&self) -> Value<M>

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<M> Compare for Value<M>

source§

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

source§

impl<M> Debug for Value<M>where M: Debug,

source§

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

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

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<M> From<Object<M>> for Value<M>

source§

fn from(o: Object<M>) -> Value<M>

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<M> From<String> for Value<M>

source§

fn from(s: String) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<Value> for Value<M>where M: Default,

source§

fn from(value: Value) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<Vec<Meta<Value<M>, M>, Global>> for Value<M>

source§

fn from(a: Vec<Meta<Value<M>, M>, Global>) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<bool> for Value<M>

source§

fn from(b: bool) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<i16> for Value<M>

source§

fn from(n: i16) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<i32> for Value<M>

source§

fn from(n: i32) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<i64> for Value<M>

source§

fn from(n: i64) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<i8> for Value<M>

source§

fn from(n: i8) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<u16> for Value<M>

source§

fn from(n: u16) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<u32> for Value<M>

source§

fn from(n: u32) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<u64> for Value<M>

source§

fn from(n: u64) -> Value<M>

Converts to this type from the input type.
source§

impl<M> From<u8> for Value<M>

source§

fn from(n: u8) -> Value<M>

Converts to this type from the input type.
source§

impl<M> Hash for Value<M>where M: Hash,

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<M, N> MapMetadataRecursively<M, N> for Value<M>

§

type Output = Value<N>

source§

fn map_metadata_recursively<F>(self, f: F) -> Value<N>where F: FnMut(M) -> N,

Maps the metadata, recursively.
source§

impl<M> Ord for Value<M>where M: Ord,

source§

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

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

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

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

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

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

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

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

impl<M> Parse<M> for Value<M>

source§

fn parse_spanned<C, F, E>( parser: &mut Parser<C, F, E>, context: Context ) -> Result<Meta<Value<M>, Span>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<DecodedChar, E>>, F: FnMut(Span) -> M,

source§

fn parse_str<F>( content: &str, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where F: FnMut(Span) -> M,

source§

fn parse_str_with<F>( content: &str, options: Options, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where F: FnMut(Span) -> M,

source§

fn parse_infallible_utf8<C, F>( chars: C, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where C: Iterator<Item = char>, F: FnMut(Span) -> M,

source§

fn parse_utf8_infallible_with<C, F>( chars: C, options: Options, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where C: Iterator<Item = char>, F: FnMut(Span) -> M,

source§

fn parse_utf8<C, F, E>( chars: C, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<char, E>>, F: FnMut(Span) -> M,

source§

fn parse_utf8_with<C, F, E>( chars: C, options: Options, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<char, E>>, F: FnMut(Span) -> M,

source§

fn parse_infallible<C, F>( chars: C, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where C: Iterator<Item = DecodedChar>, F: FnMut(Span) -> M,

source§

fn parse_infallible_with<C, F>( chars: C, options: Options, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, Infallible>, M>>where C: Iterator<Item = DecodedChar>, F: FnMut(Span) -> M,

source§

fn parse<C, F, E>( chars: C, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<DecodedChar, E>>, F: FnMut(Span) -> M,

source§

fn parse_with<C, F, E>( chars: C, options: Options, metadata_builder: F ) -> Result<Meta<Self, M>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<DecodedChar, E>>, F: FnMut(Span) -> M,

source§

fn parse_in<C, F, E>( parser: &mut Parser<C, F, E>, context: Context ) -> Result<Meta<Self, M>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<DecodedChar, E>>, F: FnMut(Span) -> M,

source§

impl<M> PartialEq<Value<M>> for Value<M>where M: PartialEq<M>,

source§

fn eq(&self, other: &Value<M>) -> 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<M> PartialOrd<Value<M>> for Value<M>where M: PartialOrd<M>,

source§

fn partial_cmp(&self, other: &Value<M>) -> 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<M> PrecomputeSize for Value<M>

source§

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

source§

impl<M> Print for Value<M>

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<M> PrintWithSize for Value<M>

source§

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

source§

impl<M> StrippedHash for Value<M>

source§

fn stripped_hash<H>(&self, state: &mut H)where H: Hasher,

source§

impl<M> StrippedOrd for Value<M>

source§

fn stripped_cmp(&self, other: &Value<M>) -> Ordering

source§

impl<M, __M> StrippedPartialEq<Value<__M>> for Value<M>

source§

fn stripped_eq(&self, other: &Value<__M>) -> bool

source§

impl<M, __M> StrippedPartialOrd<Value<__M>> for Value<M>

source§

impl<M> TryFrom<f32> for Value<M>

§

type Error = TryFromFloatError

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

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

Performs the conversion.
source§

impl<M> TryFrom<f64> for Value<M>

§

type Error = TryFromFloatError

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

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

Performs the conversion.
source§

impl<M, N, E> TryMapMetadataRecursively<M, N, E> for Value<M>

§

type Output = Value<N>

source§

fn try_map_metadata_recursively<F>(self, f: F) -> Result<Value<N>, E>where F: FnMut(M) -> Result<N, E>,

Tries to map the metadata, recursively.
source§

impl<M> Eq for Value<M>where M: Eq,

source§

impl<M> StrippedEq for Value<M>

source§

impl<M> StructuralEq for Value<M>

source§

impl<M> StructuralPartialEq for Value<M>

Auto Trait Implementations§

§

impl<M> RefUnwindSafe for Value<M>where M: RefUnwindSafe,

§

impl<M> Send for Value<M>where M: Send,

§

impl<M> Sync for Value<M>where M: Sync,

§

impl<M> Unpin for Value<M>where M: Unpin,

§

impl<M> UnwindSafe for Value<M>where M: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere 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 Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · 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§

fn unordered(&self) -> &Unordered<T>

§

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

§

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

source§

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

source§

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

source§

impl<Q, K> Equivalent<K> for Qwhere 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

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T, M> ValueOrParse<M> for Twhere T: Parse<M> + From<Value<M>>,

source§

fn value_or_parse<C, F, E>( value: Option<Meta<Value<M>, Span>>, parser: &mut Parser<C, F, E>, context: Context ) -> Result<Meta<T, Span>, Meta<Error<M, E>, M>>where C: Iterator<Item = Result<DecodedChar, E>>, F: FnMut(Span) -> M,

source§

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

source§

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

source§

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