pub struct Deserializer<'a> { /* private fields */ }Expand description
Deserializes a serializable from JSON.
Every call to deserialize reads the next value.
What may follow a value is controlled by
DeserializerConfig::set_trailing. By default only whitespace may follow
so there is only a single value. With Trailing::Newline the
deserializer reads JSON Lines:
use deser_jsonc::{
Deserializer, DeserializerConfig, Trailing,
};
let config = DeserializerConfig::builder().trailing(Trailing::Newline).build();
let mut de =
Deserializer::from_str_with_config("[1, 2]\n[3]\n", config);
assert_eq!(de.deserialize::<Vec<u32>>().unwrap(), [1, 2]);
assert_eq!(de.deserialize::<Vec<u32>>().unwrap(), [3]);
assert!(de.is_end());To deserialize a single value, use from_str and
from_slice (or the methods of the same name on
DeserializerConfig). The deserializer is also useful to
drive a custom sink.
Implementations§
Source§impl<'a> Deserializer<'a>
impl<'a> Deserializer<'a>
Sourcepub fn from_str(input: &'a str) -> Deserializer<'a>
pub fn from_str(input: &'a str) -> Deserializer<'a>
Creates a new deserializer for a string.
Sourcepub fn from_str_with_config(
input: &'a str,
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_str_with_config( input: &'a str, config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a string with the given configuration.
Sourcepub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
pub fn from_slice(input: &'a [u8]) -> Deserializer<'a>
Creates a new deserializer for a byte slice.
The input is not validated upfront. Instead strings are validated as UTF-8 when they are parsed (bytes outside of strings are only ever accepted if they are ASCII). Invalid UTF-8 is an error.
Sourcepub fn from_slice_with_config(
input: &'a [u8],
config: DeserializerConfig,
) -> Deserializer<'a>
pub fn from_slice_with_config( input: &'a [u8], config: DeserializerConfig, ) -> Deserializer<'a>
Creates a new deserializer for a byte slice with the given configuration.
See from_slice.
Sourcepub fn config(&self) -> &DeserializerConfig
pub fn config(&self) -> &DeserializerConfig
Returns the configuration.
Sourcepub fn is_end(&self) -> bool
pub fn is_end(&self) -> bool
Returns true if there are no more values.
This is the case if only whitespace is left or if a value failed and
the stream cannot be continued (see deserialize).
Sourcepub fn end(&self) -> Result<(), Error>
pub fn end(&self) -> Result<(), Error>
Fails if there is more than whitespace left.
This is useful with Trailing::Stop to check that the input was
consumed.
Sourcepub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
pub fn deserialize<T: Deserialize<'a>>(&mut self) -> Result<T, Error>
Deserializes the next value.
What may follow the value depends on
DeserializerConfig::set_trailing. Fails with
ErrorKind::EndOfFile if there are no more values.
If a value fails to deserialize (because it’s malformed or does not
match the type), the stream ends: is_end returns
true and further calls fail. With Trailing::Newline only the
rest of the line is skipped and the next call continues with the
next line.
To configure the deserialization (for instance to add layers) use
deserialize_with.
Sourcepub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
pub fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
Deserializes 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 iter<T: Deserialize<'a>>(&mut self) -> Iter<'_, 'a, T> ⓘ
pub fn iter<T: Deserialize<'a>>(&mut self) -> Iter<'_, 'a, T> ⓘ
Returns an iterator over the remaining values.
This is useful to read JSON Lines (see Trailing::Newline). The
iterator stops after the first error.
use deser_jsonc::{
Deserializer, DeserializerConfig, Trailing,
};
let config = DeserializerConfig::builder().trailing(Trailing::Newline).build();
let mut de = Deserializer::from_str_with_config("1\n2\n3\n", config);
let items = de.iter::<u32>().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(items, [1, 2, 3]);Sourcepub fn drive(
&mut self,
driver: &mut DeserializeDriver<'_, 'a>,
) -> Result<(), Error>
pub fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'a>, ) -> Result<(), Error>
Parses the next value and feeds the events into the given driver.
This is useful to deserialize into a custom Sink.
See also deserialize_with.
Strings without escape sequences are passed on borrowed from the
input (see emit_borrowed).
Errors carry the location in the input (see Error::line).
The context of the configuration is given to the driver (values that
the context of the driver has take precedence, see
DeserializeDriver::set_default_context).