Skip to content

Commit

Permalink
feat: support use_rawnumber and use_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Nov 2, 2024
1 parent 5d6a65d commit be587a8
Show file tree
Hide file tree
Showing 35 changed files with 780 additions and 909 deletions.
21 changes: 9 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ version = "0.3.14"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bumpalo = "3.13"
bytes = "1.8"
cfg-if = "1.0"
faststr = { version = "0.2", features = ["serde"] }
itoa = "1.0"
parking_lot = "0.12"
ryu = "1.0"
serde = { version = "1.0", features = ["rc", "derive"] }
simdutf8 = "0.1"
smallvec = "1.11"
static_assertions = "1.1"
thiserror = "1.0"
bumpalo = "3.13"
bytes = "1.8"
cfg-if = "1.0"
faststr = { version = "0.2", features = ["serde"] }
itoa = "1.0"
ryu = "1.0"
serde = { version = "1.0", features = ["rc", "derive"] }
simdutf8 = "0.1"
thiserror = "1.0"

[dev-dependencies]
encoding_rs = "0.8"
Expand Down
4 changes: 0 additions & 4 deletions docs/value_design.md

This file was deleted.

16 changes: 15 additions & 1 deletion fuzz/fuzz_targets/from_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ fuzz_target!(|data: &[u8]| {
let sv2: Value = from_str(&sout).unwrap();
let eq = compare_value(&jv2, &sv2);

// compre use raw
fuzz_use_raw(data, &sv);

if jv.is_object() && eq {
for ret in to_object_iter(data) {
let (k, lv) = ret.unwrap();
Expand Down Expand Up @@ -99,7 +102,8 @@ fuzz_target!(|data: &[u8]| {
}
}
Err(_) => {
let _ = from_slice::<Value>(data).unwrap_err();
let _ = from_slice::<Value>(data)
.expect_err(&format!("parse invalid json {:?} failed", data));
}
}

Expand All @@ -114,6 +118,16 @@ fn compare_lazyvalue(jv: &JValue, sv: &sonic_rs::LazyValue) {
compare_value(jv, &sv2);
}

fn fuzz_use_raw(json: &[u8], sv: &sonic_rs::Value) {
use sonic_rs::{Deserialize, Deserializer, Value};
let json = unsafe { std::str::from_utf8_unchecked(json) };
let mut de = Deserializer::from_str(json).use_raw();
let value: Value = Deserialize::deserialize(&mut de).unwrap();
let out = sonic_rs::to_string(&value).unwrap();
let got: Value = sonic_rs::from_str(&out).unwrap();
assert_eq!(&got, sv);
}

fn compare_value(jv: &JValue, sv: &sonic_rs::Value) -> bool {
match *jv {
JValue::Object(ref obj) => {
Expand Down
41 changes: 5 additions & 36 deletions scripts/sanitize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,10 @@ set -ex

export ASAN_OPTIONS="disable_coredump=0:unmap_shadow_on_exit=1:abort_on_error=1"

testcase_lists() {
cargo test -- -Zunstable-options --list --format json
local result=$?
if [ ${result} -ne 0 ]; then
exit -1
fi
cargo test -- -Zunstable-options --list --format json | jq -c 'select(.type=="test") | .name' | awk -F'"' '{print $2}' | awk '{print ($2) ? $3 : $1}'
return $?
}

sanitize() {
local san="$1"
local target="$2"
local testcase="$3"
# use single thread to make error info more readable and accurate
RUSTFLAGS="-Zsanitizer=${san}" RUSTDOCFLAGS="-Zsanitizer=${san}" cargo test --target ${target} ${testcase} -- --test-threads=1
RUSTFLAGS="-Zsanitizer=${san}" RUSTDOCFLAGS="-Zsanitizer=${san}" cargo test --doc --package sonic-rs --target ${target} ${testcase} -- --show-output --test-threads=1
}

sanitize_single() {
local san="$1"
local target="$2"
local lists=$(testcase_lists)
for case in ${lists}; do
sanitize ${san} ${target} ${case}
done
}

main() {
for san in address leak; do
echo "Running tests with $san"
sanitize_single $san "x86_64-unknown-linux-gnu"
done
}

main "$@"
for san in address leak; do
echo "Running tests with $san"
RUSTFLAGS="-Zsanitizer=${san}" RUSTDOCFLAGS="-Zsanitizer=${san}" cargo +nightly test --target x86_64-unknown-linux-gnu -- --test-threads=1
RUSTFLAGS="-Zsanitizer=${san}" RUSTDOCFLAGS="-Zsanitizer=${san}" cargo +nightly test --doc --package sonic-rs --target x86_64-unknown-linux-gnu -- --show-output --test-threads=1
done


5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct DeserializeCfg {
pub(crate) use_rawnumber: bool,
pub(crate) use_raw: bool,
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ mod test {

#[test]
fn test_serde_errors_display() {
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct Foo {
a: Vec<i32>,
Expand Down
1 change: 0 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
util::{private::Sealed, reborrow::DormantMutRef},
value::{object::DEFAULT_OBJ_CAP, shared::Shared},
JsonValueMutTrait, JsonValueTrait, PointerNode, Value,
};

Expand Down
4 changes: 2 additions & 2 deletions src/lazyvalue/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ where
let slice = json.to_u8_slice();
let reader = Read::new(slice, false);
let mut parser = Parser::new(reader);
let (sub, status) = parser.get_from_with_iter(path)?;
let (sub, status) = parser.get_from_with_iter_unchecked(path)?;
LazyValue::new(json.from_subset(sub), status == ParseStatus::HasEscaped)
}

Expand Down Expand Up @@ -388,7 +388,7 @@ where
let slice = json.to_u8_slice();
let reader = Read::new(slice, false);
let mut parser = Parser::new(reader);
let (sub, status) = parser.get_from_with_iter_checked(path)?;
let (sub, status) = parser.get_from_with_iter(path)?;
let lv = LazyValue::new(json.from_subset(sub), status == ParseStatus::HasEscaped)?;

// validate the utf-8 if slice
Expand Down
1 change: 0 additions & 1 deletion src/lazyvalue/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ impl JsonValueTrait for OwnedLazyValue {
}
}

#[cfg(feature = "arbitrary_precision")]
fn as_raw_number(&self) -> Option<crate::RawNumber> {
if let Ok(num) = from_str(self.as_raw_str()) {
Some(num)
Expand Down
5 changes: 1 addition & 4 deletions src/lazyvalue/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ 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,
JsonValueTrait, RawNumber, Result,
};

/// LazyValue wrappers a unparsed raw JSON text. It is borrowed from the origin JSON text.
Expand Down Expand Up @@ -200,7 +198,6 @@ impl<'a> JsonValueTrait for LazyValue<'a> {
}
}

#[cfg(feature = "arbitrary_precision")]
fn as_raw_number(&self) -> Option<RawNumber> {
if let Ok(num) = from_str(self.as_raw_str()) {
Some(num)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(clippy::needless_lifetimes)]
#![doc(test(attr(warn(unused))))]

mod config;
mod error;
mod index;
mod input;
Expand Down
Loading

0 comments on commit be587a8

Please sign in to comment.