pub struct Reader<R, D: StreamDeserializer> { /* private fields */ }io only.Expand description
Reads values from a Read.
The values are split and deserialized with a StreamDeserializer
(for instance deser_json::StreamDeserializer, which the configuration
of the format creates with config.reader(input)). The reader buffers
the input so it does not need to be buffered.
Readers implement Deserializer: every call to
drive reads the next value. Values read this
way cannot borrow from the reader, use
read_borrowed for that.
Implementations§
Source§impl<R: Read, D: StreamDeserializer> Reader<R, D>
impl<R: Read, D: StreamDeserializer> Reader<R, D>
Sourcepub fn new(reader: R, deserializer: D) -> Reader<R, D>
pub fn new(reader: R, deserializer: D) -> Reader<R, D>
Creates a reader.
To continue a stream whose context is known (for instance the names of the columns of a CSV file), create the stream deserializer with that context.
Sourcepub fn set_context(&mut self, context: Context)
pub fn set_context(&mut self, context: Context)
Sets the context the values are deserialized in.
This replaces the context of the stream deserializer (see
StreamDeserializer::context), which is the one of the
configuration it was created with. The values of the context are
the defaults of the extension values of the state (see
Context). A context set by the callback of
read_with takes precedence.
Sourcepub fn read<T: DeserializeOwned>(&mut self) -> Result<Option<T>, Error>
pub fn read<T: DeserializeOwned>(&mut self) -> Result<Option<T>, Error>
Reads the next value.
Returns None if there are no more values. If the format supports
it (see StreamDeserializer::supports_partial), the value is
deserialized while the input is read which means that only
incomplete tokens are buffered. Otherwise the complete value is
buffered first. Whether reading can continue after an error depends
on the format (for instance with JSON Lines it continues with the
next line).
Sourcepub fn read_with<T, F>(&mut self, setup: F) -> Result<Option<T>, Error>
pub fn read_with<T, F>(&mut self, setup: F) -> Result<Option<T>, Error>
Reads the next value with a configured driver.
The callback is invoked with the driver before the value is
deserialized, for instance to add Layers.
Sourcepub fn read_borrowed<'a, T: Deserialize<'a>>(
&'a mut self,
) -> Result<Option<T>, Error>
pub fn read_borrowed<'a, T: Deserialize<'a>>( &'a mut self, ) -> Result<Option<T>, Error>
Reads the next value which can borrow from the reader’s buffer.
The complete value is buffered first.
use deser::io::Reader;
// `Lines` is the stream deserializer of a format with a string per
// line
let mut reader = Reader::new(&b"hello\nworld\n"[..], Lines);
let value: &str = reader.read_borrowed().unwrap().unwrap();
assert_eq!(value, "hello");Sourcepub fn read_next<T, E>(&mut self) -> Result<Option<Part<E, T>>, Error>where
T: DeserializeOwned + 'static,
E: Send + 'static,
pub fn read_next<T, E>(&mut self) -> Result<Option<Part<E, T>>, Error>where
T: DeserializeOwned + 'static,
E: Send + 'static,
Reads the next element of the Streamed sequence of a value or
the value.
T is the type of the value and E the type of the elements of a
Streamed<E> sequence within it. The elements are
handed out as they are read (Part::Element), the value once it’s
complete (Part::Done). The next call continues with the next
value. Returns None if there are no more values. See Streamed
for an example.
Until the value is complete, the reader can only be used to read the value with the same types.
Sourcepub fn is_end(&mut self) -> Result<bool, Error>
pub fn is_end(&mut self) -> Result<bool, Error>
Returns true if there are no more values.
This reads until the start of the next value or the end of the
stream. If the format cannot find the start of a value on its own
(see StreamDeserializer::peek), the next value is buffered
completely. This is useful to read values with the reader’s
Deserializer implementation:
use deser::de::Deserializer;
use deser::io::Reader;
// `Lines` is the stream deserializer of a format with a string per
// line
let mut reader = Reader::new(&b"hello\nworld\n"[..], Lines);
let mut values = Vec::new();
while !reader.is_end().unwrap() {
values.push(reader.deserialize::<String>().unwrap());
}
assert_eq!(values, ["hello", "world"]);Sourcepub fn end(&mut self) -> Result<(), Error>
pub fn end(&mut self) -> Result<(), Error>
Checks that there are no more values.
Fails if another value follows (or if the data that follows is not valid).
Sourcepub fn iter<T: DeserializeOwned>(&mut self) -> Iter<'_, R, D, T> ⓘ
pub fn iter<T: DeserializeOwned>(&mut self) -> Iter<'_, R, D, T> ⓘ
Returns an iterator over the remaining values.
The iterator stops after the first error.
Sourcepub fn deserializer(&self) -> &D
pub fn deserializer(&self) -> &D
Returns the stream deserializer.
This gives access to what the stream established so far, for instance the names of the columns of a CSV file.
Sourcepub fn get_mut(&mut self) -> &mut R
pub fn get_mut(&mut self) -> &mut R
Returns a mutable reference to the underlying reader.
Reading from it directly is likely to corrupt the stream as the reader buffers data.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Returns the underlying reader.
Data that was read into the buffer but not deserialized yet is lost.
Sourcepub fn into_parts(self) -> (R, D)
pub fn into_parts(self) -> (R, D)
Returns the underlying reader and the stream deserializer.
Data that was read into the buffer but not deserialized yet is lost.
Trait Implementations§
Source§impl<'de, R: Read, D: StreamDeserializer> Deserializer<'de> for Reader<R, D>
Reads the next value.
impl<'de, R: Read, D: StreamDeserializer> Deserializer<'de> for Reader<R, D>
Reads the next value.
Values cannot borrow from the reader: borrowed data is passed on like
data that is only valid for the call (see
DeserializeDriver::transient). If there are no more values this
fails (see Reader::is_end).