sonic-rs icon indicating copy to clipboard operation
sonic-rs copied to clipboard

Placeholders in a path

Open Adam-Vandervorst opened this issue 1 year ago • 1 comments

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.

Adam-Vandervorst avatar Apr 26 '24 17:04 Adam-Vandervorst

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.

liuq19 avatar Apr 29 '24 04:04 liuq19