Skip to content

feat(): adds ArrayIndex event #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ JSON streaming parser and serializer

JSON event parser is a simple streaming JSON parser and serializer implementation in Rust.

It does not aims to be the fastest JSON parser possible but to be a simple implementation allowing to parse larger than
It does not aim to be the fastest JSON parser possible but to be a simple implementation allowing to parse larger than
memory files.

If you want fast and battle-tested code you might prefer to
Expand Down
86 changes: 43 additions & 43 deletions benches/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,57 @@ use json_event_parser::{JsonEvent, ReaderJsonParser};
use std::fs::{self, read_dir};

fn bench_parse_json_benchmark(c: &mut Criterion) {
for dataset in ["canada", "citm_catalog", "twitter"] {
let data = fs::read(format!(
"{}/benches/json-benchmark/data/{dataset}.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap();
c.bench_function(dataset, |b| {
b.iter(|| {
let mut reader = ReaderJsonParser::new(data.as_slice());
while reader.parse_next().unwrap() != JsonEvent::Eof {
// read more
}
})
});
}
for dataset in ["canada", "citm_catalog", "twitter"] {
let data = fs::read(format!(
"{}/benches/json-benchmark/data/{dataset}.json",
env!("CARGO_MANIFEST_DIR")
))
.unwrap();
c.bench_function(dataset, |b| {
b.iter(|| {
let mut reader = ReaderJsonParser::new(data.as_slice());
while reader.parse_next().unwrap() != JsonEvent::Eof {
// read more
}
})
});
}
}

fn bench_parse_testsuite(c: &mut Criterion) {
let example = load_testsuite_example();
let example = load_testsuite_example();

c.bench_function("JSON test suite", |b| {
b.iter(|| {
let mut reader = ReaderJsonParser::new(example.as_slice());
while reader.parse_next().unwrap() != JsonEvent::Eof {
// read more
}
})
});
c.bench_function("JSON test suite", |b| {
b.iter(|| {
let mut reader = ReaderJsonParser::new(example.as_slice());
while reader.parse_next().unwrap() != JsonEvent::Eof {
// read more
}
})
});
}

fn load_testsuite_example() -> Vec<u8> {
let mut result = Vec::new();
result.extend_from_slice(b"[\n");
for file in read_dir(format!(
"{}/JSONTestSuite/test_parsing",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
{
let file = file.unwrap();
let file_name = file.file_name().to_str().unwrap().to_owned();
if file_name.starts_with("y_") && file_name.ends_with(".json") {
if result.len() > 2 {
result.extend_from_slice(b",\n");
}
result.push(b'\t');
result.extend_from_slice(&fs::read(file.path()).unwrap());
}
let mut result = Vec::new();
result.extend_from_slice(b"[\n");
for file in read_dir(format!(
"{}/JSONTestSuite/test_parsing",
env!("CARGO_MANIFEST_DIR")
))
.unwrap()
{
let file = file.unwrap();
let file_name = file.file_name().to_str().unwrap().to_owned();
if file_name.starts_with("y_") && file_name.ends_with(".json") {
if result.len() > 2 {
result.extend_from_slice(b",\n");
}
result.push(b'\t');
result.extend_from_slice(&fs::read(file.path()).unwrap());
}
result.extend_from_slice(b"\n]");
result
}
result.extend_from_slice(b"\n]");
result
}

criterion_group!(parser, bench_parse_testsuite, bench_parse_json_benchmark);
Expand Down
47 changes: 25 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(
future_incompatible,
nonstandard_style,
rust_2018_idioms,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unused_qualifications
future_incompatible,
nonstandard_style,
rust_2018_idioms,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unused_qualifications
)]

mod read;
mod write;

#[cfg(feature = "async-tokio")]
pub use crate::read::TokioAsyncReaderJsonParser;
pub use crate::read::{
#[cfg(feature = "async-tokio")]
pub use crate::write::TokioAsyncWriterJsonSerializer;
pub use crate::{
read::{
JsonParseError, JsonSyntaxError, LowLevelJsonParser, LowLevelJsonParserResult,
ReaderJsonParser, SliceJsonParser, TextPosition,
},
write::{LowLevelJsonSerializer, WriterJsonSerializer},
};
#[cfg(feature = "async-tokio")]
pub use crate::write::TokioAsyncWriterJsonSerializer;
pub use crate::write::{LowLevelJsonSerializer, WriterJsonSerializer};
use std::borrow::Cow;

/// Possible events during JSON parsing.
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum JsonEvent<'a> {
String(Cow<'a, str>),
Number(Cow<'a, str>),
Boolean(bool),
Null,
StartArray,
EndArray,
StartObject,
EndObject,
ObjectKey(Cow<'a, str>),
Eof,
String(Cow<'a, str>),
Number(Cow<'a, str>),
Boolean(bool),
Null,
StartArray,
EndArray,
ArrayIndex,
StartObject,
EndObject,
ObjectKey(Cow<'a, str>),
Eof,
}

#[cfg(feature = "async-tokio")]
Expand Down
Loading