pub struct Cursor<'a> { /* private fields */ }Expand description
A strict JSON pull cursor over text.
Make one from text with new, or from bytes with from_bytes,
which checks their UTF-8 once, in one pass. Every key, string and number is then a slice of the
text, with no further check.
Read the next value with value, or with the call for its kind:
string, number, bool, null, an
object’s keys with first_key and next_key, an array’s
items with first_item and next_item, or any value with
skip. peek says which kind is next. finish
checks that nothing follows.
Every byte read is checked against RFC 8259, skipped values included: a call returns an
Error at the first byte that is not JSON. After an error, the cursor’s position is
unspecified, so start again from the input.
Implementations§
Source§impl<'a> Cursor<'a>
impl<'a> Cursor<'a>
Sourcepub fn from_bytes(bytes: &'a [u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &'a [u8]) -> Result<Self, Error>
A cursor at the start of bytes, once they are checked as UTF-8, in one pass. Err with
ErrorKind::Utf8 at the first byte that is not.
Sourcepub fn with_depth_limit(self, limit: usize) -> Self
pub fn with_depth_limit(self, limit: usize) -> Self
How deep skip goes into arrays and objects before it returns
ErrorKind::TooDeep. The skip keeps its own stack, so no limit can overflow the call
stack. DEFAULT_DEPTH_LIMIT when not set.
Sourcepub fn peek(&mut self) -> Result<Kind, Error>
pub fn peek(&mut self) -> Result<Kind, Error>
The kind of the next value, after any whitespace. Nothing is consumed.
Sourcepub fn value<'s>(&mut self, buf: &'s mut String) -> Result<Value<'s>, Error>where
'a: 's,
pub fn value<'s>(&mut self, buf: &'s mut String) -> Result<Value<'s>, Error>where
'a: 's,
The next value, after any whitespace, in one look at its first byte: a string, a number, a
boolean or null, read; or the start of an array or an object, not consumed, to be read with
first_item or first_key. A string is borrowed
or decoded as in string.
Sourcepub fn first_key<'s>(
&mut self,
buf: &'s mut String,
) -> Result<Option<&'s str>, Error>where
'a: 's,
pub fn first_key<'s>(
&mut self,
buf: &'s mut String,
) -> Result<Option<&'s str>, Error>where
'a: 's,
Enters the object at the cursor and reads its first key and colon, or None when the object
is empty. Read the key’s value next. The key is borrowed from the input when it has no
escapes, else decoded into buf, which it then borrows.
Sourcepub fn next_key<'s>(
&mut self,
buf: &'s mut String,
) -> Result<Option<&'s str>, Error>where
'a: 's,
pub fn next_key<'s>(
&mut self,
buf: &'s mut String,
) -> Result<Option<&'s str>, Error>where
'a: 's,
After a value in an object: the next key and colon, or None at the object’s end, which is
consumed. The key is borrowed as in first_key.
Sourcepub fn first_item(&mut self) -> Result<bool, Error>
pub fn first_item(&mut self) -> Result<bool, Error>
Enters the array at the cursor: true when it has a first item, which is read next, and
false when it is empty.
Sourcepub fn next_item(&mut self) -> Result<bool, Error>
pub fn next_item(&mut self) -> Result<bool, Error>
After an item in an array: true when another follows, which is read next, and false at
the array’s end, which is consumed.
Sourcepub fn string<'s>(&mut self, buf: &'s mut String) -> Result<&'s str, Error>where
'a: 's,
pub fn string<'s>(&mut self, buf: &'s mut String) -> Result<&'s str, Error>where
'a: 's,
The string at the cursor: borrowed from the input when it has no escapes, else decoded into
buf, which it then borrows.
Sourcepub fn number(&mut self) -> Result<Number<'a>, Error>
pub fn number(&mut self) -> Result<Number<'a>, Error>
The number at the cursor: its exact text, and its value as an i64 or an f64. Its grammar
is checked; its size is not limited, since the text keeps every digit.