sonic_rs::serde

Struct Deserializer

Source
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>

Source

pub fn new(read: R) -> Self

Create a new deserializer.

Source

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);
Source

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);
}
Source

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());
}
Source

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());
Source

pub fn into_stream<T>(self) -> StreamDeserializer<'de, T, R>

Convert Deserializer to a StreamDeserializer.

Source§

impl<'de> Deserializer<Read<'de>>

Source

pub fn from_json<I: JsonInput<'de>>(input: I) -> Self

Create a new deserializer from a json input JsonInput.

Source

pub fn from_str(s: &'de str) -> Self

Create a new deserializer from a string.

Source

pub fn from_slice(s: &'de [u8]) -> Self

Create a new deserializer from a string slice.

Trait Implementations§

Source§

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>,

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>,

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>,

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>,

Parses an enum as an object like {"$KEY":$VALUE}, where $VALUE is either a straight value, a [..], or a {..}.

Source§

type Error = Error

The error type that can be returned if some error occurs during deserialization.
Source§

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Source§

fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a bool value.
Source§

fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i8 value.
Source§

fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i16 value.
Source§

fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i32 value.
Source§

fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i64 value.
Source§

fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u8 value.
Source§

fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u16 value.
Source§

fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u32 value.
Source§

fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a u64 value.
Source§

fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f32 value.
Source§

fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a f64 value.
Source§

fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an i128 value. Read more
Source§

fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting an u128 value. Read more
Source§

fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a char value.
Source§

fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
where V: Visitor<'de>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the 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>,

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Source§

fn is_human_readable(&self) -> bool

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more

Auto Trait Implementations§

§

impl<R> Freeze for Deserializer<R>
where R: Freeze,

§

impl<R> !RefUnwindSafe for Deserializer<R>

§

impl<R> Send for Deserializer<R>
where R: Send,

§

impl<R> Sync for Deserializer<R>
where R: Sync,

§

impl<R> Unpin for Deserializer<R>
where R: Unpin,

§

impl<R> !UnwindSafe for Deserializer<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.