GithubHelp home page GithubHelp logo

importcjj / rust-ajson Goto Github PK

View Code? Open in Web Editor NEW
103.0 4.0 10.0 294 KB

Rust port of gjson,get JSON value by dotpath syntax

Home Page: https://importcjj.github.io/rust-ajson-playground/

License: MIT License

Rust 100.00%
rust json json-parser json-rs serde-json json-path ajson dotpath

rust-ajson's Issues

bracket in array value

Hi,

There is a bug when a bracket sits in array's value.

#[test]
fn test_bracket_in_array() {
let json = r##"{
"children": ["Sara","Alex]","Jack"],
"##;
let r = parse(json).unwrap();
assert_eq!(r.get("children.#").unwrap().to_i64(), 3);
assert_eq!(r.get("children").unwrap().to_vec(), vec!["Sara", "Alex]", "Jack"]);
assert_eq!(r.get("children.1").unwrap(), "Alex]");
assert_eq!(r.get("child*.2").unwrap(), "Jack");
assert_eq!(r.get("c?ildren.0").unwrap(), "Sara");
}

---- test_bracket_in_array stdout ----
thread 'test_bracket_in_array' panicked at 'assertion failed: (left == right)
left: ["Sara", "Alex]"],
right: ["Sara", "Alex]", "Jack"]', tests/test_ajson.rs:590:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace.

Some([]) using get_from_read

Hello! I have a json that looks like this:

{
  "ids":[1, 2, 3,4, 5]
}

When I run this:

extern crate ajson;
use std::fs::File;

fn main() {
	let f = File::open("ids.json").unwrap();
	let value = ajson::get_from_read(f, "ids.#.1");
	println!("{:?}", value);
}

I simply get Some([]) as a result. Have I done anything wrong?

Thank you for this library by the way :)

performance comparison between rust-ajson with gjson.rs

I forked your benches to test the speed of ajson and gjson.rs. Below is the result::

ajson benchmark         time:   [9.1979 us 9.2489 us 9.3066 us]
                        change: [-10.706% -9.1719% -7.6992%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  2 (2.00%) high mild

gjson benchmark         time:   [3.6221 us 3.6413 us 3.6623 us]
                        change: [-15.350% -12.966% -10.873%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  3 (3.00%) high mild
  3 (3.00%) high severe

serde_json benchmark    time:   [59.616 us 59.876 us 60.162 us]
                        change: [-14.793% -12.771% -10.900%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low mild
  2 (2.00%) high mild
  6 (6.00%) high severe

json-rust benchmark     time:   [29.568 us 29.729 us 29.894 us]
                        change: [-15.512% -14.014% -12.561%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  1 (1.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

ajson selector          time:   [3.0875 us 3.1000 us 3.1139 us]
                        change: [-11.651% -10.079% -8.4826%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

gjson selector          time:   [2.0724 us 2.0882 us 2.1050 us]
                        change: [-5.7071% -3.8407% -1.9520%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high severe

ajson multi query       time:   [2.6245 us 2.6365 us 2.6492 us]
                        change: [-9.4889% -8.3135% -7.1778%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) low mild
  5 (5.00%) high mild
  3 (3.00%) high severe

gjson multi query       time:   [858.23 ns 861.61 ns 865.37 ns]
                        change: [-10.479% -8.6397% -6.8407%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe

serde derive            time:   [9.1830 us 9.2368 us 9.2964 us]
                        change: [-10.407% -8.5730% -6.7286%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  4 (4.00%) high mild
  1 (1.00%) high severe

serde derive multi query
                        time:   [1.9318 us 1.9421 us 1.9542 us]
                        change: [-5.3005% -4.0650% -2.8234%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild

nom json bench          time:   [1.2689 us 1.2735 us 1.2783 us]
                        change: [-4.7747% -3.7796% -2.8171%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  2 (2.00%) high mild
  3 (3.00%) high severe

Fork: gjson-bench

It seems that ajson is a little bit slower than gsjon.rs :)

Handle unicode character in json object keys

Thanks for writing a very useful library.

I think currently library doesn't handle unicode characters in object keys.


    let data = r#"{"sample_unicode\u0041": "abc\u0041\u0042"}"#;

    let name = ajson::parse(data).unwrap();
    dbg!(name);

    {
        dbg!(ajson::get(data, "sample_unicodeA"));

        dbg!(ajson::get(data, "sample_unicode\\u0041"));
    }

    {
        let v: Value = serde_json::from_str(data).unwrap();
        dbg!(&v["sample_unicodeA"]);
    }

Output:

[src/main.rs:10] name = {"sample_unicode\u0041": "abc\u0041\u0042"}
[src/main.rs:13] ajson::get(data, "sample_unicodeA") = None
[src/main.rs:15] ajson::get(data, "sample_unicode\\u0041") = Some(
    "abcAB",
)
[src/main.rs:20] &v["sample_unicodeA"] = String(
    "abcAB",
)

If you willing to accept PRs, I will try to fix this issue and add necessary test cases.

Crate name on Crates.io

Hi, nice work on porting gjson from Go to Rust.

I'm the creator and maintainer of the official gjson project and the gjson specification.

I noticed that you chose to use the crate name gjson in crates.io. It so happens that the official Rust version of gjson is currently in development.

I'm asking you if you could assign the ownership of the name to me as I believe the Rust community would benefit from having the gjson crate name point to the official project.
https://crates.io/policies#package-ownership

Opening this issue to raise the concern as early as possible, because changing the crate name is easier at the early stage of the project.

Multiple conditions in queries

Hello!
First of all, thanks for this project.

Could you please add conditions operators AND, OR for using multiple conditions in one query?

For example:

let json_string = r#"
{
    "customFields": [
        {
            "projectCustomField": {
                "id": "1",
                "$type": "EnumProjectCustomField"
            },
            "value": {
                "localizedName": null,
                "description": null,
                "name": "High",
                "id": "3",
                "$type": "EnumBundleElement"
            },
            "name": "Priority",
            "id": "1",
            "$type": "SingleEnumIssueCustomField"
        },
        {
            "projectCustomField": {
                "id": "2",
                "$type": "EnumProjectCustomField"
            },
            "value": {
                "localizedName": null,
                "description": null,
                "name": "Bug",
                "id": "3",
                "$type": "EnumBundleElement"
            },
            "name": "Type",
            "id": "2",
            "$type": "SingleEnumIssueCustomField"
        },
    ]
}"#;

let prioroty= ajson::get(json_string, r#"customFields.#($type == "SingleEnumIssueCustomField" && name == "Priority").value.name"#).map(|s| s.to_string());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.