Skip to content

Commit

Permalink
Change name to rjson, see #2
Browse files Browse the repository at this point in the history
  • Loading branch information
importcjj committed Aug 2, 2019
1 parent 67aa40b commit dafe2b2
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "gjson"
name = "rjson"
version = "0.1.2"
authors = ["importcjj <[email protected]>"]
license = "MIT"
description = "JSON Parser for Rust - Get JSON values quickly"
keywords = ["json", "json-parser", "gjson"]
keywords = ["json", "json-parser", "gjson", "rjson"]
categories = ["encoding"]
readme = "README.md"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE-MIT"]
Expand All @@ -19,5 +19,5 @@ json = "0.11.13"
serde_json = "1.0"

[[bench]]
name = "gjson_benchmark"
name = "rjson_benchmark"
harness = false
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div align="center">
<img alt="GJSON" src="logo.png">
<!-- <img alt="RJSON" src="logo.png"> -->
<h1>R-JSON</h1>
<p>Read JSON values quickly - Rust JSON Parser</p>

<p>Get JSON values quickly - JSON Parser for Rust</p>

<a href="https://github.com/importcjj/gjson">
<img src="https://travis-ci.com/importcjj/gjson.svg?branch=master"></a>
<a href="https://github.com/importcjj/rjson">
<img src="https://travis-ci.com/importcjj/rjson.svg?branch=master"></a>

<img src="https://img.shields.io/badge/crates.io-0.1.2-blue">

<a href="https://importcjj.github.io/rust-gjson-playground/">
<a href="https://importcjj.github.io/rust-rjson-playground/">
<img src="https://img.shields.io/badge/goto-playground-orange">

</a>
Expand All @@ -22,31 +22,31 @@ Inspiration comes from [gjson](https://github.com/tidwall/gjson) in golang
Add it to your `Cargo.toml` file:
```
[dependencies]
gjson = "0.1"
rjson = "0.1"
```
Then add it to your code:
```rust
extern crate gjson;
extern crate rjson;
```

## Enjoy

GJSON get json value with specified path, such as `project.name` or `project.version`. When the path matches, it returns immediately!
RJSON get json value with specified path, such as `project.name` or `project.version`. When the path matches, it returns immediately!

```rust
let data = r#"
{
"project": {
"name": "gjson",
"name": "rjson",
"maintainer": "importcjj",
"version": 0.1,
"rusts": ["stable", "nightly"]
}
}
"#;

let name = gjson::get(data, "project.name");
println!("{}", name.as_str()); // gjson
let name = rjson::get(data, "project.name");
println!("{}", name.as_str()); // rjson
```


Expand Down Expand Up @@ -81,7 +81,7 @@ value.get(&str) -> Value


```rust
value.exsits() -> bool
value.exists() -> bool
value.is_number() -> bool
value.is_string() -> bool
value.is_bool() -> bool
Expand All @@ -90,12 +90,12 @@ value.is_array() -> bool
value.is_null() -> bool
```

Sometimes you need to check if value exists, you can use `exsits()`. Notice that when used for `null` values, exsits returns `true`.
Sometimes you need to check if value exists, you can use `exists()`. Notice that when used for `null` values, exists returns `true`.

```rust

let v = gjson::get(json, "path");
if v.exsits() {
let v = rjson::get(json, "path");
if v.exist() {
println!("got it {}", value);
}
```
Expand Down Expand Up @@ -176,27 +176,27 @@ Basically, you can use selectors to assemble whatever you want, and of course, t
```

```rust
gjson::get(json, "name.[first,last]").as_array();
gjson::get(json, "name.first");
gjson::get(json, "name.last");
rjson::get(json, "name.[first,last]").as_array();
rjson::get(json, "name.first");
rjson::get(json, "name.last");
```

## io::Read

Not only string, GJSON also can parse JSON from io::Read.
Not only string, RJSON also can parse JSON from io::Read.

```rust
use std::fs::File;

let f = file::Open("path/to/json").unwrap();
let json = gjson::parse_from_read(f);
let json = rjson::parse_from_read(f);
let value = json.get("a.b");
println!("{}", value.as_str());
```

## Validate

GJSON can help you get the desired value from flawed JSON, but it's worth being more careful because of its looseness.
RJSON can help you get the desired value from flawed JSON, but it's worth being more careful because of its looseness.

`be careful!!!`

Expand All @@ -206,12 +206,12 @@ Maybe need a validate function 🤔

`$ cargo bench`

* [gjson](https://github.com/importcjj/gjson)
* [rjson](https://github.com/importcjj/rjson)
* [serde_json](https://github.com/serde-rs/json)
* [rust-json](https://github.com/maciejhirsz/json-rust)

```
gjson benchmark time: [6.7000 us 6.8023 us 6.9081 us]
rjson benchmark time: [6.7000 us 6.8023 us 6.9081 us]
change: [-1.8368% -0.4152% +1.0466%] (p = 0.58 > 0.05)
No change in performance detected.
Found 3 outliers among 100 measurements (3.00%)
Expand All @@ -238,7 +238,7 @@ Found 5 outliers among 100 measurements (5.00%)

## problems

GJSON has just been finished, there may be some bugs and shortcomings, please feel free to issue. Also, Rust is a new language for me, and maybe gjson isn't rust enough, so I hope you have some suggestions.
RJSON has just been finished, there may be some bugs and shortcomings, please feel free to issue. Also, Rust is a new language for me, and maybe rjson isn't rust enough, so I hope you have some suggestions.

## License
MIT License.
40 changes: 20 additions & 20 deletions benches/gjson_benchmark.rs → benches/rjson_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate criterion;

use criterion::black_box;
use criterion::Criterion;
extern crate gjson;
extern crate rjson;
extern crate json;
extern crate serde_json;

Expand Down Expand Up @@ -63,25 +63,25 @@ static BENCH_DATA: &'static str = r#"{
// }
// }

fn gjson_selector(json: &str) {
gjson::get(json, "widget.[image.src,text.data]").as_array();
fn rjson_selector(json: &str) {
rjson::get(json, "widget.[image.src,text.data]").as_array();
}

fn gjson_multi_query(json: &str) {
fn rjson_multi_query(json: &str) {
[
gjson::get(json, "widget.image.src"),
gjson::get(json, "widget.text.data"),
rjson::get(json, "widget.image.src"),
rjson::get(json, "widget.text.data"),
];
}

fn gjson_bench(json: &str) {
gjson::get(json, "widget.window.name").as_str();
gjson::get(json, "widget.image.hOffset").as_f64();
gjson::get(json, "widget.text.onMouseUp").as_str();
gjson::get(json, "widget.debug").as_str();
// gjson::get(json, "widget.text").as_map();
gjson::get(json, "widget.menu.#(sub_item>7)#.title").as_array();
// gjson::get(json, "widget.menu.[1.title,2.options]").as_array();
fn rjson_bench(json: &str) {
rjson::get(json, "widget.window.name").as_str();
rjson::get(json, "widget.image.hOffset").as_f64();
rjson::get(json, "widget.text.onMouseUp").as_str();
rjson::get(json, "widget.debug").as_str();
// rjson::get(json, "widget.text").as_map();
rjson::get(json, "widget.menu.#(sub_item>7)#.title").as_array();
// rjson::get(json, "widget.menu.[1.title,2.options]").as_array();
}

fn json_rust_bench(data: &str) {
Expand Down Expand Up @@ -129,20 +129,20 @@ fn serde_json_bench(json: &str) {

fn criterion_benchmark(c: &mut Criterion) {
// c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20))));
c.bench_function("gjson benchmark", |b| {
b.iter(|| gjson_bench(black_box(BENCH_DATA)))
c.bench_function("rjson benchmark", |b| {
b.iter(|| rjson_bench(black_box(BENCH_DATA)))
});
c.bench_function("serde_json benchmark", |b| {
b.iter(|| serde_json_bench(black_box(BENCH_DATA)))
});
c.bench_function("json-rust benchmark", |b| {
b.iter(|| json_rust_bench(black_box(BENCH_DATA)))
});
c.bench_function("gjson selector", |b| {
b.iter(|| gjson_selector(black_box(BENCH_DATA)))
c.bench_function("rjson selector", |b| {
b.iter(|| rjson_selector(black_box(BENCH_DATA)))
});
c.bench_function("gjson multi query", |b| {
b.iter(|| gjson_multi_query(black_box(BENCH_DATA)))
c.bench_function("rjson multi query", |b| {
b.iter(|| rjson_multi_query(black_box(BENCH_DATA)))
});
}

Expand Down
Binary file removed logo.png
Binary file not shown.
4 changes: 2 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ pub enum QueryValue {
F64(f64),
Boolean(bool),
Null,
NotExsit,
NotExist,
}

impl QueryValue {
pub fn exists(&self) -> bool {
*self != QueryValue::NotExsit
*self != QueryValue::NotExist
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/path_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ fn parse_query_from_utf8<'a>(v: &'a [u8]) -> (Query<'a>, usize) {
let mut depth = 1;
let mut end = 0;

let mut op_exsit = false;
let mut op_exist = false;
let mut op_start = 0;
let mut op_end = 0;

while let Some(b) = reader.peek() {
match b {
b'!' | b'=' | b'<' | b'>' | b'%' => {
if depth == 1 {
if !op_exsit {
op_exsit = true;
if !op_exist {
op_exist = true;
op_start = reader.position();
op_end = op_start;
} else {
Expand Down Expand Up @@ -132,7 +132,7 @@ fn parse_query_from_utf8<'a>(v: &'a [u8]) -> (Query<'a>, usize) {
}
b' ' => (),
_ => {
if op_exsit {
if op_exist {
let (val, offset) = parser_query_value(reader.tail(v));
if val.exists() {
q.set_val(val);
Expand All @@ -149,7 +149,7 @@ fn parse_query_from_utf8<'a>(v: &'a [u8]) -> (Query<'a>, usize) {

q.set_on(true);

if op_exsit {
if op_exist {
q.set_path(util::trim_space_u8(&v[..op_start]));
q.set_op(String::from_utf8_lossy(reader.slice(op_start, op_end)).to_string());
} else if end > 0 {
Expand Down Expand Up @@ -181,7 +181,7 @@ fn parser_query_value(v: &[u8]) -> (QueryValue, usize) {
b'"' => {
let (start, end) = reader.read_str_value();
if end - start < 2 {
QueryValue::NotExsit
QueryValue::NotExist
} else {
let raw = reader.slice(start + 1, end - 1);
let s = String::from_utf8_lossy(raw).to_string();
Expand All @@ -193,13 +193,13 @@ fn parser_query_value(v: &[u8]) -> (QueryValue, usize) {
let n = Number::from(&mut reader);
QueryValue::F64(n.as_f64())
}
_ => QueryValue::NotExsit,
_ => QueryValue::NotExist,
};

return (value, reader.offset() - 1);
}

(QueryValue::NotExsit, 0)
(QueryValue::NotExist, 0)
}

// fn parse_query<'a>(v: &'a [u8]) -> (Query<'a>, usize) {
Expand Down Expand Up @@ -279,7 +279,7 @@ fn new_query_from_utf8<'a>(v: &'a [u8]) -> Query<'a> {
// let mut current_path = Path::new();
// let mut reader = reader::RefReader::new(v);
// let mut end = 0;
// let mut part_exsit = true;
// let mut part_exist = true;
// let mut depth = 0;
// current_path.set_ok(true);
// while let Some(b) = reader.peek() {
Expand Down Expand Up @@ -326,10 +326,10 @@ fn new_query_from_utf8<'a>(v: &'a [u8]) -> Query<'a> {
// }

// if depth == 0 && reader.position() == 0 {
// part_exsit = false;
// part_exist = false;
// }

// if part_exsit {
// if part_exist {
// // println!("set path part {}", end);
// current_path.set_part(reader.head(v, end));
// } else {
Expand Down
2 changes: 1 addition & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Value {
impl fmt::Debug for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Value::NotExist => write!(f, "<NOT EXSIT>"),
Value::NotExist => write!(f, "<NOT Exist>"),
Value::String(_) => write!(f, r#""{}""#, self.as_str()),
_ => write!(f, "{}", self.as_str()),
}
Expand Down
Loading

0 comments on commit dafe2b2

Please sign in to comment.