Enum json_ld_syntax::Value
source · 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
impl Value
pub fn get_fragment(&self, index: usize) -> Result<FragmentRef<'_>, usize>
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<&[Value]>
pub fn as_array_mut(&mut self) -> Option<&mut Vec<Value>>
sourcepub fn force_as_array(&self) -> &[Value]
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.
pub fn as_object(&self) -> Option<&Object>
pub fn as_object_mut(&mut self) -> Option<&mut Object>
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<Value>>
pub fn into_object(self) -> Option<Object>
pub fn traverse(&self) -> Traverse<'_>
sourcepub fn count(&self, f: impl FnMut(usize, FragmentRef<'_>) -> bool) -> usize
pub fn count(&self, f: impl FnMut(usize, FragmentRef<'_>) -> bool) -> usize
Recursively count the number of values for which f
returns true
.
Trait Implementations§
source§impl Ord for Value
impl Ord for Value
source§impl Parse for Value
impl Parse for Value
fn parse_in<C, E>( parser: &mut Parser<C, E>, context: Context ) -> Result<Meta<Value, usize>, Error<E>>
fn parse_slice(content: &[u8]) -> Result<(Self, CodeMap), Error>
fn parse_slice_with( content: &[u8], options: Options ) -> Result<(Self, CodeMap), Error>
fn parse_str(content: &str) -> Result<(Self, CodeMap), Error>
fn parse_str_with( content: &str, options: Options ) -> Result<(Self, CodeMap), Error>
fn parse_infallible_utf8<C>(chars: C) -> Result<(Self, CodeMap), Error>
fn parse_utf8_infallible_with<C>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error>
fn parse_utf8<C, E>(chars: C) -> Result<(Self, CodeMap), Error<E>>
fn parse_utf8_with<C, E>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error<E>>
fn parse_infallible<C>(chars: C) -> Result<(Self, CodeMap), Error>where
C: Iterator<Item = DecodedChar>,
fn parse_infallible_with<C>(
chars: C,
options: Options
) -> Result<(Self, CodeMap), Error>where
C: Iterator<Item = DecodedChar>,
fn parse<C, E>(chars: C) -> Result<(Self, CodeMap), Error<E>>
fn parse_with<C, E>( chars: C, options: Options ) -> Result<(Self, CodeMap), Error<E>>
source§impl PartialEq for Value
impl PartialEq for Value
source§impl PartialOrd for Value
impl PartialOrd for Value
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 PrecomputeSize for Value
impl PrecomputeSize for Value
source§impl Print for Value
impl Print for Value
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 PrintWithSize for Value
impl PrintWithSize for Value
source§impl UnorderedPartialEq for Value
impl UnorderedPartialEq for Value
fn unordered_eq(&self, other: &Value) -> bool
impl Eq for Value
impl StructuralPartialEq for Value
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> BorrowUnordered for T
impl<T> BorrowUnordered for T
fn as_unordered(&self) -> &Unordered<T>
source§impl<T> CallHasher for T
impl<T> CallHasher for T
source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
fn equivalent(&self, key: &K) -> bool
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.