From 2917fac8f1980c1cba0dfc9dd5065f72500225b3 Mon Sep 17 00:00:00 2001 From: liuq19 Date: Fri, 5 Jul 2024 18:44:50 +0800 Subject: [PATCH] bump 0.3.8 --- Cargo.toml | 2 +- src/lazyvalue/value.rs | 2 ++ src/lib.rs | 2 ++ src/reader.rs | 50 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b5f8625..ab17c50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sonic-rs" -version = "0.3.7" +version = "0.3.8" authors = ["Volo Team "] edition = "2021" description = "Sonic-rs is a fast Rust JSON library based on SIMD" diff --git a/src/lazyvalue/value.rs b/src/lazyvalue/value.rs index f294c11..521f0a8 100644 --- a/src/lazyvalue/value.rs +++ b/src/lazyvalue/value.rs @@ -9,6 +9,8 @@ use std::{ use faststr::FastStr; +#[cfg(feature = "arbitrary_precision")] +use crate::RawNumber; use crate::{ from_str, get_unchecked, index::Index, input::JsonSlice, serde::Number, JsonType, JsonValueTrait, Result, diff --git a/src/lib.rs b/src/lib.rs index 7d74ccb..d3563e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,8 @@ pub mod writer; pub use ::faststr::FastStr; // re-export the serde trait pub use ::serde::{Deserialize, Serialize}; +#[doc(inline)] +pub use reader::Read; #[doc(inline)] pub use crate::error::{Error, Result}; diff --git a/src/reader.rs b/src/reader.rs index 7498734..e5b37c1 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -95,7 +95,30 @@ pub trait Reader<'de>: Sealed { } } -/// JSON input source that reads from a slice of bytes. +/// JSON input source that reads from a string/bytes-like JSON input. +/// +/// Support most common types: &str, &[u8], &FastStr, &Bytes and &String +/// +/// # Examples +/// ``` +/// use bytes::Bytes; +/// use faststr::FastStr; +/// use serde::de::Deserialize; +/// use sonic_rs::{Deserializer, Read}; +/// +/// let mut de = Deserializer::new(Read::from(r#"123"#)); +/// let num: i32 = Deserialize::deserialize(&mut de).unwrap(); +/// assert_eq!(num, 123); +/// +/// let mut de = Deserializer::new(Read::from(r#"123"#.as_bytes())); +/// let num: i32 = Deserialize::deserialize(&mut de).unwrap(); +/// assert_eq!(num, 123); +/// +/// let f = FastStr::new("123"); +/// let mut de = Deserializer::new(Read::from(&f)); +/// let num: i32 = Deserialize::deserialize(&mut de).unwrap(); +/// assert_eq!(num, 123); +/// ``` pub struct Read<'a> { slice: &'a [u8], pub(crate) index: usize, @@ -335,8 +358,11 @@ impl<'a> Reader<'a> for PaddedSliceRead<'a> { #[cfg(test)] mod test { - use super::*; + use bytes::Bytes; + use faststr::FastStr; + use super::*; + use crate::{Deserialize, Deserializer}; fn test_peek() { let data = b"1234567890"; let mut reader = Read::new(data, false); @@ -371,4 +397,24 @@ mod test { test_next(); test_index(); } + + macro_rules! test_deserialize_reader { + ($json:expr) => { + let mut de = Deserializer::new(Read::from($json)); + let num: i32 = Deserialize::deserialize(&mut de).unwrap(); + assert_eq!(num, 123); + }; + } + + #[test] + fn test_deserialize() { + let b = Bytes::from(r#"123"#); + let f = FastStr::from(r#"123"#); + let s = String::from(r#"123"#); + test_deserialize_reader!(r#"123"#); + test_deserialize_reader!(r#"123"#.as_bytes()); + test_deserialize_reader!(&b); + test_deserialize_reader!(&f); + test_deserialize_reader!(&s); + } }