#json-parser #json #gjson

ajson

Rust JSON 解析器 - 快速获取 JSON 值

9 个版本

0.3.1 2022年8月30日
0.3.0 2022年8月26日
0.2.4 2020年11月19日
0.2.3 2019年12月29日
0.1.1 2019年8月4日

#489编码

Download history 108/week @ 2024-03-11 111/week @ 2024-03-18 102/week @ 2024-03-25 168/week @ 2024-04-01 80/week @ 2024-04-08 87/week @ 2024-04-15 179/week @ 2024-04-22 101/week @ 2024-04-29 107/week @ 2024-05-06 105/week @ 2024-05-13 109/week @ 2024-05-20 91/week @ 2024-05-27 148/week @ 2024-06-03 146/week @ 2024-06-10 105/week @ 2024-06-17 136/week @ 2024-06-24

549 每月下载量
用于 6 crates

MIT 许可证

77KB
2K SLoC

A-JSON

快速读取 JSON 值 - Rust JSON 解析器

名称更改为 AJSON,请参阅 问题

灵感来源于 golang 中的 gjson

安装

将其添加到您的 Cargo.toml 文件中

[dependencies]
ajson = "0.3"

待办事项

  • 添加文档
  • 遵循 api-guidelines
  • 更新基准
  • 优化

一个简单的例子

AJSON 通过指定的路径获取 JSON 值,例如 project.nameproject.version。当路径匹配时,它会立即返回!

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

let name = ajson::get(data, "project.name").unwrap().unwrap();
println!("{}", name.as_str()); // ajson

路径语法

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"]}
    ]
}

基本

以下是路径语法的快速概述,更多完整信息请参阅 GJSON 语法。

路径是一系列由点分隔的键。一个键可能包含特殊的通配符字符 '*' 和 '?'. 要访问数组值,请使用索引作为键。要获取数组的元素数量或访问子路径,请使用 '#' 字符。点和通配符字符可以用 '' 转义。

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"

转义字符

特殊用途字符,如 ., *, 和 ?,可以用 . 转义。

fav\.movie             "Deer Hunter"

数组

'#' 字符允许深入到 JSON 数组中。要获取数组的长度,只需使用 '#' 即可。

friends.#              3
friends.#.age         [44,68,47]

查询

您还可以使用 #(...), 查询数组以找到第一个匹配项,或使用 #(...)# 找到所有匹配项。查询支持 ==, !=, <, <=, >, >= 比较运算符和简单的模式匹配 % (like) 和 !% (not like) 运算符。

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"]

构造

基本上,您可以使用选择器组装您想要的任何内容,当然,结果仍然是 JSON ;)

{name.first,age,"murphys":friends.#(last="Murphy")#.first}
[name.first,age,children.0]
ajson::get(json, "name.[first,last]").unwrap().unwrap().to_vec();
ajson::get(json, "name.first").unwrap().unwrap(); 
ajson::get(json, "name.last").unwrap().unwrap();

值类型。

enum Value {
    String(String),
    Number(Number),
    Object(String),
    Array(String),
    Boolean(bool),
    Null,
}

值具有满足您不同需求的多种方法。

value.get(&str) -> Option<Value>
value.as_str() -> &str
value.to_u64() -> u64
value.to_i64() -> i64
value.to_f64() -> f64
value.to_bool() -> bool
value.to_vec() -> Vec<Value>
value.to_object() -> HashMap<String, Value>
value.is_number() -> bool
value.is_string() -> bool
value.is_bool() -> bool
value.is_object() -> bool
value.is_array() -> bool
value.is_null() -> bool

性能

$cargo bench

ajson benchmark         time:   [2.0816 us 2.0865 us 2.0917 us]                             
                        change: [+0.6172% +0.9272% +1.2430%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 11 outliers among 100 measurements (11.00%)
  7 (7.00%) high mild
  4 (4.00%) high severe

serde_json benchmark    time:   [23.033 us 23.076 us 23.119 us]                                  
                        change: [-0.7185% -0.3455% +0.0230%] (p = 0.07 > 0.05)
                        No change in performance detected.
Found 7 outliers among 100 measurements (7.00%)
  6 (6.00%) high mild
  1 (1.00%) high severe

json-rust benchmark     time:   [12.225 us 12.289 us 12.381 us]                                 
                        change: [-2.6200% -1.1789% +0.8442%] (p = 0.19 > 0.05)
                        No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe

ajson selector          time:   [1.1523 us 1.1561 us 1.1604 us]                            
                        change: [+0.1567% +0.7278% +1.2945%] (p = 0.01 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild

ajson multi query       time:   [559.19 ns 559.96 ns 560.77 ns]                               
                        change: [-1.4268% -1.0380% -0.6698%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild

serde derive            time:   [4.5301 us 4.5403 us 4.5507 us]                          
                        change: [-2.3423% -1.9438% -1.5697%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

serde derive multi query                        
                        time:   [956.86 ns 962.64 ns 970.05 ns]
                        change: [-1.7069% -1.0299% -0.2924%] (p = 0.01 < 0.05)
                        Change within noise threshold.
Found 9 outliers among 100 measurements (9.00%)
  3 (3.00%) high mild
  6 (6.00%) high severe

nom json bench          time:   [2.9468 us 2.9515 us 2.9566 us]
Found 5 outliers among 100 measurements (5.00%)
  4 (4.00%) high mild
  1 (1.00%) high severe
  • MacBook Pro (13英寸,2018年,四个 Thunderbolt 3 端口)
  • 2.7 GHz Intel Core i7
  • 16 GB 2133 MHz LPDDR3

问题

AJSON 刚完成,可能存在一些错误和不足,请随时提出。另外,Rust 是我的新语言,也许 ajson 还不够 Rust,所以我希望您能提出一些建议。

许可证

MIT 许可证。

依赖项

~52–570KB