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

Docs say that deserialization without alloc is supported #66

Open
leighmcculloch opened this issue Feb 9, 2024 · 1 comment
Open

Docs say that deserialization without alloc is supported #66

leighmcculloch opened this issue Feb 9, 2024 · 1 comment

Comments

@leighmcculloch
Copy link

The docs appear to say:

//! - (De)serialization doesn't require memory allocations

//! - Supports deserialization of:
//! - `bool`
//! - Integers
//! - `str` (This is a zero copy operation.) (\*)

However, looking at the API, DeserializedOwned is required:

pub fn from_slice<T>(v: &[u8]) -> Result<T>
where
T: de::DeserializeOwned,

What's the right way to use the lib to do deserialisation with &str fields being a zero copy?

Thanks!

@webmaster128
Copy link
Member

The docs are outdated, thank you for pointing this out. They are from the original serde-json-core.

For JSON neither serialization nor deseralization can be done zero-copy without heavily restricting the supported contents because of escaping. E.g.

  • Serialize string \ (1 byte) gives you "\\" (2 bytes)
  • Deserialize JSON string "23\u00B0C" (9 bytes) returns 23°C (4 bytes)

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