Document how to deserialize from a prefix of an io::Read without blocking until EOF

Open
#522 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
documentation

Research direction

Start with the documentation for serde_json::from_reader and the Deserializer::from_reader example shown in the issue. Explain that from_reader waits for EOF and rejects trailing data, then document deserializing a prefix from a persistent stream with T::deserialize. Done means the documented behavior and example clearly distinguish both use cases.

Written by the indexing model from the issue text.

Description

docs

The behavior of serde_json::from_reader is that it expects the input stream to end after the deserialized object. If the stream contains trailing data, that is considered an error. If the stream does not end, such as in the case of a persistent socket connection, then from_reader would not return. This is the correct behavior for from_reader for example when deserializing from a File, in which we don't want to silently allow trailing garbage. It is possible instead to deserialize from a prefix of an input stream without looking for EOF by managing your own Deserializer:

let mut de = serde_json::Deserializer::from_reader(stream);
let t = T::deserialize(&mut de)?;
Dominant language
Rust
Stars
5.6k
Forks
670
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from serde-rs/json

All issues in serde-rs/json

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.