Enum json_syntax::Value
source · pub enum Value {
Null,
Boolean(bool),
Number(NumberBuf),
String(String),
Array(Array),
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)
Number.
String(String)
String.
Array(Array)
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>
pub fn as_string(&self) -> Option<&str>
pub fn as_string_mut(&mut self) -> Option<&mut String>
pub fn as_array(&self) -> Option<&[Self]>
pub fn as_array_mut(&mut self) -> Option<&mut Array>
sourcepub fn force_as_array(&self) -> &[Self]
pub fn force_as_array(&self) -> &[Self]
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>
pub fn into_string(self) -> Option<String>
pub fn into_array(self) -> Option<Array>
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<Self, 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
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.