[][src]Struct serde_json::de::StreamDeserializer

pub struct StreamDeserializer<'de, R, T> { /* fields omitted */ }

Iterator that deserializes a stream into multiple JSON values.

A stream deserializer can be created from any JSON deserializer using the Deserializer::into_iter method.

The data can consist of any JSON value. Values need to be a self-delineating value e.g. arrays, objects, or strings, or be followed by whitespace or a self-delineating value.

use serde_json::{Deserializer, Value};

fn main() {
    let data = "{\"k\": 3}1\"cool\"\"stuff\" 3{}  [0, 1, 2]";

    let stream = Deserializer::from_str(data).into_iter::<Value>();

    for value in stream {
        println!("{}", value.unwrap());
    }
}

Implementations

impl<'de, R, T> StreamDeserializer<'de, R, T> where
    R: Read<'de>,
    T: Deserialize<'de>, 
[src]

pub fn new(read: R) -> Self[src]

Create a JSON stream deserializer from one of the possible serde_json input sources.

Typically it is more convenient to use one of these methods instead:

  • Deserializer::from_str(...).into_iter()
  • Deserializer::from_bytes(...).into_iter()
  • Deserializer::from_reader(...).into_iter()

pub fn byte_offset(&self) -> usize[src]

Returns the number of bytes so far deserialized into a successful T.

If a stream deserializer returns an EOF error, new data can be joined to old_data[stream.byte_offset()..] to try again.

let data = b"[0] [1] [";

let de = serde_json::Deserializer::from_slice(data);
let mut stream = de.into_iter::<Vec<i32>>();
assert_eq!(0, stream.byte_offset());

println!("{:?}", stream.next()); // [0]
assert_eq!(3, stream.byte_offset());

println!("{:?}", stream.next()); // [1]
assert_eq!(7, stream.byte_offset());

println!("{:?}", stream.next()); // error
assert_eq!(8, stream.byte_offset());

// If err.is_eof(), can join the remaining data to new data and continue.
let remaining = &data[stream.byte_offset()..];

Note: In the future this method may be changed to return the number of bytes so far deserialized into a successful T or syntactically valid JSON skipped over due to a type error. See serde-rs/json#70 for an example illustrating this.

Trait Implementations

impl<'de, R, T> FusedIterator for StreamDeserializer<'de, R, T> where
    R: Read<'de> + Fused,
    T: Deserialize<'de>, 
[src]

impl<'de, R, T> Iterator for StreamDeserializer<'de, R, T> where
    R: Read<'de>,
    T: Deserialize<'de>, 
[src]

type Item = Result<T>

The type of the elements being iterated over.

Auto Trait Implementations

impl<'de, R, T> RefUnwindSafe for StreamDeserializer<'de, R, T> where
    R: RefUnwindSafe,
    T: RefUnwindSafe

impl<'de, R, T> Send for StreamDeserializer<'de, R, T> where
    R: Send,
    T: Send

impl<'de, R, T> Sync for StreamDeserializer<'de, R, T> where
    R: Sync,
    T: Sync

impl<'de, R, T> Unpin for StreamDeserializer<'de, R, T> where
    R: Unpin,
    T: Unpin

impl<'de, R, T> UnwindSafe for StreamDeserializer<'de, R, T> where
    R: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.