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 Nodes 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::Strings, 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:

Implementations§

source§

impl<'a, Backing> GenericDocument<'a, Backing>
where Backing: NodeCollection<'a>,

source

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.

source

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.

source

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?
examples/heapless.rs (line 7)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    // Using a heapless vec, we can parse directly to the stack.
    let doc: HeaplessDocument<'_, 3> =
        HeaplessDocument::from_json(r#"{"hello": "world"}"#).expect("invalid json");
    let mut nodes = doc.into_iter();
    assert_eq!(nodes.next(), Some(Node::Object { length: 1 }));
    assert_eq!(nodes.next(), Some(Node::String(JsonString::from("hello"))));
    assert_eq!(nodes.next(), Some(Node::String(JsonString::from("world"))));

    // When parsing a document too large for the heapless Vec, an error will be
    // returned instead of panicing.
    let error = HeaplessDocument::<3>::from_json("[1, 2, 3, 4]").expect_err("shouldn't have space");
    assert_eq!(error.kind(), &ErrorKind::PaylodTooLarge);
}
source

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.

source

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>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, Backing> From<GenericDocument<'a, Backing>> for Value<'a>
where Backing: NodeCollection<'a>,

Available on crate feature alloc only.
source§

fn from(doc: GenericDocument<'a, Backing>) -> Self

Converts to this type from the input type.
source§

impl<'doc, 'a, Backing> IntoIterator for &'doc GenericDocument<'a, Backing>
where Backing: NodeCollection<'a>,

§

type IntoIter = DocumentIter<'doc, 'a>

Which kind of iterator are we turning this into?
§

type Item = Node<'a>

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, Backing: PartialEq> PartialEq for GenericDocument<'a, Backing>

source§

fn eq(&self, other: &GenericDocument<'a, Backing>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, Backing: Eq> Eq for GenericDocument<'a, Backing>

source§

impl<'a, Backing> StructuralEq for GenericDocument<'a, Backing>

source§

impl<'a, Backing> StructuralPartialEq for GenericDocument<'a, Backing>

Auto Trait Implementations§

§

impl<'a, Backing> RefUnwindSafe for GenericDocument<'a, Backing>
where Backing: RefUnwindSafe,

§

impl<'a, Backing> Send for GenericDocument<'a, Backing>
where Backing: Send,

§

impl<'a, Backing> Sync for GenericDocument<'a, Backing>
where Backing: Sync,

§

impl<'a, Backing> Unpin for GenericDocument<'a, Backing>
where Backing: Unpin,

§

impl<'a, Backing> UnwindSafe for GenericDocument<'a, Backing>
where Backing: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.