Skip to content

Commit

Permalink
Add nesting limit for JsonStreamReader
Browse files Browse the repository at this point in the history
Resolves #28
  • Loading branch information
Marcono1234 committed Dec 26, 2023
1 parent c6ecb8f commit d0e3ca3
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 50 deletions.
18 changes: 16 additions & 2 deletions benches/reader_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ fn bench_compare(c: &mut Criterion, name: &str, json: &str) {
group.bench_with_input("struson-skip", json, |b, json| {
b.iter(|| {
call_unwrap(|| {
let mut json_reader = JsonStreamReader::new(json.as_bytes());
let mut json_reader = JsonStreamReader::new_custom(
json.as_bytes(),
ReaderSettings {
max_nesting_depth: None,
..Default::default()
},
);
json_reader.skip_value()?;
json_reader.consume_trailing_whitespace()?;
Ok(())
Expand All @@ -28,6 +34,7 @@ fn bench_compare(c: &mut Criterion, name: &str, json: &str) {
json.as_bytes(),
ReaderSettings {
track_path: false,
max_nesting_depth: None,
..Default::default()
},
);
Expand Down Expand Up @@ -112,7 +119,13 @@ fn bench_compare(c: &mut Criterion, name: &str, json: &str) {
group.bench_with_input("struson-read", json, |b, json| {
b.iter(|| {
call_unwrap(|| {
let json_reader = JsonStreamReader::new(json.as_bytes());
let json_reader = JsonStreamReader::new_custom(
json.as_bytes(),
ReaderSettings {
max_nesting_depth: None,
..Default::default()
},
);
struson_read(json_reader)
});
})
Expand All @@ -125,6 +138,7 @@ fn bench_compare(c: &mut Criterion, name: &str, json: &str) {
json.as_bytes(),
ReaderSettings {
track_path: false,
max_nesting_depth: None,
..Default::default()
},
);
Expand Down
18 changes: 18 additions & 0 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,17 +692,20 @@ pub enum UnexpectedStructureKind {
/// Index (starting at 0) of the expected item
expected_index: u32,
},

/// A JSON object does not have a member with a certain name
MissingObjectMember {
/// Name of the expected member
member_name: String,
},

/// A JSON array or object has fewer elements than expected
///
/// This is a more general version than [`TooShortArray`](UnexpectedStructureKind::TooShortArray)
/// and [`MissingObjectMember`](UnexpectedStructureKind::MissingObjectMember) where it is only known
/// that more elements are expected without knowing the expected index or member name.
FewerElementsThanExpected,

/// A JSON array or object has more elements than expected
MoreElementsThanExpected,
}
Expand All @@ -715,6 +718,7 @@ pub enum ReaderError {
/// A syntax error was encountered
#[error("syntax error: {0}")]
SyntaxError(#[from] JsonSyntaxError),

/// The next JSON value had an unexpected type
///
/// This error can occur for example when trying to read a JSON number when the next value is actually
Expand All @@ -728,6 +732,7 @@ pub enum ReaderError {
/// Location where the error occurred in the JSON document
location: JsonReaderPosition,
},

/// The JSON document had an unexpected structure
///
/// This error occurs when trying to consume more elements than a JSON array or object has, or
Expand All @@ -751,6 +756,18 @@ pub enum ReaderError {
/// Location where the error occurred in the JSON document
location: JsonReaderPosition,
},

/// The maximum nesting depth was exceeded while reading
///
/// See [`ReaderSettings::max_nesting_depth`] for more information.
#[error("maximum nesting depth {max_nesting_depth} exceeded at {location}")]
MaxNestingDepthExceeded {
/// The maximum nesting depth
max_nesting_depth: u32,
/// Location within the JSON document
location: JsonReaderPosition,
},

/// An unsupported JSON number value was encountered
///
/// See [`ReaderSettings::restrict_number_values`] for more information.
Expand All @@ -761,6 +778,7 @@ pub enum ReaderError {
/// Location of the number value within the JSON document
location: JsonReaderPosition,
},

/// An IO error occurred while trying to read from the underlying reader, or
/// malformed UTF-8 data was encountered
#[error("IO error '{error}' at (roughly) {location}")]
Expand Down
Loading

0 comments on commit d0e3ca3

Please sign in to comment.