cloudwego / sonic-rs

A fast Rust JSON library based on SIMD.
https://crates.io/crates/sonic-rs
Apache License 2.0
403 stars 28 forks source link

Placeholders in a path #79

Open Adam-Vandervorst opened 4 months ago

Adam-Vandervorst commented 4 months ago

Is your feature request related to a problem? Please describe.

If you want to project out a simple structure out of a large JSON you have two options: a) Formalize the large JSON scheme, parse that, and post-process it b) Get the parts you need with using paths

However, a) is expensive for the programmer, and b) is quite limited and computationally expensive if you want to get around that

Describe the solution you'd like

let vs = sonic_rs::get(line, sonic_rs::pointer!["aliases", "en", Index, "value"])

and

let o = sonic_rs::get(line, sonic_rs::pointer!["aliases", Key, 0, "value"])

Describe alternatives you've considered

for i in 0..MAX_ARRAY_SIZE { 
  match sonic_rs::get(line, sonic_rs::pointer!["aliases", "en", i, "value"]) {
    Ok(r) => { vs.push_back(r) }
    Err(_) => { break }
  }
}

and

for lang in languages { 
  match sonic_rs::get(line, sonic_rs::pointer!["aliases", lang, 0, "value"]) {
    Ok(r) => { o.insert(lang, r) }
    Err(_) => { continue }
  }
}

Additional context

Think about it as using a well-typed Glob for your JSON. Notably, the workaround gets quadratically worse when you have more placeholders in your path.

liuq19 commented 4 months ago

Yes, pointer! has limitations when it comes to retrieving multiple keys from a large JSON.

If we want to perform quick and flexible searches on large JSON data as above, it is advisable to support the JSONPath RFC. While it is included in the roadmap, it may not be completed shortly.

There is an existing high-performance JSONPath library available at https://github.com/rsonquery/rsonpath that can be used for your purpose.