Skip to content

Commit

Permalink
partial parsing with JsonValue (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored Oct 31, 2024
1 parent 0496e89 commit e1d4c9f
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 159 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resolver = "2"

[workspace.package]
authors = ["Samuel Colvin <samuel@pydantic.dev>"]
version = "0.6.1"
version = "0.7.0"
edition = "2021"
license = "MIT"
keywords = ["JSON", "parsing", "deserialization", "iter"]
Expand Down
4 changes: 2 additions & 2 deletions crates/jiter/benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn string_array_jiter_value_owned(bench: &mut Bencher) {
let json = read_file("./benches/string_array.json");
let json_data = json.as_bytes();
bench.iter(|| {
let v = JsonValue::parse_owned(black_box(json_data), false).unwrap();
let v = JsonValue::parse_owned(black_box(json_data), false, false).unwrap();
black_box(v)
})
}
Expand All @@ -267,7 +267,7 @@ fn medium_response_jiter_value_owned(bench: &mut Bencher) {
let json = read_file("./benches/medium_response.json");
let json_data = json.as_bytes();
bench.iter(|| {
let v = JsonValue::parse_owned(black_box(json_data), false).unwrap();
let v = JsonValue::parse_owned(black_box(json_data), false, false).unwrap();
black_box(v)
})
}
Expand Down
12 changes: 12 additions & 0 deletions crates/jiter/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ impl JsonError {
let position = self.get_position(json_data);
format!("{} at {}", self.error_type, position)
}

pub(crate) fn allowed_if_partial(&self) -> bool {
matches!(
self.error_type,
JsonErrorType::EofWhileParsingList
| JsonErrorType::EofWhileParsingObject
| JsonErrorType::EofWhileParsingString
| JsonErrorType::EofWhileParsingValue
| JsonErrorType::ExpectedListCommaOrEnd
| JsonErrorType::ExpectedObjectCommaOrEnd
)
}
}

impl std::fmt::Display for JsonError {
Expand Down
2 changes: 2 additions & 0 deletions crates/jiter/src/jiter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl<'j> Jiter<'j> {
&mut self.tape,
DEFAULT_RECURSION_LIMIT,
self.allow_inf_nan,
false,
)
.map_err(Into::into)
}
Expand Down Expand Up @@ -288,6 +289,7 @@ impl<'j> Jiter<'j> {
&mut self.tape,
DEFAULT_RECURSION_LIMIT,
self.allow_inf_nan,
false,
)
.map_err(Into::into)
}
Expand Down
10 changes: 1 addition & 9 deletions crates/jiter/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,7 @@ impl<'j, StringCache: StringMaybeCache, KeyCheck: MaybeKeyCheck, ParseNumber: Ma

fn _allow_partial_err(&self, e: &JsonError) -> bool {
if self.partial_mode.is_active() {
matches!(
e.error_type,
JsonErrorType::EofWhileParsingList
| JsonErrorType::EofWhileParsingObject
| JsonErrorType::EofWhileParsingString
| JsonErrorType::EofWhileParsingValue
| JsonErrorType::ExpectedListCommaOrEnd
| JsonErrorType::ExpectedObjectCommaOrEnd
)
e.allowed_if_partial()
} else {
false
}
Expand Down
Loading

0 comments on commit e1d4c9f

Please sign in to comment.