6 个版本

0.1.5 2024 年 6 月 26 日
0.1.4 2024 年 6 月 24 日
0.1.3 2024 年 5 月 9 日
0.1.2 2024 年 4 月 28 日
0.0.0 2023 年 11 月 13 日

##198解析实现

Download history 275/week @ 2024-04-27 485/week @ 2024-05-04 279/week @ 2024-05-11 155/week @ 2024-05-18 191/week @ 2024-05-25 190/week @ 2024-06-01 375/week @ 2024-06-08 273/week @ 2024-06-15 761/week @ 2024-06-22 644/week @ 2024-06-29 470/week @ 2024-07-06 239/week @ 2024-07-13 316/week @ 2024-07-20 408/week @ 2024-07-27 397/week @ 2024-08-03 269/week @ 2024-08-10

1,410 每月下载量

Apache-2.0

225KB
5.5K SLoC

jsonata-rs

crates.io docs.rs

Rust 中 JSONata 的(不完整)实现。Stedi 对 jsonata-rust 的分支。

Alpha 版本。所有内部和外部接口均视为不稳定,且可能未经通知而更改。

什么是 JSONata?

来自 JSONata 网站的内容

  • 轻量级 JSON 数据查询和转换语言
  • 灵感来源于 XPath 3.1 的位置路径语义
  • 具有最少语法的复杂查询表达式
  • 内置操作符和函数,用于操作和组合数据
  • 创建用户自定义函数
  • 将查询结果格式化为任何 JSON 输出结构

阅读 完整文档,并在 Stedi 的 JSONata 演示场 中尝试。

入门指南

API 目前不够直观,因为您需要提供 bumpalo 区域以分配值。

首先,将以下内容添加到您的 Cargo.toml

[dependencies]
jsonata-rs = "0"
bumpalo = "3.9.1"

然后您可以使用类似以下的方式评估 JSON 输入的表达式

use bumpalo::Bump;
use jsonata_rs::JsonAta;

// Create an arena for allocating values, this will go away in future except for advanced use cases
let arena = Bump::new();

// Provide some JSON input, this could be read from a file or come from the network
let input = "{ \"name\": \"world\" }";

// The JSONata expression to evaluate
let expr = "\"Hello, \" & name & \"!\"";

// Parse the expression - this could fail
let jsonata = JsonAta::new(expr, &arena).unwrap();

// Evaluate the expression against the input - this could fail
let result = jsonata.evaluate(Some(input)).unwrap();

// Serialize the result into JSON
println!("{}", result.serialize(false));

还有一个基本的 CLI 工具

# cargo install jsonata-rs

# jsonata "1 + 1"
2

# jsonata '"Hello, " & name & "!"' '{ "name": "world" }'
"Hello, world!"

表达式和输入可以在命令行中指定,但这需要手动转义。或者,它们可以来自文件。以下是 --help 的输出

# jsonata --help
jsonata-rs
A command line JSON processor using JSONata

USAGE:
    jsonata [FLAGS] [OPTIONS] [ARGS]

FLAGS:
    -a, --ast        Parse the given expression, print the AST and exit
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -e, --expr-file <expr-file>      File containing the JSONata expression to evaluate (overrides expr on command line)
    -i, --input-file <input-file>    Input JSON file (if not specified, STDIN)

ARGS:
    <expr>     JSONata expression to evaluate
    <input>    JSON input

缺少(但计划中)的功能

有几种 JSONata 功能尚未实现

  • 许多内置函数尚未实现
  • 父操作符
  • 正则表达式
  • 部分函数应用
  • 与参考实现匹配的 JSON AST 输出

与参考 JSONata 的区别

不支持函数签名

函数签名存在一些问题,如在此处所述这里,并且此实现不支持。

然而,大多数JSONata函数都支持根据它们的签名将上下文作为第一个参数传递,例如:

["Hello", "world"].$substring(1, 2)

/* Output: ["el", "or"] */

这已在每个内置函数本身中实现。例如,如果$string看到它没有参数被调用,它将使用当前上下文。

此外,对于所有内置函数,参数的类型检查也是直接在函数本身中实现的,这样你就可以为传递错误内容到这些函数获得与参考JSONata等效的运行时错误。

状态

有一个状态文档,其中描述了此实现的当前状态和长期目标。

测试

参考JSONata包含一个包含1000多个测试的广泛测试套件。目前,此实现通过了其中600多个,你可以这样运行它们:

cargo test testsuite

tests/testsuite/groups中是通过的测试组,而tests/testsuite/skip包含仍需功能实现的组。剩余的组中可能也有通过的测试,但我不想将它们分开——只有当一个测试组完全通过时,它才会被移动。

贡献

我们欢迎社区贡献和拉取请求。

许可

此项目使用Apache-2.0许可证。您提交的任何代码都将在此许可证下发布。

依赖

~4.5–6MB
~101K SLoC