Enum json_syntax::Value
source · [−]pub enum Value<M> {
Null,
Boolean(bool),
Number(NumberBuf),
String(String),
Array(Array<M>),
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)
Number.
String(String)
String.
Array(Array<M>)
Array.
Object(Object<M>)
Object.
Implementations
sourceimpl<M> Value<M>
impl<M> Value<M>
pub fn kind(&self) -> Kind
pub fn is_kind(&self, kind: Kind) -> bool
pub fn is_null(&self) -> bool
pub fn is_boolean(&self) -> bool
pub fn is_number(&self) -> bool
pub fn is_string(&self) -> bool
pub fn is_array(&self) -> bool
pub fn is_object(&self) -> bool
pub fn as_boolean(&self) -> Option<bool>
pub fn as_boolean_mut(&mut self) -> Option<&mut bool>
pub fn as_number(&self) -> Option<&Number>
pub fn as_number_mut(&mut self) -> Option<&mut NumberBuf>
pub fn as_string(&self) -> Option<&str>
pub fn as_string_mut(&mut self) -> Option<&mut String>
pub fn as_array(&self) -> Option<&[Meta<Self, M>]>
pub fn as_array_mut(&mut self) -> Option<&mut Array<M>>
pub fn as_object(&self) -> Option<&Object<M>>
pub fn as_object_mut(&mut self) -> Option<&mut Object<M>>
pub fn into_boolean(self) -> Option<bool>
pub fn into_number(self) -> Option<NumberBuf>
pub fn into_string(self) -> Option<String>
pub fn into_array(self) -> Option<Array<M>>
pub fn into_object(self) -> Option<Object<M>>
pub fn traverse(&self) -> TraverseStripped<'_, M>ⓘNotable traits for TraverseStripped<'a, M>impl<'a, M> Iterator for TraverseStripped<'a, M> type Item = StrippedFragmentRef<'a, M>;
sourcepub fn count(&self, f: impl FnMut(StrippedFragmentRef<'_, M>) -> bool) -> usize
pub fn count(&self, f: impl FnMut(StrippedFragmentRef<'_, M>) -> bool) -> usize
Recursively count the number of values for which f
returns true
.
sourcepub fn volume(&self) -> usize
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()
.
sourcepub fn map_metadata<N>(self, f: impl FnMut(M) -> N) -> Value<N>
pub fn map_metadata<N>(self, f: impl FnMut(M) -> N) -> Value<N>
Recursively maps the metadata inside the value.
Trait Implementations
sourceimpl<M, N> MapMetadataRecursively<M, N> for Value<M>
impl<M, N> MapMetadataRecursively<M, N> for Value<M>
sourceimpl<M: Ord> Ord for Value<M>
impl<M: Ord> Ord for Value<M>
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<M> Parse<M> for Value<M>
impl<M> Parse<M> for Value<M>
fn parse_spanned<C, F, E>(
parser: &mut Parser<C, F, E>,
context: Context
) -> Result<Meta<Self, Span>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<DecodedChar, E>>,
F: FnMut(Span) -> M,
fn parse_str<F>(
content: &str,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
F: FnMut(Span) -> M,
fn parse_str_with<F>(
content: &str,
options: Options,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
F: FnMut(Span) -> M,
fn parse_infallible_utf8<C, F>(
chars: C,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
C: Iterator<Item = char>,
F: FnMut(Span) -> M,
fn parse_utf8_infallible_with<C, F>(
chars: C,
options: Options,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
C: Iterator<Item = char>,
F: FnMut(Span) -> M,
fn parse_utf8<C, F, E>(
chars: C,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<char, E>>,
F: FnMut(Span) -> M,
fn parse_utf8_with<C, F, E>(
chars: C,
options: Options,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<char, E>>,
F: FnMut(Span) -> M,
fn parse_infallible<C, F>(
chars: C,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
C: Iterator<Item = DecodedChar>,
F: FnMut(Span) -> M,
fn parse_infallible_with<C, F>(
chars: C,
options: Options,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<Infallible, M>, M>> where
C: Iterator<Item = DecodedChar>,
F: FnMut(Span) -> M,
fn parse<C, F, E>(
chars: C,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<DecodedChar, E>>,
F: FnMut(Span) -> M,
fn parse_with<C, F, E>(
chars: C,
options: Options,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<DecodedChar, E>>,
F: FnMut(Span) -> M,
fn parse_in<C, F, E>(
parser: &mut Parser<C, F, E>,
context: Context
) -> Result<Meta<Self, M>, Meta<Error<E, M>, M>> where
C: Iterator<Item = Result<DecodedChar, E>>,
F: FnMut(Span) -> M,
sourceimpl<M: PartialEq> PartialEq<Value<M>> for Value<M>
impl<M: PartialEq> PartialEq<Value<M>> for Value<M>
sourceimpl<M: PartialOrd> PartialOrd<Value<M>> for Value<M>
impl<M: PartialOrd> PartialOrd<Value<M>> for Value<M>
sourcefn partial_cmp(&self, other: &Value<M>) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl<M> PrecomputeSize for Value<M>
impl<M> PrecomputeSize for Value<M>
sourceimpl<M> Print for Value<M>
impl<M> Print for Value<M>
fn fmt_with(
&self,
f: &mut Formatter<'_>,
options: &Options,
indent: usize
) -> Result
sourcefn pretty_print(&self) -> Printed<'_, Self>
fn pretty_print(&self) -> Printed<'_, Self>
Print the value with Options::pretty
options.
sourcefn compact_print(&self) -> Printed<'_, Self>
fn compact_print(&self) -> Printed<'_, Self>
Print the value with Options::compact
options.
sourcefn inline_print(&self) -> Printed<'_, Self>
fn inline_print(&self) -> Printed<'_, Self>
Print the value with Options::inline
options.
sourcefn print_with(&self, options: Options) -> Printed<'_, Self>
fn print_with(&self, options: Options) -> Printed<'_, Self>
Print the value with the given options.
sourceimpl<M> PrintWithSize for Value<M>
impl<M> PrintWithSize for Value<M>
sourceimpl<M> StrippedHash for Value<M>
impl<M> StrippedHash for Value<M>
fn stripped_hash<H: Hasher>(&self, state: &mut H)
sourceimpl<M> StrippedOrd for Value<M>
impl<M> StrippedOrd for Value<M>
fn stripped_cmp(&self, other: &Self) -> Ordering
sourceimpl<M> StrippedPartialEq<Value<M>> for Value<M>
impl<M> StrippedPartialEq<Value<M>> for Value<M>
fn stripped_eq(&self, other: &Self) -> bool
sourceimpl<M> StrippedPartialOrd<Value<M>> for Value<M>
impl<M> StrippedPartialOrd<Value<M>> for Value<M>
fn stripped_partial_cmp(&self, other: &Self) -> Option<Ordering>
sourceimpl<M, N, E> TryMapMetadataRecursively<M, N, E> for Value<M>
impl<M, N, E> TryMapMetadataRecursively<M, N, E> for Value<M>
impl<M: Eq> Eq for Value<M>
impl<M> StrippedEq for Value<M>
impl<M> StructuralEq for Value<M>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more