Skip to content

Example Proposition for str deserialization #61

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
XdoctorwhoZ opened this issue Mar 20, 2022 · 2 comments
Closed

Example Proposition for str deserialization #61

XdoctorwhoZ opened this issue Mar 20, 2022 · 2 comments

Comments

@XdoctorwhoZ
Copy link

Hello Guys,
This is a great crates !

But as a rust newcomer I was a bit lost face to:

Supports deserialization of:
    [...]
    str (This is a zero copy operation.) (*)
    [...]

Examples of std serde use dynamic allocation and don't speak about this use case.

I put here a proposition of example that would have helped me. If you put this in your documentation I'm sure it will help other newcomers.

// Import
use serde::{Deserialize};
use serde_json_core;

// The field is a ref that will point directly in the parsed buffer
#[derive(Deserialize)]
struct MyStruct<'a> {
    the_str: &'a str,
}

// Deserialize buffer
match serde_json_core::de::from_slice::<MyStruct>( b"{\"the_str\": \"jsoncore\"}" ) {
    Err(_e) => {
        // Err
    }
    Ok(deser) => {
        // result in deser.0.the_str
    }
}
@ryan-summers
Copy link
Member

I see no issue with adding these kind of examples to the main repository docs, and rust-doc would gladly accept examples. I'd be happy to review a PR for the addition :)

@ryan-summers
Copy link
Member

I've added a basic doc to the main repo docs in #91 that should cover this case as well as showing how to use the library. Thanks for bringing this up!

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

No branches or pull requests

2 participants