16 个版本 (5 个重大更新)

0.5.0 2024 年 6 月 24 日
0.4.1 2024 年 5 月 31 日
0.1.1 2024 年 3 月 25 日
0.0.5 2023 年 11 月 27 日

#114 in 解析器实现

Download history 6497/week @ 2024-05-04 7276/week @ 2024-05-11 6279/week @ 2024-05-18 5737/week @ 2024-05-25 6672/week @ 2024-06-01 6907/week @ 2024-06-08 6613/week @ 2024-06-15 8224/week @ 2024-06-22 5798/week @ 2024-06-29 6167/week @ 2024-07-06 8171/week @ 2024-07-13 8957/week @ 2024-07-20 9094/week @ 2024-07-27 8439/week @ 2024-08-03 8966/week @ 2024-08-10 9794/week @ 2024-08-17

每月下载量 37,541
用于 5 个 crate (直接使用 2 个)

MIT 许可证

145KB
3K SLoC

jiter

CI Crates.io CodSpeed Badge

快速可迭代 JSON 解析器。

文档可在 docs.rs/jiter 查找。

jiter 有三个接口

JsonValue 示例

有关更多详细信息,请参阅 [《JsonValue 文档》][JsonValue]。

use jiter::JsonValue;

let json_data = r#"
    {
        "name": "John Doe",
        "age": 43,
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    }"#;
let json_value = JsonValue::parse(json_data.as_bytes(), true).unwrap();
println!("{:#?}", json_value);

返回

Object(
    {
        "name": Str("John Doe"),
        "age": Int(43),
        "phones": Array(
            [
                Str("+44 1234567"),
                Str("+44 2345678"),
            ],
        ),
    },
)

jiter 示例

要使用 [Jiter],您需要知道您期望的架构

use jiter::{Jiter, NumberInt, Peek};

let json_data = r#"
    {
        "name": "John Doe",
        "age": 43,
        "phones": [
            "+44 1234567",
            "+44 2345678"
        ]
    }"#;
let mut jiter = Jiter::new(json_data.as_bytes()).with_allow_inf_nan();
assert_eq!(jiter.next_object().unwrap(), Some("name"));
assert_eq!(jiter.next_str().unwrap(), "John Doe");
assert_eq!(jiter.next_key().unwrap(), Some("age"));
assert_eq!(jiter.next_int().unwrap(), NumberInt::Int(43));
assert_eq!(jiter.next_key().unwrap(), Some("phones"));
assert_eq!(jiter.next_array().unwrap(), Some(Peek::String));
// we know the next value is a string as we just asserted so
assert_eq!(jiter.known_str().unwrap(), "+44 1234567");
assert_eq!(jiter.array_step().unwrap(), Some(Peek::String));
// same again
assert_eq!(jiter.known_str().unwrap(), "+44 2345678");
// next we'll get `None` from `array_step` as the array is finished
assert_eq!(jiter.array_step().unwrap(), None);
// and `None` from `next_key` as the object is finished
assert_eq!(jiter.next_key().unwrap(), None);
// and we check there's nothing else in the input
jiter.finish().unwrap();

基准测试

有谎言、该死的谎言和基准测试。

特别是,serde-json 基准测试使用 serde_json::Value,这比将数据反序列化为字符串要慢得多。

有关更多详细信息,请参阅 基准测试

running 48 tests
test big_jiter_iter                    ... bench:   3,662,616 ns/iter (+/- 88,878)
test big_jiter_value                   ... bench:   6,998,605 ns/iter (+/- 292,383)
test big_serde_value                   ... bench:  29,793,191 ns/iter (+/- 576,173)
test bigints_array_jiter_iter          ... bench:      11,836 ns/iter (+/- 414)
test bigints_array_jiter_value         ... bench:      28,979 ns/iter (+/- 938)
test bigints_array_serde_value         ... bench:     129,797 ns/iter (+/- 5,096)
test floats_array_jiter_iter           ... bench:      19,302 ns/iter (+/- 631)
test floats_array_jiter_value          ... bench:      31,083 ns/iter (+/- 921)
test floats_array_serde_value          ... bench:     208,932 ns/iter (+/- 6,167)
test lazy_map_lookup_1_10              ... bench:         615 ns/iter (+/- 15)
test lazy_map_lookup_2_20              ... bench:       1,776 ns/iter (+/- 36)
test lazy_map_lookup_3_50              ... bench:       4,291 ns/iter (+/- 77)
test massive_ints_array_jiter_iter     ... bench:      62,244 ns/iter (+/- 1,616)
test massive_ints_array_jiter_value    ... bench:      82,889 ns/iter (+/- 1,916)
test massive_ints_array_serde_value    ... bench:     498,650 ns/iter (+/- 47,759)
test medium_response_jiter_iter        ... bench:           0 ns/iter (+/- 0)
test medium_response_jiter_value       ... bench:       3,521 ns/iter (+/- 101)
test medium_response_jiter_value_owned ... bench:       6,088 ns/iter (+/- 180)
test medium_response_serde_value       ... bench:       9,383 ns/iter (+/- 342)
test pass1_jiter_iter                  ... bench:           0 ns/iter (+/- 0)
test pass1_jiter_value                 ... bench:       3,048 ns/iter (+/- 79)
test pass1_serde_value                 ... bench:       6,588 ns/iter (+/- 232)
test pass2_jiter_iter                  ... bench:         384 ns/iter (+/- 9)
test pass2_jiter_value                 ... bench:       1,259 ns/iter (+/- 44)
test pass2_serde_value                 ... bench:       1,237 ns/iter (+/- 38)
test sentence_jiter_iter               ... bench:         283 ns/iter (+/- 10)
test sentence_jiter_value              ... bench:         357 ns/iter (+/- 15)
test sentence_serde_value              ... bench:         428 ns/iter (+/- 9)
test short_numbers_jiter_iter          ... bench:           0 ns/iter (+/- 0)
test short_numbers_jiter_value         ... bench:      18,085 ns/iter (+/- 613)
test short_numbers_serde_value         ... bench:      87,253 ns/iter (+/- 1,506)
test string_array_jiter_iter           ... bench:         615 ns/iter (+/- 18)
test string_array_jiter_value          ... bench:       1,410 ns/iter (+/- 44)
test string_array_jiter_value_owned    ... bench:       2,863 ns/iter (+/- 151)
test string_array_serde_value          ... bench:       3,467 ns/iter (+/- 60)
test true_array_jiter_iter             ... bench:         299 ns/iter (+/- 8)
test true_array_jiter_value            ... bench:         995 ns/iter (+/- 29)
test true_array_serde_value            ... bench:       1,207 ns/iter (+/- 36)
test true_object_jiter_iter            ... bench:       2,482 ns/iter (+/- 84)
test true_object_jiter_value           ... bench:       2,058 ns/iter (+/- 45)
test true_object_serde_value           ... bench:       7,991 ns/iter (+/- 370)
test unicode_jiter_iter                ... bench:         315 ns/iter (+/- 7)
test unicode_jiter_value               ... bench:         389 ns/iter (+/- 6)
test unicode_serde_value               ... bench:         445 ns/iter (+/- 6)
test x100_jiter_iter                   ... bench:          12 ns/iter (+/- 0)
test x100_jiter_value                  ... bench:          20 ns/iter (+/- 1)
test x100_serde_iter                   ... bench:          72 ns/iter (+/- 3)
test x100_serde_value                  ... bench:          83 ns/iter (+/- 3)

依赖项

~3–4MB
~79K SLoC