Inspiration comes from gjson in golang
Add it to your Cargo.toml
file:
[dependencies]
gjson = "*"
Then add it to your code:
rust
extern crate gjson;
GJSON 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", "maintainer": "importcjj", "version": 0.1, "rusts": ["stable", "nightly"] } } "#;
let name = gjson::get(data, "project.name"); println!("{}", name.as_str()); // gjson ```
Value types.
rust
enum Value {
String(String),
Number(Number),
Object(String),
Array(String),
Boolean(bool),
Null,
NotExist,
}
Value has a number of methods that meet your different needs.
rust
value.as_str() -> &str
value.as_u64() -> u64
value.as_i64() -> i64
value.as_f64() -> f64
value.as_bool() -> bool
value.as_array() -> Vec<Value>
value.as_map() -> HashMap<String, Value>
value.get(&str) -> Value
rust
value.exsits() -> bool
value.is_number() -> bool
value.is_string() -> bool
value.is_bool() -> bool
value.is_object() -> bool
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
.
```rust
let v = gjson::get(json, "path"); if v.exsits() { println!("got it {}", value); } ```
Parse
needs to read a complete json element, but get
returns the result immediately, so get
is recommended if you want to simply get a value
JSON example
json
{
"name": {"first": "Tom", "last": "Anderson"},
"age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter",
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
{"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
{"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
]
}
Below is a quick overview of the path syntax, for more complete information please check out GJSON Syntax.
A path is a series of keys separated by a dot. A key may contain special wildcard characters '*' and '?'. To access an array value use the index as the key. To get the number of elements in an array or to access a child path, use the '#' character. The dot and wildcard characters can be escaped with '\'.
name.last >> "Anderson"
age >> 37
children >> ["Sara","Alex","Jack"]
children.# >> 3
children.1 >> "Alex"
child*.2 >> "Jack"
c?ildren.0 >> "Sara"
fav\.movie >> "Deer Hunter"
friends.#.first >> ["Dale","Roger","Jane"]
friends.1.last >> "Craig"
Special purpose characters, such as ., *, and ? can be escaped with .
fav\.movie "Deer Hunter"
The # character allows for digging into JSON Arrays.To get the length of an array you'll just use the # all by itself.
friends.# 3
friends.#.age [44,68,47]
You can also query an array for the first match by using #(...), or find all matches with #(...)#. Queries support the ==, !=, <, <=, >, >= comparison operators and the simple pattern matching % (like) and !% (not like) operators.
friends.#(last=="Murphy").first >> "Dale"
friends.#(last=="Murphy")#.first >> ["Dale","Jane"]
friends.#(age>45)#.last >> ["Craig","Murphy"]
friends.#(first%"D*").last >> "Murphy"
friends.#(nets.#(=="fb"))#.first >> ["Dale","Roger"]
Basically, you can use selectors to assemble whatever you want, and of course, the result is still a json ;)
{name.first,age,"murphys":friends.#(last="Murphy")#.first}
[name.first,age,children.0]
rust
gjson::get(json, "name.[first,last]").as_array();
// It is better than
gjson::get(json, "name.first");
gjson::get(json, "name.last");
Not only string, GJSON also can parse JSON from io::Read.
```rust use std::fs::File;
let f = file::Open("path/to/json").unwrap(); let json = gjson::parsefromread(f); let value = json.get("a.b"); println!("{}", value.as_str()); ```
GJSON can help you get the desired value from flawed JSON, but it's worth being more careful because of its looseness.
be careful!!!
Maybe need a validate function 🤔
$ cargo bench
```
gjson 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%)
3 (3.00%) high mild
serde_json benchmark time: [48.196 us 48.543 us 48.947 us]
change: [+2.9073% +4.4909% +6.3532%] (p = 0.00 < 0.05)
Performance has regressed.
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) high mild
2 (2.00%) high severe
json-rust benchmark time: [24.540 us 24.773 us 25.061 us]
change: [+4.8288% +6.0452% +7.4633%] (p = 0.00 < 0.05)
Performance has regressed.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe
```
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.
MIT License.