Skip to content

Commit

Permalink
bump 0.3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Jul 5, 2024
1 parent b86259d commit 2917fac
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sonic-rs"
version = "0.3.7"
version = "0.3.8"
authors = ["Volo Team <[email protected]>"]
edition = "2021"
description = "Sonic-rs is a fast Rust JSON library based on SIMD"
Expand Down
2 changes: 2 additions & 0 deletions src/lazyvalue/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
50 changes: 48 additions & 2 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 2917fac

Please sign in to comment.