pub enum JsonValue {
Null,
Boolean(bool),
Number(JsonNumberBuf),
String(JsonString),
Array(JsonArrayBuf),
Object(JsonObject),
}Expand description
JSON Value.
§Parsing
You can parse a Value by importing the JsonParse trait providing a
collection of parsing functions.
§Example
use json_syntax::{JsonValue, JsonParse, JsonCodeMap};
let (value, code_map) = JsonValue::parse_str("{ \"key\": \"value\" }").unwrap();The code_map value of type JsonCodeMap contains code-mapping information
about all the fragments of the JSON value (their location in the source
text).
§Comparison
The PartialEq and PartialOrd implementations will compare the JSON
values semantically, without regard for the object entries indexes. The only
exception is when one key has multiple values, in which cases the values are
ordered by index. It is possible to compare the lexical representation of
objects (where the entries indexes matter) by using the
JsonBorrowLexical::as_lexical method on both ends and comparing the
results.
§Example
use json_syntax::{json, lexical::JsonBorrowLexical};
let a = json!({ "a": 0, "b": 1 });
let b = json!({ "b": 1, "a": 0 });
assert_eq!(a, b); // equals, because semantically equivalent.
assert_ne!(a.as_lexical(), b.as_lexical()); // not equals.§Printing
The JsonPrint trait provide a highly configurable printing method.
§Example
use json_syntax::{JsonValue, JsonParse, JsonPrint};
let value = JsonValue::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::JsonPrintOptions::PRETTY;
options.indent = json_syntax::print::Indent::Tabs(1);
println!("{}", value.print_with(&options)); // multi line, indent with tabsVariants§
Null
null.
Boolean(bool)
Boolean true or false.
Number(JsonNumberBuf)
Number.
String(JsonString)
String.
Array(JsonArrayBuf)
Array.
Object(JsonObject)
Object.
Implementations§
Source§impl JsonValue
impl JsonValue
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
use json_syntax::JsonValue;
// 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 = JsonValue::from_serde_json(a);
// We convert it back into a `serde_json` value.
let _ = JsonValue::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
use json_syntax::JsonValue;
// 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 = JsonValue::from_serde_json(a);
// We convert it back into a `serde_json` value.
let _ = JsonValue::into_serde_json(b);Source§impl JsonValue
impl JsonValue
pub fn get_fragment(&self, index: usize) -> Result<JsonFragment<'_>, usize>
pub fn kind(&self) -> JsonKind
pub fn is_kind(&self, kind: JsonKind) -> 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<&JsonNumber>
pub fn as_number_mut(&mut self) -> Option<&mut JsonNumberBuf>
pub fn as_string(&self) -> Option<&str>
pub fn as_string_mut(&mut self) -> Option<&mut JsonString>
pub fn as_array(&self) -> Option<&[Self]>
pub fn as_array_mut(&mut self) -> Option<&mut JsonArrayBuf>
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<&JsonObject>
pub fn as_object_mut(&mut self) -> Option<&mut JsonObject>
pub fn into_boolean(self) -> Option<bool>
pub fn into_number(self) -> Option<JsonNumberBuf>
pub fn into_string(self) -> Option<JsonString>
pub fn into_array(self) -> Option<JsonArrayBuf>
pub fn into_object(self) -> Option<JsonObject>
pub fn traverse(&self) -> Traverse<'_> ⓘ
Sourcepub fn count(&self, f: impl FnMut(usize, JsonFragment<'_>) -> bool) -> usize
pub fn count(&self, f: impl FnMut(usize, JsonFragment<'_>) -> 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.
Sourcepub fn canonicalized(&self) -> Self
pub fn canonicalized(&self) -> Self
Returns the canonical form of this value according to RFC 8785.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JsonValue
impl<'de> Deserialize<'de> for JsonValue
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 JsonValue
impl<'de> Deserializer<'de> for JsonValue
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 moreimpl Eq for JsonValue
Source§impl<'n> From<&'n JsonNumber> for JsonValue
impl<'n> From<&'n JsonNumber> for JsonValue
Source§fn from(n: &'n JsonNumber) -> Self
fn from(n: &'n JsonNumber) -> Self
Source§impl From<JsonNumberBuf> for JsonValue
impl From<JsonNumberBuf> for JsonValue
Source§fn from(n: JsonNumberBuf) -> Self
fn from(n: JsonNumberBuf) -> Self
Source§impl From<JsonObject> for JsonValue
impl From<JsonObject> for JsonValue
Source§fn from(o: JsonObject) -> Self
fn from(o: JsonObject) -> Self
Source§impl From<SmallString<[u8; 16]>> for JsonValue
impl From<SmallString<[u8; 16]>> for JsonValue
Source§fn from(s: JsonString) -> Self
fn from(s: JsonString) -> Self
Source§impl From<Vec<JsonValue>> for JsonValue
impl From<Vec<JsonValue>> for JsonValue
Source§fn from(a: JsonArrayBuf) -> Self
fn from(a: JsonArrayBuf) -> Self
Source§impl<'de> IntoDeserializer<'de, DeserializeError> for JsonValue
impl<'de> IntoDeserializer<'de, DeserializeError> for JsonValue
Source§type Deserializer = JsonValue
type Deserializer = JsonValue
Source§fn into_deserializer(self) -> Self::Deserializer
fn into_deserializer(self) -> Self::Deserializer
Source§impl JsonBorrowLexical for JsonValue
impl JsonBorrowLexical for JsonValue
fn as_lexical(&self) -> &JsonLexical<Self>
impl JsonLexicalEq for JsonValue
Source§impl JsonLexicalPartialEq for JsonValue
impl JsonLexicalPartialEq for JsonValue
fn lexical_eq(&self, other: &Self) -> bool
Source§impl JsonParse for JsonValue
impl JsonParse for JsonValue
fn parse_in<C, E>( parser: &mut Parser<C, E>, context: JsonParsingContext, ) -> Result<(Self, usize), JsonParseError<E>>
fn parse_slice(content: &[u8]) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_slice_with( content: &[u8], options: Options, ) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_str(content: &str) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_str_with( content: &str, options: Options, ) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_infallible_utf8<C>( chars: C, ) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_utf8_infallible_with<C>( chars: C, options: Options, ) -> Result<(Self, JsonCodeMap), JsonParseError>
fn parse_utf8<C, E>(chars: C) -> Result<(Self, JsonCodeMap), JsonParseError<E>>
fn parse_utf8_with<C, E>( chars: C, options: Options, ) -> Result<(Self, JsonCodeMap), JsonParseError<E>>
fn parse_infallible<C>(chars: C) -> Result<(Self, JsonCodeMap), JsonParseError>where
C: Iterator<Item = DecodedChar>,
fn parse_infallible_with<C>(
chars: C,
options: Options,
) -> Result<(Self, JsonCodeMap), JsonParseError>where
C: Iterator<Item = DecodedChar>,
fn parse<C, E>(chars: C) -> Result<(Self, JsonCodeMap), JsonParseError<E>>
fn parse_with<C, E>( chars: C, options: Options, ) -> Result<(Self, JsonCodeMap), JsonParseError<E>>
Source§impl Ord for JsonValue
impl Ord for JsonValue
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for JsonValue
impl PartialOrd for JsonValue
impl StructuralPartialEq for JsonValue
Auto Trait Implementations§
impl Freeze for JsonValue
impl RefUnwindSafe for JsonValue
impl Send for JsonValue
impl Sync for JsonValue
impl Unpin for JsonValue
impl UnsafeUnpin for JsonValue
impl UnwindSafe for JsonValue
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.Source§impl<T> JsonPrint for Twhere
T: JsonVisit,
impl<T> JsonPrint for Twhere
T: JsonVisit,
fn fmt_with( &self, f: &mut Formatter<'_>, options: &JsonPrintOptions, indent: usize, ) -> Result<(), Error>
Source§fn pretty_print(&self) -> Printed<'_, 'static, Self>
fn pretty_print(&self) -> Printed<'_, 'static, Self>
Options::pretty options.Source§fn compact_print(&self) -> Printed<'_, 'static, Self>
fn compact_print(&self) -> Printed<'_, 'static, Self>
Options::compact options.Source§fn inline_print(&self) -> Printed<'_, 'static, Self>
fn inline_print(&self) -> Printed<'_, 'static, Self>
Options::inline options.