Skip to content

Example Proposition for str deserialization #61

Closed
@XdoctorwhoZ

Description

@XdoctorwhoZ

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
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions