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
Sourcepub fn from_serde_json(value: Value) -> Self
pub fn from_serde_json(value: Value) -> Self
Converts a serde_json::Value
into a Value
.
§Example
// First we create a `serde_json` value.
let a = serde_json::json!({
"foo": 1,
"bar": [2, 3]
});
// We convert the `serde_json` value into a `json_syntax` value.
let b = json_syntax::Value::from_serde_json(a);
// We convert it back into a `serde_json` value.
let _ = json_syntax::Value::into_serde_json(b);
Sourcepub fn into_serde_json(self) -> Value
pub fn into_serde_json(self) -> Value
Converts a Value
into a serde_json::Value
.
§Example
// First we create a `serde_json` value.
let a = serde_json::json!({
"foo": 1,
"bar": [2, 3]
});
// We convert the `serde_json` value into a `json_syntax` value.
let b = json_syntax::Value::from_serde_json(a);
// We convert it back into a `serde_json` value.
let _ = json_syntax::Value::into_serde_json(b);
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
.
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(|(_, f)| f.is_value()).count()
.
Sourcepub fn canonicalize_with(&mut self, buffer: &mut Buffer)
pub fn canonicalize_with(&mut self, buffer: &mut Buffer)
Puts this JSON value in canonical form according to RFC 8785.
The given buffer
is used to canonicalize the number values.
Sourcepub fn canonicalize(&mut self)
pub fn canonicalize(&mut self)
Puts this JSON value in canonical form according to RFC 8785.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'de> Deserializer<'de> for Value
impl<'de> Deserializer<'de> for Value
Source§type Error = DeserializeError
type Error = DeserializeError
Source§fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserializer
to figure out how to drive the visitor based
on what data type is in the input. Read moreSource§fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i8
value.Source§fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i16
value.Source§fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i32
value.Source§fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an i64
value.Source§fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Source§fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u8
value.Source§fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u16
value.Source§fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u32
value.Source§fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a u64
value.Source§fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Source§fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a f32
value.Source§fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a f64
value.Source§fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an optional value. Read moreSource§fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting an enum value with a
particular name and possible variants.Source§fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_newtype_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a newtype struct with a
particular name.Source§fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a bool
value.Source§fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a char
value.Source§fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a byte array and does not
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a unit value.Source§fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a unit struct with a
particular name.Source§fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values.Source§fn deserialize_tuple<V>(
self,
_len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_tuple<V>(
self,
_len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data.Source§fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a tuple struct with a
particular name and number of fields.Source§fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a map of key-value pairs.Source§fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting a struct with a particular
name and fields.Source§fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant.Source§fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>where
V: Visitor<'de>,
Deserialize
type needs to deserialize a value whose type
doesn’t matter because it is ignored. Read moreSource§fn is_human_readable(&self) -> bool
fn is_human_readable(&self) -> bool
Deserialize
implementations should expect to
deserialize their human-readable form. Read moreSource§impl<'de> IntoDeserializer<'de, DeserializeError> for Value
impl<'de> IntoDeserializer<'de, DeserializeError> for Value
Source§type Deserializer = Value
type Deserializer = Value
Source§fn into_deserializer(self) -> Self::Deserializer
fn into_deserializer(self) -> Self::Deserializer
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 PartialOrd for Value
impl PartialOrd for Value
Source§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.