1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#![forbid(unsafe_code)]
#![warn(
missing_docs,
elided_lifetimes_in_paths,
explicit_outlives_requirements,
missing_abi,
noop_method_call,
pointer_structural_match,
semicolon_in_expressions_from_macros,
unused_import_braces,
unused_lifetimes,
clippy::cargo,
clippy::missing_panics_doc,
clippy::doc_markdown,
clippy::ptr_as_ptr,
clippy::cloned_instead_of_copied,
clippy::unreadable_literal
)]
use error::{ParseError, ParseOrJsonError};
use eval::EvalCtx;
mod ast;
mod eval;
pub mod error;
pub use ast::Path as JsonPath;
fn resolve_path<'a>(path: &[eval::Idx], val: &'a mut serde_json::Value) -> &'a mut serde_json::Value {
use serde_json::Value;
pub struct CurRef<'a, T>(&'a mut T);
let mut cur = CurRef(val);
for p in path {
match cur.0 {
Value::Array(v) => cur = CurRef(&mut v[p.as_int()]),
Value::Object(m) => cur = CurRef(&mut m[p.as_string()]),
_ => unreachable!()
}
}
cur.0
}
pub fn find<'a>(pattern: &str, value: &'a serde_json::Value) -> Result<Vec<&'a serde_json::Value>, ParseError> {
Ok(JsonPath::compile(pattern)?.find(value))
}
pub fn find_str(pattern: &str, value: &str) -> Result<Vec<serde_json::Value>, ParseOrJsonError> {
Ok(JsonPath::compile(pattern)?.find_str(value)?)
}
impl JsonPath {
pub fn compile(pattern: &str) -> Result<JsonPath, ParseError> {
use chumsky::Parser;
Self::parser()
.parse(pattern)
.map_err(|e| ParseError::new(pattern, e))
}
pub fn find<'a>(&self, value: &'a serde_json::Value) -> Vec<&'a serde_json::Value> {
let mut ctx = EvalCtx::new(value);
self.eval(&mut ctx);
ctx.into_matched()
}
pub fn delete(&self, value: &serde_json::Value) -> serde_json::Value {
use serde_json::Value;
let mut ctx = EvalCtx::new(value);
self.eval(&mut ctx);
let paths: Vec<_> = ctx.paths_matched();
let mut out = value.clone();
for p in paths {
let delete_on = resolve_path(&p[..p.len() - 1], &mut out);
let last_idx = p.last().expect("Idx should match found item");
match delete_on {
Value::Array(v) => {
v.remove(last_idx.as_int());
}
Value::Object(m) => {
m.remove(last_idx.as_string());
}
_ => unreachable!(),
}
}
out
}
pub fn replace(&self, value: &serde_json::Value, mut f: impl FnMut(&serde_json::Value) -> serde_json::Value) -> serde_json::Value {
use serde_json::Value;
let mut ctx = EvalCtx::new(value);
self.eval(&mut ctx);
let paths: Vec<_> = ctx.paths_matched();
let mut out = value.clone();
for p in paths {
let replace_on = resolve_path(&p[..p.len() - 1], &mut out);
let last_idx = p.last().unwrap();
match replace_on {
Value::Array(v) => {
let last_idx = last_idx.as_int();
let new = f(&v[last_idx]);
v[last_idx] = new;
}
Value::Object(m) => {
let last_idx = last_idx.as_string();
let new = f(&m[last_idx]);
m[last_idx] = new;
}
_ => unreachable!(),
}
}
out
}
pub fn find_str(&self, str: &str) -> Result<Vec<serde_json::Value>, serde_json::Error> {
let val = serde_json::from_str(str)?;
Ok(self.find(&val).into_iter().cloned().collect())
}
pub fn delete_str(&self, str: &str) -> Result<serde_json::Value, serde_json::Error> {
let val = serde_json::from_str(str)?;
Ok(self.delete(&val))
}
pub fn replace_str(&self, str: &str, f: impl FnMut(&serde_json::Value) -> serde_json::Value) -> Result<serde_json::Value, serde_json::Error> {
let val = serde_json::from_str(str)?;
Ok(self.replace(&val, f))
}
}
#[cfg(test)]
mod tests;