jsonpath/README.md

488 lines
12 KiB
Markdown
Raw Normal View History

# jsonpath_lib
2018-12-26 14:45:31 +09:00
2019-03-06 23:50:10 +09:00
[![Build Status](https://travis-ci.org/freestrings/jsonpath.svg?branch=master)](https://travis-ci.org/freestrings/jsonpath)
2019-03-11 17:35:15 +09:00
![crates.io](https://img.shields.io/crates/v/jsonpath_lib.svg)
2019-03-27 09:37:50 +09:00
![Crates.io](https://img.shields.io/crates/d/jsonpath_lib.svg?label=%60jsonpath_lib%60%20crates.io%20downloads)
![npm](https://img.shields.io/npm/dt/jsonpath-rs.svg?label=%60jsonpath-rs%60%20npm%20downloads)
2019-03-06 23:50:10 +09:00
2019-03-26 17:59:42 +09:00
`Rust` 버전 [JsonPath](https://goessner.net/articles/JsonPath/) 구현이다. `Webassembly``Javascript`에서도 역시 동일한 API 인터페이스를 제공 한다.
2019-03-06 23:50:10 +09:00
2019-03-26 17:59:42 +09:00
It is an implementation for [JsonPath](https://goessner.net/articles/JsonPath/) written in `Rust`. it provide the same API interface in `Webassembly` and` Javascript` also.
2018-12-26 14:45:31 +09:00
2019-03-23 11:51:35 +09:00
- [Webassembly Demo](https://freestrings.github.io/jsonpath/)
- [Rust documentation](https://docs.rs/jsonpath_lib/0.1.6/jsonpath_lib)
2019-03-17 18:27:47 +09:00
2019-04-04 09:37:44 +09:00
## Why?
2018-12-26 14:45:31 +09:00
2019-03-06 23:50:10 +09:00
To enjoy Rust!
2019-04-04 09:37:44 +09:00
## API
2019-03-06 23:50:10 +09:00
2019-03-17 18:25:00 +09:00
[With Javascript](#with-javascript)
2019-03-06 23:50:10 +09:00
- [jsonpath-wasm library](#jsonpath-wasm-library)
2019-03-17 18:25:00 +09:00
- [jsonpath-rs library](#jsonpath-rs-library-only-nodejs)
2019-03-11 17:35:15 +09:00
- [javascript - jsonpath.select(json: string|object, jsonpath: string)](#javascript---jsonpathselectjson-stringobject-jsonpath-string)
2019-03-06 23:50:10 +09:00
- [javascript - jsonpath.compile(jsonpath: string)](#javascript---jsonpathcompilejsonpath-string)
2019-03-11 17:35:15 +09:00
- [javascript - jsonpath.selector(json: string|object)](#javascript---jsonpathselectorjson-stringobject)
- [javascript - alloc_json, dealloc_json](#javascript---alloc_json-dealloc_json)
2019-04-04 09:37:44 +09:00
- [javascript-wasm - examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples)
2019-03-06 23:50:10 +09:00
[With Rust (as library)](#with-rust-as-library)
- [jsonpath_lib library](#jsonpath_lib-library)
2019-04-08 14:44:18 +09:00
- [rust - jsonpath::Selector struct](#rust---jsonpathselector-struct)
2019-03-17 18:25:00 +09:00
- [rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str)](#rust---jsonpathselectjson-serde_jsonvaluevalue-jsonpath-str)
2019-03-24 21:18:58 +09:00
- [rust - jsonpath::select_as_str(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_as_strjson-str-jsonpath-str)
2019-04-04 09:37:44 +09:00
- [rust - jsonpath::select_as\<T: `serde::de::DeserializeOwned`\>(json_str: &str, jsonpath: &str)](#rust---jsonpathselect_ast-serdededeserializeownedjson-str-jsonpath-str)
2019-03-06 23:50:10 +09:00
- [rust - jsonpath::compile(jsonpath: &str)](#rust---jsonpathcompilejsonpath-str)
2019-03-17 18:25:00 +09:00
- [rust - jsonpath::selector(json: &serde_json::value::Value)](#rust---jsonpathselectorjson-serde_jsonvaluevalue)
2019-04-04 09:37:44 +09:00
- [rust - jsonpath::selector_as\<T: `serde::de::DeserializeOwned`\>(json: &serde_json::value::Value)](#rust---jsonpathselector_ast-serdededeserializeownedjson-serde_jsonvaluevalue)
2019-03-17 18:25:00 +09:00
- [rust - examples](https://github.com/freestrings/jsonpath/wiki/rust-examples)
2019-03-06 23:50:10 +09:00
2019-03-17 18:25:00 +09:00
[Simple time check - webassembly](https://github.com/freestrings/jsonpath/wiki/Simple-timecheck---jsonpath-wasm)
2019-03-17 18:25:00 +09:00
[Simple time check - native addon for NodeJs](https://github.com/freestrings/jsonpath/wiki/Simple-timecheck-jsonpath-native)
2018-12-26 14:45:31 +09:00
2019-03-17 18:25:00 +09:00
## With Javascript
2018-12-26 14:45:31 +09:00
2019-03-06 23:50:10 +09:00
### jsonpath-wasm library
2019-03-04 23:29:09 +09:00
2019-03-05 18:43:58 +09:00
*(not yet published `jsonpath-wasm`)*
2019-03-04 23:29:09 +09:00
```javascript
2019-03-06 18:55:21 +09:00
// browser
2019-03-04 23:29:09 +09:00
import * as jsonpath from "jsonpath-wasm";
2019-03-17 18:25:00 +09:00
// NodeJs
const jsonpath = require('jsonpath-wasm');
```
### jsonpath-rs library (Only NodeJS)
`jsonpath-rs` is native addon for NodeJs
```javascript
const jsonpath = require('jsonpath-rs');
2019-03-05 14:33:33 +09:00
```
2019-03-11 17:35:15 +09:00
### javascript - jsonpath.select(json: string|object, jsonpath: string)
2019-03-05 14:33:33 +09:00
2019-03-06 18:55:21 +09:00
```javascript
let jsonObj = {
2019-04-05 11:52:20 +09:00
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
2019-03-06 18:55:21 +09:00
};
2019-03-05 14:33:33 +09:00
2019-04-05 11:52:20 +09:00
let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let selectAsString = jsonpath.select(JSON.stringify(jsonObj), '$..friends[0]');
let selectAsObj = jsonpath.select(jsonObj, '$..friends[0]');
2019-03-06 18:55:21 +09:00
console.log(
2019-04-05 11:52:20 +09:00
JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(ret) == JSON.stringify(selectAsObj)
2019-03-06 18:55:21 +09:00
);
2019-04-05 11:52:20 +09:00
// => true, true
2019-03-06 18:55:21 +09:00
```
2019-03-05 14:33:33 +09:00
2019-03-06 23:50:10 +09:00
### javascript - jsonpath.compile(jsonpath: string)
2019-03-05 14:33:33 +09:00
2019-03-06 18:55:21 +09:00
```javascript
2019-04-05 11:52:20 +09:00
let template = jsonpath.compile('$..friends[0]');
2019-03-04 23:29:09 +09:00
2019-03-06 18:55:21 +09:00
let jsonObj = {
"school": {
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
2019-03-06 18:55:21 +09:00
};
2019-03-04 23:29:09 +09:00
2019-04-05 11:52:20 +09:00
let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
2019-03-06 18:55:21 +09:00
2019-04-05 11:52:20 +09:00
let selectAsString = template(JSON.stringify(jsonObj));
let selectAsObj = template(jsonObj);
console.log(
JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(ret) == JSON.stringify(selectAsObj)
);
// => true, true
2019-03-06 18:55:21 +09:00
let jsonObj2 = {
"school": {
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "Millicent Norman"},
{"name": "Vincent Cannon"}
2019-03-06 23:50:10 +09:00
]
2019-03-06 18:55:21 +09:00
},
2019-04-05 11:52:20 +09:00
"friends": [ {"age": 30}, {"age": 40} ]
2019-03-06 18:55:21 +09:00
};
2019-04-05 11:52:20 +09:00
let ret2 = [
{"age": 30},
{"name": "Millicent Norman"}
];
let selectAsString2 = template(JSON.stringify(jsonObj2));
let selectAsObj2 = template(jsonObj2);
2019-03-06 18:55:21 +09:00
2019-04-05 11:52:20 +09:00
console.log(
JSON.stringify(ret2) == JSON.stringify(selectAsString2),
JSON.stringify(ret2) == JSON.stringify(selectAsObj2)
);
// => true, true
2019-03-05 14:33:33 +09:00
```
2019-04-04 09:37:44 +09:00
2019-03-11 17:35:15 +09:00
### javascript - jsonpath.selector(json: string|object)
2019-04-04 09:37:44 +09:00
2019-03-06 18:55:21 +09:00
```javascript
let jsonObj = {
"school": {
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
2019-03-06 18:55:21 +09:00
};
2019-03-04 23:29:09 +09:00
2019-04-05 11:52:20 +09:00
let ret1 = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let ret2 = [
{"name": "친구4"},
{"name": "친구2", "age": 20}
];
2019-03-06 23:50:10 +09:00
2019-03-11 17:35:15 +09:00
let selector = jsonpath.selector(jsonObj);
2019-04-05 11:52:20 +09:00
// or as json string
// let selector = jsonpath.selector(JSON.stringify(jsonObj));
let select1 = selector('$..friends[0]');
let select2 = selector('$..friends[1]');
console.log(
JSON.stringify(ret1) == JSON.stringify(select1),
JSON.stringify(ret2) == JSON.stringify(select2)
);
2019-03-06 18:55:21 +09:00
2019-04-05 11:52:20 +09:00
// => true, true
2019-03-11 17:35:15 +09:00
```
### javascript - alloc_json, dealloc_json
2019-04-04 09:37:44 +09:00
*(not supported in `jsonpath-rs`)*
2019-03-17 18:25:00 +09:00
2019-03-11 17:35:15 +09:00
wasm-bindgen은 Javascript와 Webassembly 간 값을 주고받을 때 JSON 객체는 String으로 변환되기 때문에, 반복해서 사용되는 JSON 객체를 Webassembly 영역에 생성해 두면 성능에 도움이 된다.
Since wasm-bindgen converts JSON objects to String when exchanging values between Javascript and Webassembly, it is helpful to create repeated Json objects in Webassembly area.
```javascript
2019-04-05 11:52:20 +09:00
const jsonpath = require('@nodejs/jsonpath-wasm');
2019-03-11 17:35:15 +09:00
let jsonObj = {
"school": {
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-11 17:35:15 +09:00
},
2019-04-05 11:52:20 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
2019-03-11 17:35:15 +09:00
};
2019-04-05 11:52:20 +09:00
// allocate jsonObj in webassembly
let ptr = jsonpath.alloc_json(jsonObj);
// `0` is invalid pointer
if(ptr == 0) {
console.error('invalid ptr');
}
2019-03-11 17:35:15 +09:00
let path = '$..friends[0]';
let template = jsonpath.compile(path);
let selector = jsonpath.selector(jsonObj);
2019-04-05 11:52:20 +09:00
// create selector as pointer
let ptrSelector = jsonpath.selector(ptr);
2019-03-11 17:35:15 +09:00
let ret1 = selector(path)
2019-04-05 11:52:20 +09:00
let ret2 = ptrSelector(path)
2019-03-11 17:35:15 +09:00
let ret3 = template(jsonObj);
2019-04-05 11:52:20 +09:00
// select as pointer
2019-03-11 17:35:15 +09:00
let ret4 = template(ptr);
let ret5 = jsonpath.select(jsonObj, path);
2019-04-05 11:52:20 +09:00
// select as pointer
2019-03-11 17:35:15 +09:00
let ret6 = jsonpath.select(ptr, path);
console.log(
2019-04-05 11:52:20 +09:00
JSON.stringify(ret1) == JSON.stringify(ret2),
JSON.stringify(ret1) == JSON.stringify(ret3),
JSON.stringify(ret1) == JSON.stringify(ret4),
JSON.stringify(ret1) == JSON.stringify(ret5),
JSON.stringify(ret1) == JSON.stringify(ret6));
// => true true true true true
2019-03-11 17:35:15 +09:00
jsonpath.dealloc_json(ptr);
2019-03-05 14:33:33 +09:00
```
2019-03-04 23:29:09 +09:00
2019-03-05 23:48:27 +09:00
## With Rust (as library)
2018-12-26 14:45:31 +09:00
2019-03-06 23:50:10 +09:00
### jsonpath_lib library
2019-03-06 18:55:21 +09:00
```rust
extern crate jsonpath_lib as jsonpath;
#[macro_use]
extern crate serde_json;
```
2019-04-08 14:44:18 +09:00
### rust - jsonpath::Selector struct
```rust
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Friend {
name: String,
age: Option<u8>,
}
let json_obj = json!({
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]});
let mut selector = Selector::new();
let result = selector
.path("$..[?(@.age >= 30)]").unwrap()
// .value_from_str(&serde_json::to_string(&json_obj).unwrap() /*&str*/).unwrap()
// .value_from(&json_obj /*&impl serde::ser::Serialize*/).unwrap()
.value((&json_obj /*serde_json::value::Value*/ ).into()).unwrap()
.select_to_value().unwrap();
assert_eq!(json!([{"name": "친구3", "age": 30}]), result);
let result = selector.select_to_str().unwrap();
assert_eq!(r#"[{"name":"친구3","age":30}]"#, result);
let result = selector.select_to::<Vec<Friend>>().unwrap();
assert_eq!(vec![Friend { name: "친구3".to_string(), age: Some(30) }], result);
```
### rust - jsonpath::select(json: &serde_json::value::Value, jsonpath: &str)
2019-03-06 18:55:21 +09:00
```rust
let json_obj = json!({
"school": {
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]});
let json = jsonpath::select(&json_obj, "$..friends[0]").unwrap();
let ret = json!([
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
]);
assert_eq!(json, ret);
2019-03-06 18:55:21 +09:00
```
2019-03-24 21:18:58 +09:00
### rust - jsonpath::select_as_str(json: &str, jsonpath: &str)
```rust
2019-04-04 09:37:44 +09:00
let ret = jsonpath::select_as_str(r#"
{
"school": {
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}
"#, "$..friends[0]").unwrap();
assert_eq!(ret, r#"[{"name":"친구3","age":30},{"name":"친구1","age":20}]"#);
```
2019-04-04 09:37:44 +09:00
### rust - jsonpath::select_as\<T: `serde::de::DeserializeOwned`\>(json: &str, jsonpath: &str)
2019-03-24 21:18:58 +09:00
```rust
2019-04-04 09:37:44 +09:00
#[derive(Deserialize, PartialEq, Debug)]
2019-03-24 21:18:58 +09:00
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
2019-04-04 09:37:44 +09:00
2019-03-24 21:18:58 +09:00
let ret: Person = jsonpath::select_as(r#"
{
"person":
{
"name": "Doe John",
"age": 44,
"phones": [
"+44 1234567",
"+44 2345678"
]
}
}
"#, "$.person").unwrap();
2019-04-04 09:37:44 +09:00
2019-03-24 21:18:58 +09:00
let person = Person {
name: "Doe John".to_string(),
age: 44,
phones: vec!["+44 1234567".to_string(), "+44 2345678".to_string()],
};
2019-04-04 09:37:44 +09:00
2019-03-24 21:18:58 +09:00
assert_eq!(person, ret);
```
2019-03-06 23:50:10 +09:00
### rust - jsonpath::compile(jsonpath: &str)
2019-03-06 18:55:21 +09:00
```rust
let mut template = jsonpath::compile("$..friends[0]");
let json_obj = json!({
"school": {
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]});
2019-03-06 18:55:21 +09:00
2019-03-17 18:25:00 +09:00
let json = template(&json_obj).unwrap();
2019-04-04 09:37:44 +09:00
let ret = json!([
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
]);
2019-03-06 18:55:21 +09:00
assert_eq!(json, ret);
2019-04-04 09:37:44 +09:00
```
### rust - jsonpath::selector(json: &serde_json::value::Value)
2019-03-06 18:55:21 +09:00
2019-04-04 09:37:44 +09:00
```rust
2019-03-06 18:55:21 +09:00
let json_obj = json!({
"school": {
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]});
let mut selector = jsonpath::selector(&json_obj);
let json = selector("$..friends[0]").unwrap();
let ret = json!([
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
]);
assert_eq!(json, ret);
let json = selector("$..friends[1]").unwrap();
let ret = json!([
{"name": "친구4"},
{"name": "친구2", "age": 20}
]);
2019-03-06 18:55:21 +09:00
assert_eq!(json, ret);
```
2019-04-04 09:37:44 +09:00
### rust - jsonpath::selector_as\<T: `serde::de::DeserializeOwned`\>(json: &serde_json::value::Value)
2019-03-06 18:55:21 +09:00
```rust
let json_obj = json!({
"school": {
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
2019-03-06 18:55:21 +09:00
},
2019-04-04 09:37:44 +09:00
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]});
2019-03-06 18:55:21 +09:00
2019-04-04 09:37:44 +09:00
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Friend {
name: String,
age: Option<u8>,
}
let mut selector = jsonpath::selector_as::<Vec<Friend>>(&json_obj);
2019-03-06 18:55:21 +09:00
2019-03-11 17:35:15 +09:00
let json = selector("$..friends[0]").unwrap();
2019-04-04 09:37:44 +09:00
let ret = vec!(
Friend { name: "친구3".to_string(), age: Some(30) },
Friend { name: "친구1".to_string(), age: Some(20) }
);
2019-03-06 18:55:21 +09:00
assert_eq!(json, ret);
2019-03-11 17:35:15 +09:00
let json = selector("$..friends[1]").unwrap();
2019-04-04 09:37:44 +09:00
let ret = vec!(
Friend { name: "친구4".to_string(), age: None },
Friend { name: "친구2".to_string(), age: Some(20) }
);
2019-03-06 18:55:21 +09:00
assert_eq!(json, ret);
2019-04-04 09:37:44 +09:00
```