Skip to content

Commit 7536afd

Browse files
committed
Show how to use BufReader
1 parent e37cddd commit 7536afd

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/de.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,13 @@ where
21242124
/// The content of the IO stream is deserialized directly from the stream
21252125
/// without being buffered in memory by serde_json.
21262126
///
2127+
/// When reading from a source against which short reads are not efficient, such
2128+
/// as a [`File`], you will want to apply your own buffering because serde_json
2129+
/// will not buffer the input. See [`std::io::BufReader`].
2130+
///
2131+
/// [`File`]: https://doc.rust-lang.org/std/fs/struct.File.html
2132+
/// [`BufReader`]: https://doc.rust-lang.org/std/io/struct.BufReader.html
2133+
///
21272134
/// # Example
21282135
///
21292136
/// ```rust
@@ -2135,6 +2142,7 @@ where
21352142
///
21362143
/// use std::error::Error;
21372144
/// use std::fs::File;
2145+
/// use std::io::BufReader;
21382146
/// use std::path::Path;
21392147
///
21402148
/// #[derive(Deserialize, Debug)]
@@ -2144,11 +2152,12 @@ where
21442152
/// }
21452153
///
21462154
/// fn read_user_from_file<P: AsRef<Path>>(path: P) -> Result<User, Box<Error>> {
2147-
/// // Open the file in read-only mode.
2155+
/// // Open the file in read-only mode with buffer.
21482156
/// let file = File::open(path)?;
2157+
/// let reader = BufReader::new(file);
21492158
///
21502159
/// // Read the JSON contents of the file as an instance of `User`.
2151-
/// let u = serde_json::from_reader(file)?;
2160+
/// let u = serde_json::from_reader(reader)?;
21522161
///
21532162
/// // Return the `User`.
21542163
/// Ok(u)

0 commit comments

Comments
 (0)