Skip to content

Improve documentation for buffering around functions taking a reader #1237

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

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,17 @@ where
/// Create a JSON deserializer from one of the possible serde_json input
/// sources.
///
/// When reading from a source against which short reads are not efficient, such
/// as a [`File`], you will want to apply your own buffering because serde_json
/// will not buffer the input. See [`std::io::BufReader`].
///
/// Typically it is more convenient to use one of these methods instead:
///
/// - Deserializer::from_str
/// - Deserializer::from_slice
/// - Deserializer::from_reader
///
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
pub fn new(read: R) -> Self {
Deserializer {
read,
Expand Down Expand Up @@ -2568,6 +2574,7 @@ where
///
/// use std::error::Error;
/// use std::net::{TcpListener, TcpStream};
/// use std::io::BufReader;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
Expand All @@ -2576,7 +2583,8 @@ where
/// }
///
/// fn read_user_from_stream(tcp_stream: TcpStream) -> Result<User, Box<dyn Error>> {
/// let mut de = serde_json::Deserializer::from_reader(tcp_stream);
/// let buf_tcp_stream = BufReader::new(tcp_stream);
/// let mut de = serde_json::Deserializer::from_reader(buf_tcp_stream);
/// let u = User::deserialize(&mut de)?;
///
/// Ok(u)
Expand Down
6 changes: 6 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ where
R: io::Read,
{
/// Create a JSON input source to read from a std::io input stream.
///
/// When reading from a source against which short reads are not efficient, such
/// as a [`File`], you will want to apply your own buffering because serde_json
/// will not buffer the input. See [`std::io::BufReader`].
///
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
pub fn new(reader: R) -> Self {
IoRead {
iter: LineColIterator::new(reader.bytes()),
Expand Down
Loading