access-json 0.1.0

Use serde to query large nested structures in Rust. For low-effort, read-only FFI.
Documentation
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! # Query a subset of your Serde-Serializable Data
//!
//! This library allows you to execute a simple path-query against any serde-serializable object; returning the result as a ``Option<serde_json::Value>``.
//!
//! ```
//! use access_json::JSONQuery;
//! use std::collections::HashMap;
//! use serde_json;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut data: HashMap<&str, u32> = HashMap::default();
//! data.insert("cat", 9);
//!
//! let query = JSONQuery::parse(".cat")?; // QueryParseErr
//! let output = query.execute(&data)?; // QueryExecErr
//! let expected = serde_json::to_value(&9)?; // You must derive Serialize!
//!
//! assert_eq!(Some(expected), output);
//! # Ok(())
//! # }
//! ```
//!
//! ## A More Complex, Nested Example
//!
//!
//! ```
//! use access_json::JSONQuery;
//! use serde_json::{self, Value};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let data: Value = serde_json::from_str(r#"{
//!    "items": [
//!       {
//!          "unwanted": 7,
//!          "wanted": {"x": 3, "y": 7},
//!          "array": [3,2,1]
//!       },
//!       {
//!          "whatever": true
//!       }
//!    ]
//! }"#)?;
//!
//! // We can reference dictionary fields and array indices together:
//! let output = JSONQuery::parse(".items[1].whatever")?.execute(&data)?;
//! let expected = serde_json::to_value(&true)?;
//! assert_eq!(Some(expected), output);
//!
//! // We can have results be of any-size sub-tree, e.g., a whole array or vec.
//! let output = JSONQuery::parse(".items[0].array")?.execute(&data)?;
//! let expected = serde_json::to_value(&vec![3,2,1])?;
//! assert_eq!(Some(expected), output);
//! # Ok(())
//! # }
//! ```
//!
//! ## Just ``#[derive(Serialize)]`` to query any struct or enum:
//!
//! ```
//! use access_json::JSONQuery;
//! #[macro_use]
//! extern crate serde_derive;
//!
//! #[derive(Serialize)]
//! struct Dog {
//!    name: String,
//!    age: i32,
//!    favorites: Vec<String>,
//! }
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let data = Dog {
//!     name: "Buddy".into(),
//!     age: 14,
//!     favorites: vec!["walks".into(), "naps".into()],
//! };
//!
//! let found = JSONQuery::parse(".name")?.execute(&data)?.unwrap();
//! assert_eq!("Buddy", found);
//! # Ok(())
//! # }
//! ```
//!

#[macro_use]
extern crate serde_derive;

pub use erased_serde::Serialize as AnySerializable;

pub mod query;
pub mod query_executor;
pub mod query_parser;

#[doc(inline)]
pub use query::JSONQuery;
#[doc(inline)]
pub use query_executor::QueryExecErr;
#[doc(inline)]
pub use query_parser::QueryParseErr;

#[cfg(test)]
mod tests {
    use super::query::*;
    use serde_json::Value as JV;
    use std::collections::HashMap;

    #[test]
    fn test_query_hashmap() {
        let mut data: HashMap<&str, usize> = HashMap::default();
        data.insert("hello", 7);
        data.insert("world", 5);
        let world_q = JSONQuery::single(QueryElement::field("world"));
        let found = world_q.execute(&data).unwrap();
        assert_eq!(found, Some(JV::Number(5.into())));

        let hello_q = JSONQuery::single(QueryElement::field("hello"));
        let found = hello_q.execute(&data).unwrap();
        assert_eq!(found, Some(JV::Number(7.into())));
    }

    #[test]
    fn test_query_vec() {
        let data = vec![0, 1, 2, 3, 4, 5];

        for i in 0..data.len() {
            let elem_q = JSONQuery::single(QueryElement::array_item(i));
            let found = elem_q.execute(&data).unwrap().unwrap();
            assert_eq!(found, (JV::Number(i.into())));
        }

        let missing_q = JSONQuery::single(QueryElement::array_item(17));
        let found = missing_q.execute(&data).unwrap();
        assert_eq!(None, found);
    }

    #[test]
    fn test_tuple() {
        let point = (17, 39);

        let first_q = JSONQuery::single(QueryElement::array_item(0));
        let found = first_q.execute(&point).unwrap().unwrap();
        assert_eq!(found, (JV::Number(17.into())));

        let second_q = JSONQuery::single(QueryElement::array_item(1));
        let found = second_q.execute(&point).unwrap().unwrap();

        assert_eq!(found, (JV::Number(39.into())));
        let missing_q = JSONQuery::single(QueryElement::array_item(3));
        let found = missing_q.execute(&point).unwrap();
        assert_eq!(None, found);
    }

    #[derive(PartialEq, Eq, Clone, Serialize)]
    struct Example {
        name: String,
        age: i32,
        favorites: Vec<String>,
    }

    #[test]
    fn test_example_struct() {
        let data = Example {
            name: "Buddy".into(),
            age: 14,
            favorites: vec!["walks".into(), "naps".into()],
        };

        let name_q = JSONQuery::single(QueryElement::field("name"));
        assert_eq!("Buddy", name_q.execute(&data).unwrap().unwrap());
        let age_q = JSONQuery::single(QueryElement::field("age"));
        assert_eq!(14, age_q.execute(&data).unwrap().unwrap());

        let first_favorite_q = JSONQuery::new(vec![
            QueryElement::field("favorites"),
            QueryElement::array_item(0),
        ]);
        assert_eq!("walks", first_favorite_q.execute(&data).unwrap().unwrap());
    }

    #[test]
    fn test_whole_object_results() {
        let data = Example {
            name: "Buddy".into(),
            age: 14,
            favorites: vec!["walks".into(), "naps".into()],
        };

        let all_favorites = JSONQuery::single(QueryElement::field("favorites"));
        let expected: Vec<JV> = vec!["walks".into(), "naps".into()];
        assert_eq!(
            Some(&expected),
            all_favorites.execute(&data).unwrap().unwrap().as_array()
        );
    }

    #[derive(Serialize)]
    struct NestedStructs {
        dog: Example,
        truthiness: bool,
        score: i32,
    }

    #[test]
    fn test_nested_structs() {
        let data = NestedStructs {
            dog: Example {
                name: "Buddy".into(),
                age: 14,
                favorites: vec!["walks".into(), "naps".into()],
            },
            truthiness: false,
            score: -77,
        };

        assert_eq!(
            JSONQuery::parse(".dog.name")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap(),
            "Buddy"
        );
        assert_eq!(
            JSONQuery::parse(".truthiness")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap(),
            false
        )
    }

    #[test]
    fn test_vec_structs() {
        let data: Vec<Example> = vec![
            Example {
                name: "Buddy".into(),
                age: 14,
                favorites: vec![],
            },
            Example {
                name: "Tuukka".into(),
                age: 6,
                favorites: vec![],
            },
        ];
        assert_eq!(
            JSONQuery::parse("[0].name")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap(),
            "Buddy"
        );
        assert_eq!(
            JSONQuery::parse("[1].name")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap(),
            "Tuukka"
        )
    }

    #[derive(Serialize)]
    enum Pet {
        Bird,
        Dog(Example),
        Cat { lives: u32 },
        Digits(u32, u32, u32),
    }

    #[test]
    fn test_enum_examples() {
        let buddy = Example {
            name: "Buddy".into(),
            age: 14,
            favorites: vec!["walks".into(), "naps".into()],
        };
        let data = vec![
            Pet::Bird,
            Pet::Dog(buddy.clone()),
            Pet::Cat { lives: 9 },
            Pet::Digits(7, 5, 6),
        ];
        // For debugging:
        //println!("json: {}", serde_json::to_string_pretty(&data).unwrap());

        assert_eq!(
            "Bird",
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            14,
            JSONQuery::parse("[1].Dog.age")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            serde_json::to_value(buddy).unwrap(),
            JSONQuery::parse("[1].Dog")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            "Bird",
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            9,
            JSONQuery::parse("[2].Cat.lives")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            serde_json::to_value(vec![7, 5, 6]).unwrap(),
            JSONQuery::parse("[3].Digits")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
    }

    // tuple-struct
    #[derive(Serialize)]
    struct Point(u32, u32);

    #[test]
    fn test_tuple_struct() {
        let data = vec![Point(1, 2), Point(3, 4)];
        assert_eq!(
            serde_json::to_value(vec![1, 2]).unwrap(),
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            4,
            JSONQuery::parse("[1][1]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
    }

    #[test]
    fn test_list_of_lists() {
        let data = vec![
            vec![vec![1, 2, 3], vec![4, 5, 6]],
            vec![vec![7, 8, 9], vec![10, 11, 12]],
        ];

        assert_eq!(
            serde_json::to_value(&vec![vec![1, 2, 3], vec![4, 5, 6]]).unwrap(),
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        )
    }

    #[derive(Debug, PartialEq, Eq, Serialize)]
    struct UnitStruct;

    #[test]
    fn test_unit_struct() {
        let nothings = vec![UnitStruct, UnitStruct];
        assert_eq!(
            serde_json::to_value(&UnitStruct).unwrap(),
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&nothings)
                .unwrap()
                .unwrap()
        )
    }

    #[derive(Debug, PartialEq, Eq, Serialize)]
    struct NewType(i32);

    #[test]
    fn test_newtype_struct() {
        let data = vec![NewType(-2), NewType(3)];
        assert_eq!(
            -2,
            JSONQuery::parse("[0]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        );
        assert_eq!(
            3,
            JSONQuery::parse("[1]")
                .unwrap()
                .execute(&data)
                .unwrap()
                .unwrap()
        )
    }
}