pub struct ArrayReader<'r, 'de> { /* private fields */ }Expand description
Reads the items of an array one by one; from Reader::read_array.
Errors in an item get its index in their path.
Implementations§
Source§impl<'de> ArrayReader<'_, 'de>
impl<'de> ArrayReader<'_, 'de>
Sourcepub fn next_item<T: FromJson<'de>>(&mut self) -> Result<Option<T>, DecodeError>
pub fn next_item<T: FromJson<'de>>(&mut self) -> Result<Option<T>, DecodeError>
Reads the next item as a T, or None after the last one, when the
array’s ] has been read.
§Errors
The DecodeError of T, with the item’s index in its path.
§Examples
let mut r = jsonx_lib::Reader::new(r#"[1, "x"]"#);
let mut items = r.read_array()?;
assert_eq!(items.next_item::<u8>()?, Some(1));
assert_eq!(items.next_item::<u8>().unwrap_err().pointer(), "/1");Sourcepub fn next_with<T>(
&mut self,
read: impl FnOnce(&mut Reader<'de>) -> Result<T, DecodeError>,
) -> Result<Option<T>, DecodeError>
pub fn next_with<T>( &mut self, read: impl FnOnce(&mut Reader<'de>) -> Result<T, DecodeError>, ) -> Result<Option<T>, DecodeError>
Reads the next item with read, or returns None after the last.
§Errors
Whatever read returns, with the item’s index in its path.
§Examples
let mut r = jsonx_lib::Reader::new("[[1, 2], [3]]");
let mut rows = r.read_array()?;
let mut lengths = Vec::new();
while let Some(n) = rows.next_with(|r| Ok(r.decode::<Vec<u8>>()?.len()))? {
lengths.push(n);
}
assert_eq!(lengths, [2, 1]);Sourcepub fn skip(&mut self) -> Result<bool, DecodeError>
pub fn skip(&mut self) -> Result<bool, DecodeError>
Sourcepub fn end(self) -> Result<(), DecodeError>
pub fn end(self) -> Result<(), DecodeError>
Checks that no item is left: for a fixed-length array or a tuple, after its items were read.
§Errors
DecodeErrorKind::WrongLength at the [ if items remain (all of
them are skipped and counted), or an error of the reader.
§Examples
use jsonx_lib::DecodeErrorKind;
let mut r = jsonx_lib::Reader::new("[1, 2, 3]");
let mut items = r.read_array()?;
items.next_item::<u8>()?;
let err = items.end().unwrap_err();
assert_eq!(err.kind(), &DecodeErrorKind::WrongLength { expected: 1, found: 3 });Auto Trait Implementations§
impl<'r, 'de> !UnwindSafe for ArrayReader<'r, 'de>
impl<'r, 'de> Forget for ArrayReader<'r, 'de>
impl<'r, 'de> Freeze for ArrayReader<'r, 'de>
impl<'r, 'de> Move for ArrayReader<'r, 'de>
impl<'r, 'de> RefUnwindSafe for ArrayReader<'r, 'de>
impl<'r, 'de> Send for ArrayReader<'r, 'de>
impl<'r, 'de> Sync for ArrayReader<'r, 'de>
impl<'r, 'de> Unpin for ArrayReader<'r, 'de>
impl<'r, 'de> UnsafeUnpin for ArrayReader<'r, 'de>
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
Mutably borrows from an owned value. Read more