Enum json_ld_syntax::Value
source · 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>
impl<M> Value<M>
sourcepub fn from_serde_json(
value: Value,
f: impl Clone + Fn(SerdeJsonFragment<'_>) -> M
) -> Meta<Value<M>, M>
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).
sourcepub fn into_serde_json(_: Meta<Value<M>, M>) -> Value
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>
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<SmallVec<[u8; 16]>>>
pub fn as_string(&self) -> Option<&str>
pub fn as_string_mut(&mut self) -> Option<&mut SmallString<[u8; 16]>>
pub fn as_array(&self) -> Option<&[Meta<Value<M>, M>]>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Meta<Value<M>, M>, Global>>
sourcepub fn force_as_array(
value: &Meta<Value<M>, M>
) -> Meta<&[Meta<Value<M>, M>], &M>
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.
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<SmallVec<[u8; 16]>>>
pub fn into_string(self) -> Option<SmallString<[u8; 16]>>
pub fn into_array(self) -> Option<Vec<Meta<Value<M>, M>, Global>>
pub fn into_object(self) -> Option<Object<M>>
pub fn traverse(&self) -> TraverseStripped<'_, 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§
source§impl<M, N> MapMetadataRecursively<M, N> for Value<M>
impl<M, N> MapMetadataRecursively<M, N> for Value<M>
source§impl<M> Ord for Value<M>where
M: Ord,
impl<M> Ord for Value<M>where
M: Ord,
source§impl<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<Value<M>, 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, Infallible>, 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, Infallible>, M>>where
F: FnMut(Span) -> M,
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,
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,
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, Infallible>, 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, Infallible>, 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,
source§impl<M> PartialEq<Value<M>> for Value<M>where
M: PartialEq<M>,
impl<M> PartialEq<Value<M>> for Value<M>where
M: PartialEq<M>,
source§impl<M> PartialOrd<Value<M>> for Value<M>where
M: PartialOrd<M>,
impl<M> PartialOrd<Value<M>> for Value<M>where
M: PartialOrd<M>,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<M> PrecomputeSize for Value<M>
impl<M> PrecomputeSize for Value<M>
source§impl<M> Print for Value<M>
impl<M> Print for Value<M>
fn fmt_with(
&self,
f: &mut Formatter<'_>,
options: &Options,
indent: usize
) -> Result<(), Error>
source§fn pretty_print(&self) -> Printed<'_, Self>
fn pretty_print(&self) -> Printed<'_, Self>
Options::pretty
options.source§fn compact_print(&self) -> Printed<'_, Self>
fn compact_print(&self) -> Printed<'_, Self>
Options::compact
options.source§fn inline_print(&self) -> Printed<'_, Self>
fn inline_print(&self) -> Printed<'_, Self>
Options::inline
options.source§fn print_with(&self, options: Options) -> Printed<'_, Self>
fn print_with(&self, options: Options) -> Printed<'_, Self>
source§impl<M> PrintWithSize for Value<M>
impl<M> PrintWithSize for Value<M>
source§impl<M> StrippedHash for Value<M>
impl<M> StrippedHash for Value<M>
fn stripped_hash<H>(&self, state: &mut H)where
H: Hasher,
source§impl<M> StrippedOrd for Value<M>
impl<M> StrippedOrd for Value<M>
fn stripped_cmp(&self, other: &Value<M>) -> Ordering
source§impl<M, __M> StrippedPartialEq<Value<__M>> for Value<M>
impl<M, __M> StrippedPartialEq<Value<__M>> for Value<M>
fn stripped_eq(&self, other: &Value<__M>) -> bool
source§impl<M, __M> StrippedPartialOrd<Value<__M>> for Value<M>
impl<M, __M> StrippedPartialOrd<Value<__M>> for Value<M>
source§impl<M, N, E> TryMapMetadataRecursively<M, N, E> for Value<M>
impl<M, N, E> TryMapMetadataRecursively<M, N, E> for Value<M>
impl<M> Eq for Value<M>where
M: Eq,
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§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
fn equivalent(&self, key: &K) -> bool
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.