Skip to content

Add streaming deserializer mode #665

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

Closed
wants to merge 1 commit into from
Closed

Conversation

tinou98
Copy link
Contributor

@tinou98 tinou98 commented May 9, 2020

This allows to efficiently de-serialize array ([ val, val ]), map, ({ key: val, key: val }), and list (val val) of any type.

It follows some idea discussed in #526 (comment)

Full manual example:

    let deserializer = serde_json::Deserializer::from_str(
        r#"[1, 5, {"a": 1, "b": 2, "c": [1, 2], "d": 3}, 0]
        {
            "a"
            :
            { "b" : 1 },
            "d": 5
        }
        "#,
    );

    let stream = Stream::new(deserializer);

    let mut stream = stream.enter_array().unwrap();
    stream.next_value::<usize>().unwrap(); // 1
    stream.next_value::<usize>().unwrap(); // 5

    let mut stream = stream.enter_map().unwrap();
    stream.next_value::<usize>().unwrap(); // a, 1
    stream.next_value::<usize>().unwrap(); // b, 2
    let (key, mut stream) = stream.enter_array().unwrap(); // key == c
    stream.next_value::<usize>().unwrap(); // 1
    stream.next_value::<usize>().unwrap(); // 2

    let mut stream = stream.end_array().unwrap();
    stream.next_value::<usize>().unwrap(); // d, 3
    let mut stream = stream.end_map().unwrap();

    stream.next_value::<usize>().unwrap(); // 0
    let stream = stream.end_array().unwrap();

    let stream = stream.enter_map().unwrap();

    let (key, mut stream) = stream.enter_map().unwrap(); // key == a
    stream.next_value::<usize>().unwrap(); // b, 1
    let mut stream = stream.end_map().unwrap();

    stream.next_value::<usize>().unwrap(); // d, 5
    let stream = stream.end_map().unwrap();

    stream.end().unwrap();

Iterator mode :

    let deserializer = serde_json::Deserializer::from_str(r#"[1, 2, 5, 6, 0]5"#);

    let stream = Stream::new(deserializer);

    let mut stream = stream.enter_array().unwrap();
    let iter = stream.iter::<usize>();
    for res in iter {
        let el = res.unwrap();
        // ...
    }

    let mut stream = stream.end_array().unwrap();
    stream.next_value::<usize>().unwrap(); // 5
    stream.end().unwrap();

This allow to efficiently de-serialize big structore of any type
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks promising.

I am closing the PR because like in #526 I'd like for this to be pursued and released in an external crate at first. I would only consider accepting an implementation in serde_json once there is sufficient usage experience with that.

@dtolnay dtolnay closed this May 9, 2020
@tinou98
Copy link
Contributor Author

tinou98 commented May 20, 2020

I tried to move this to separate crate, but I'm facing a big problem : I cannot use serde_json::Deserializer. If I want to use it, I need to give it the Read object and there is no way to get it back.

Would it be possible to add Read implementation to mutable reference to Read ?

impl<'de, R: Read<'de>> Read<'de> for &mut R;

Because of the Sealed trait, we can't implement Read in other create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants