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
sourcepub fn is_empty_array_or_object(&self) -> bool
pub fn is_empty_array_or_object(&self) -> bool
Checks if the value is either an empty array or an empty object.
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>>
sourcepub fn force_as_array(value: &Meta<Self, M>) -> Meta<&[Meta<Self, M>], &M>
pub fn force_as_array(value: &Meta<Self, M>) -> Meta<&[Meta<Self, 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.
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
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
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<M, E>, 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<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<M>, M>>where
F: FnMut(Span) -> M,
fn parse_infallible_utf8<C, F>(
chars: C,
metadata_builder: F
) -> Result<Meta<Self, M>, Meta<Error<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<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<M, E>, 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<M, E>, 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<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<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<M, E>, 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<M, E>, 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<M, E>, 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>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresourceimpl<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>
Options::pretty
options.sourcefn compact_print(&self) -> Printed<'_, Self>
fn compact_print(&self) -> Printed<'_, Self>
Options::compact
options.sourcefn inline_print(&self) -> Printed<'_, Self>
fn inline_print(&self) -> Printed<'_, Self>
Options::inline
options.