Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support streaming iteration over sequences #760

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 109 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}
}

/// Turn a JSON deserializer into a sequence.
pub fn as_seq(&mut self) -> Result<SeqAccess<R>> {
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};

match peek {
b'[' => {
self.eat_char();

Ok(SeqAccess::new(self))
}
_ => {
let err = self.peek_invalid_type(&"Expected array");
Err(self.fix_position(err))
}
}
}

/// Parse arbitrarily deep JSON structures without any consideration for
/// overflowing the stack.
///
Expand Down Expand Up @@ -1881,7 +1903,30 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
}
}

struct SeqAccess<'a, R: 'a> {
/// A sequence that can be iterated over.
///
/// This can be created from any JSON deserializer using the `Deserializer::as_seq` method.
///
/// The data must be an array. Elements can be any JSON value.
///
/// ```
/// use serde::de::SeqAccess;
/// use serde_json::{Deserializer, Value};
///
/// fn main() {
/// let data = r#"[1, "2", {"three": [4]}]"#;
///
/// let mut deserializer = Deserializer::from_str(data);
/// let mut seq = deserializer.as_seq().unwrap();
///
/// while let Some(value) = seq.next_element::<Value>().unwrap() {
/// println!("{}", value);
/// }
///
/// seq.end();
/// }
/// ```
pub struct SeqAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
}
Expand All @@ -1892,6 +1937,22 @@ impl<'a, R: 'a> SeqAccess<'a, R> {
}
}

impl<'de, 'a, R: Read<'de> + 'a> SeqAccess<'a, R> {
/// Ensures that the end of the sequence has been reached. This must be called even if
/// `next_element` returns `Ok(None)`.
pub fn end(self) -> Result<()> {
self.de.end_seq()
}

/// Turn a JSON sequence into an iterator over values of type T.
pub fn into_iter<T: de::Deserialize<'de>>(self) -> SeqIntoIter<'a, R, T> {
SeqIntoIter {
seq: Some(self),
_phantom: PhantomData,
}
}
}

impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
type Error = Error;

Expand Down Expand Up @@ -1928,6 +1989,53 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
}
}

/// Iterator that deserializes a sequence into multiple JSON values.
///
/// A sequence iterator can be created from any sequence using the `Sequence::into_iter` method.
///
/// The elements can consist of any JSON value.
///
/// ```
/// use serde_json::{Deserializer, Value};
///
/// fn main() {
/// let data = r#"[5, "6", {"seven": [8]}]"#;
///
/// let mut deserializer = Deserializer::from_str(data);
/// let seq = deserializer.as_seq().unwrap().into_iter::<Value>();
///
/// for value in seq {
/// println!("{}", value.unwrap());
/// }
/// }
/// ```
pub struct SeqIntoIter<'a, R: 'a, T> {
seq: Option<SeqAccess<'a, R>>,
_phantom: PhantomData<fn() -> T>,
}

impl<'de, 'a, R: Read<'de> + 'a, T: de::Deserialize<'de>> Iterator for SeqIntoIter<'a, R, T> {
type Item = Result<T>;

fn next(&mut self) -> Option<Self::Item> {
self.seq
.take()
.and_then(|mut seq| match de::SeqAccess::next_element(&mut seq) {
Ok(Some(elem)) => {
self.seq = Some(seq);
Some(Ok(elem))
}
Ok(None) => match seq.end() {
Ok(()) => None,
Err(err) => Some(Err(err)),
},
Err(err) => Some(Err(err)),
})
}
}

impl<'de, 'a, R: Read<'de> + 'a, T: de::Deserialize<'de>> FusedIterator for SeqIntoIter<'a, R, T> {}

struct MapAccess<'a, R: 'a> {
de: &'a mut Deserializer<R>,
first: bool,
Expand Down