pub struct GenericDocument<'a, Backing> { /* private fields */ }
Expand description
A parsed JSON payload.
This type is a read-only view into the JSON payload.
This structure is faster to parse than Value
, but it is more work to
interact with because the entire payload is stored in a single list
internally.
The list of Node
s is sequentially ordered by the order in which they
appear in the document. This means that the first Node
tells us what the
root type of the document is.
The nodes Node::Null
, Node::Boolean
, Node::String
, and
Node::Number
all directly represent a Value
with the same name.
Node::Object
contains a length field, which lets us know how many
key-value pairs will appear after the object’s node. Object keys are
guaranteed to be Node::String
s, but object values can be any Node
variant. This means care must be taken to handle nested structures.
Node::Array
also contains a length field, which lets us know how many
values follow the array’s node. Because array values can be any type, care
must be taken to handle nested structures correctly.
DocumentIter
has multiple methods to help efficiently deal with nested
data types:
skip_next_value()
: Skips over the next value, taking care to handle nested structures properly.next_value()
: Returns aValue
from the iterator.
Implementations§
Source§impl<'a, Backing> GenericDocument<'a, Backing>where
Backing: NodeCollection<'a>,
impl<'a, Backing> GenericDocument<'a, Backing>where
Backing: NodeCollection<'a>,
Sourcepub fn from_json_bytes(source: &'a [u8]) -> Result<Self, Error>where
Backing: Default,
pub fn from_json_bytes(source: &'a [u8]) -> Result<Self, Error>where
Backing: Default,
Parses a JSON payload from source
.
This function verifies that json
is valid UTF-8 while parsing the
JSON.
Sourcepub fn from_json_bytes_with_config(
source: &'a [u8],
config: ParseConfig,
) -> Result<Self, Error>where
Backing: Default,
pub fn from_json_bytes_with_config(
source: &'a [u8],
config: ParseConfig,
) -> Result<Self, Error>where
Backing: Default,
Parses a JSON payload from source
, with the settings from config
.
This function verifies that json
is valid UTF-8 while parsing the
JSON.
Sourcepub fn from_json(source: &'a str) -> Result<Self, Error>where
Backing: Default,
pub fn from_json(source: &'a str) -> Result<Self, Error>where
Backing: Default,
Parses a JSON payload from source
.
Because the str
type guarantees that json
is valid UTF-8, no
additional unicode checks are performed on unescaped unicode sequences.
Examples found in repository?
4fn main() {
5 // Using a heapless vec, we can parse directly to the stack.
6 let doc: HeaplessDocument<'_, 3> =
7 HeaplessDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json");
8 let mut nodes = doc.into_iter();
9 assert_eq!(nodes.next(), Some(Node::Object { length: 1 }));
10 assert_eq!(nodes.next(), Some(Node::String(JsonString::from("hello"))));
11 assert_eq!(nodes.next(), Some(Node::String(JsonString::from("world"))));
12
13 // When parsing a document too large for the heapless Vec, an error will be
14 // returned instead of panicing.
15 let error = HeaplessDocument::<3>::from_json("[1, 2, 3, 4]").expect_err("shouldn't have space");
16 assert_eq!(error.kind(), &ErrorKind::PaylodTooLarge);
17}
Sourcepub fn from_json_with_config(
source: &'a str,
config: ParseConfig,
) -> Result<Self, Error>where
Backing: Default,
pub fn from_json_with_config(
source: &'a str,
config: ParseConfig,
) -> Result<Self, Error>where
Backing: Default,
Parses a JSON payload from source
, with the settings from config
.
Because the str
type guarantees that json
is valid UTF-8, no
additional unicode checks are performed on unescaped unicode sequences.
Sourcepub fn iter(&self) -> DocumentIter<'_, 'a> ⓘ
pub fn iter(&self) -> DocumentIter<'_, 'a> ⓘ
Returns an iterator over the nodes in this document.
Trait Implementations§
Source§impl<'a, Backing: Debug> Debug for GenericDocument<'a, Backing>
impl<'a, Backing: Debug> Debug for GenericDocument<'a, Backing>
Source§impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>where
Backing: NodeCollection<'a>,
Available on crate feature alloc
only.
impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>where
Backing: NodeCollection<'a>,
alloc
only.Source§fn from(doc: GenericDocument<'a, Backing>) -> Self
fn from(doc: GenericDocument<'a, Backing>) -> Self
Source§impl<'doc, 'a, Backing> IntoIterator for &'doc GenericDocument<'a, Backing>where
Backing: NodeCollection<'a>,
impl<'doc, 'a, Backing> IntoIterator for &'doc GenericDocument<'a, Backing>where
Backing: NodeCollection<'a>,
Source§impl<'a, Backing: PartialEq> PartialEq for GenericDocument<'a, Backing>
impl<'a, Backing: PartialEq> PartialEq for GenericDocument<'a, Backing>
Source§fn eq(&self, other: &GenericDocument<'a, Backing>) -> bool
fn eq(&self, other: &GenericDocument<'a, Backing>) -> bool
self
and other
values to be equal, and is used by ==
.