pub struct Deserializer<R> { /* private fields */ }
Expand description
A structure that deserializes JSON into Rust values.
Implementations§
Source§impl<'de, R: Reader<'de>> Deserializer<R>
impl<'de, R: Reader<'de>> Deserializer<R>
Sourcepub fn use_rawnumber(self) -> Self
pub fn use_rawnumber(self) -> Self
Parse all number as crate::RawNumber
.
§Example
use sonic_rs::{Deserializer, Value};
let json = r#"{"a":1.2345678901234567890123}"#;
let mut de = Deserializer::from_str(json).use_rawnumber();
let value: Value = de.deserialize().unwrap();
let out = sonic_rs::to_string(&value).unwrap();
assert_eq!(json, out);
Sourcepub fn use_raw(self) -> Self
pub fn use_raw(self) -> Self
Parse all number as RawNumber
and record the raw text for all JSON string.
This will make sure the the serialize of number
or string
will retain from the origin
JSON text.
§Example
use sonic_rs::{Deserializer, Value};
let data = [
r#"{"a":1.2345678901234567890123}"#,
r#"{"a":1,"b":"\\u0001"}"#,
r#"{"a":1,"b":"💎"}"#,
r#"{"\\u0001":1,"b":"\\u0001"}"#,
];
for json in data {
let mut de = Deserializer::from_str(json).use_raw();
let value: Value = de.deserialize().unwrap();
let out = sonic_rs::to_string(&value).unwrap();
assert_eq!(json, out);
}
Sourcepub fn utf8_lossy(self) -> Self
pub fn utf8_lossy(self) -> Self
Allow to parse JSON with invalid UTF-8 and UTF-16 characters. Will replace them with
\uFFFD
(displayed as �).
§Example
use sonic_rs::{Deserializer, Value};
let data = [
&[b'\"', 0xff, b'\"'][..], // invalid UTF8 char in string
br#"{"a":"\ud800","b":"\udc00"}"#, // invalid UTF16 surrogate pair
];
let expect = [r#""�""#, r#"{"a":"�","b":"�"}"#];
let mut exp = expect.iter();
for json in data {
let mut de = Deserializer::from_slice(json).utf8_lossy();
let value: Value = de.deserialize().unwrap();
let out = sonic_rs::to_string(&value).unwrap();
assert_eq!(&out, exp.next().unwrap());
}
Sourcepub fn deserialize<T>(&mut self) -> Result<T>where
T: Deserialize<'de>,
pub fn deserialize<T>(&mut self) -> Result<T>where
T: Deserialize<'de>,
Deserialize a JSON stream to a Rust data structure.
It can be used repeatedly and we do not check trailing chars after deserilalized.
§Example
use sonic_rs::Deserializer;
let multiple_json = r#"{"a": 123, "b": "foo"} true [1, 2, 3] wrong chars"#;
let mut deserializer = Deserializer::from_json(multiple_json);
let val: Value = deserializer.deserialize().unwrap();
assert_eq!(val["a"].as_i64().unwrap(), 123);
assert_eq!(val["b"].as_str().unwrap(), "foo");
let val: bool = deserializer.deserialize().unwrap();
assert_eq!(val, true);
let val: Vec<u8> = deserializer.deserialize().unwrap();
assert_eq!(val, &[1, 2, 3]);
// encounter the wrong chars in json
assert!(deserializer.deserialize::<Value>().is_err());
Sourcepub fn into_stream<T>(self) -> StreamDeserializer<'de, T, R> ⓘ
pub fn into_stream<T>(self) -> StreamDeserializer<'de, T, R> ⓘ
Convert Deserializer to a StreamDeserializer
.
Source§impl<'de> Deserializer<Read<'de>>
impl<'de> Deserializer<Read<'de>>
Trait Implementations§
Source§impl<'de, 'a, R: Reader<'de>> Deserializer<'de> for &'a mut Deserializer<R>
impl<'de, 'a, R: Reader<'de>> Deserializer<'de> for &'a mut Deserializer<R>
Source§fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Parses a JSON string as bytes. Note that this function does not check whether the bytes represent a valid UTF-8 string.
Followed as serde_json
.
Source§fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Parses a null
as a None, and any other values as a Some(...)
.
Source§fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_newtype_struct<V>(
self,
name: &'static str,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
Parses a newtype struct as the underlying value.
Source§fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
Parses an enum as an object like {"$KEY":$VALUE}
, where $VALUE is either a straight
value, a [..]
, or a {..}
.
Source§type Error = Error
type Error = Error
Source§fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserializer
to figure out how to drive the visitor based
on what data type is in the input. Read moreSource§fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a bool
value.Source§fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting an i8
value.Source§fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting an i16
value.Source§fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting an i32
value.Source§fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting an i64
value.Source§fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a u8
value.Source§fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a u16
value.Source§fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a u32
value.Source§fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a u64
value.Source§fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a f32
value.Source§fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a f64
value.Source§fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a char
value.Source§fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and does
not benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a string value and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a byte array and would
benefit from taking ownership of buffered data owned by the
Deserializer
. Read moreSource§fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a unit value.Source§fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_unit_struct<V>(
self,
_name: &'static str,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a unit struct with a
particular name.Source§fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values.Source§fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a sequence of values and
knows how many values there are without looking at the serialized data.Source§fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
_len: usize,
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a tuple struct with a
particular name and number of fields.Source§fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a map of key-value pairs.Source§fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_struct<V>(
self,
_name: &'static str,
_fields: &'static [&'static str],
visitor: V,
) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting a struct with a particular
name and fields.Source§fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type is expecting the name of a struct
field or the discriminant of an enum variant.Source§fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>where
V: Visitor<'de>,
Deserialize
type needs to deserialize a value whose type
doesn’t matter because it is ignored. Read moreSource§fn is_human_readable(&self) -> bool
fn is_human_readable(&self) -> bool
Deserialize
implementations should expect to
deserialize their human-readable form. Read more