Function from_reader
fn from_reader<R, T>(rdr: R) -> crate::error::Result<T>
where
R: crate::io::Read,
T: de::DeserializeOwned
Deserialize an instance of type T from an I/O stream of JSON.
The content of the I/O stream is deserialized directly from the stream without being buffered in memory by serde_json.
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.
It is expected that the input stream ends after the deserialized object.
If the stream does not end, such as in the case of a persistent socket connection,
this function will not return. It is possible instead to deserialize from a prefix of an input
stream without looking for EOF by managing your own Deserializer.
Note that counter to intuition, this function is usually slower than
reading a file completely into memory and then applying from_str
or from_slice on it. See issue #160.
Example
Reading the contents of a file.
use Deserialize;
use Error;
use File;
use BufReader;
use Path;
#
Reading from a persistent socket connection.
use Deserialize;
use Error;
use BufReader;
use ;
#
Errors
This conversion can fail if the structure of the input does not match the
structure expected by T, for example if T is a struct type but the input
contains something other than a JSON map. It can also fail if the structure
is correct but T's implementation of Deserialize decides that something
is wrong with the data, for example required struct fields are missing from
the JSON map or some number is too big to fit in the expected primitive
type.