README.md - update javascript example

This commit is contained in:
freestrings 2019-04-05 11:52:20 +09:00
parent 46b10d71a0
commit 238d1f45b5
3 changed files with 211 additions and 87 deletions

169
README.md
View File

@ -69,56 +69,92 @@ const jsonpath = require('jsonpath-rs');
```javascript ```javascript
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [{"id": 0}, {"id": 1}] "friends": [
}, {"name": "친구1", "age": 20},
"friends": [{"id": 0}, {"id": 1}] {"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret = [{"id": 0}, {"id": 0}];
let a = jsonpath.select(JSON.stringify(jsonObj), "$..friends[0]"); let ret = [
let b = jsonpath.select(jsonObj, "$..friends[0]"); {"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let selectAsString = jsonpath.select(JSON.stringify(jsonObj), '$..friends[0]');
let selectAsObj = jsonpath.select(jsonObj, '$..friends[0]');
console.log( console.log(
JSON.stringify(ret) == JSON.stringify(a), JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(a) == JSON.stringify(b) JSON.stringify(ret) == JSON.stringify(selectAsObj)
); );
// => true, true
``` ```
### javascript - jsonpath.compile(jsonpath: string) ### javascript - jsonpath.compile(jsonpath: string)
```javascript ```javascript
let template = jsonpath.compile("$..friends[0]"); let template = jsonpath.compile('$..friends[0]');
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [ {"id": 0}, {"id": 1} ] "friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
}, },
"friends": [ {"id": 0}, {"id": 1} ] "friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret = JSON.stringify([ {"id": 0}, {"id": 0} ]); let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
// 1. read as json object let selectAsString = template(JSON.stringify(jsonObj));
console.log(JSON.stringify(template(jsonObj)) == ret); let selectAsObj = template(jsonObj);
// 2. read as json string
console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == ret); console.log(
JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(ret) == JSON.stringify(selectAsObj)
);
// => true, true
let jsonObj2 = { let jsonObj2 = {
"school": { "school": {
"friends": [ "friends": [
{"name": "Millicent Norman"}, {"name": "Millicent Norman"},
{"name": "Vincent Cannon"} {"name": "Vincent Cannon"}
] ]
}, },
"friends": [ {"id": 0}, {"id": 1} ] "friends": [ {"age": 30}, {"age": 40} ]
}; };
let ret2 = JSON.stringify([ {"id": 0}, {"name": "Millicent Norman"} ]); let ret2 = [
{"age": 30},
{"name": "Millicent Norman"}
];
// 1. read as json object let selectAsString2 = template(JSON.stringify(jsonObj2));
console.log(JSON.stringify(template(jsonObj2)) == ret2); let selectAsObj2 = template(jsonObj2);
// 2. read as json string
console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); console.log(
JSON.stringify(ret2) == JSON.stringify(selectAsString2),
JSON.stringify(ret2) == JSON.stringify(selectAsObj2)
);
// => true, true
``` ```
### javascript - jsonpath.selector(json: string|object) ### javascript - jsonpath.selector(json: string|object)
@ -126,23 +162,40 @@ console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2);
```javascript ```javascript
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [{"id": 0}, {"id": 1}] "friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
}, },
"friends": [{"id": 0},{"id": 1}] "friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret1 = JSON.stringify([ {"id": 0}, {"id": 0} ]); let ret1 = [
let ret2 = JSON.stringify([ {"id": 1}, {"id": 1} ]); {"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let ret2 = [
{"name": "친구4"},
{"name": "친구2", "age": 20}
];
// 1. read as json object
let selector = jsonpath.selector(jsonObj); let selector = jsonpath.selector(jsonObj);
console.log(JSON.stringify(selector("$..friends[0]")) == ret1); // or as json string
console.log(JSON.stringify(selector("$..friends[1]")) == ret2); // let selector = jsonpath.selector(JSON.stringify(jsonObj));
// 2. read as json string let select1 = selector('$..friends[0]');
let selector = jsonpath.selector(JSON.stringify(jsonObj)); let select2 = selector('$..friends[1]');
console.log(JSON.stringify(selector("$..friends[0]")) == ret1);
console.log(JSON.stringify(selector("$..friends[1]")) == ret2); console.log(
JSON.stringify(ret1) == JSON.stringify(select1),
JSON.stringify(ret2) == JSON.stringify(select2)
);
// => true, true
``` ```
### javascript - alloc_json, dealloc_json ### javascript - alloc_json, dealloc_json
@ -154,34 +207,52 @@ wasm-bindgen은 Javascript와 Webassembly 간 값을 주고받을 때 JSON 객
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. 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 ```javascript
const jsonpath = require('@nodejs/jsonpath-wasm');
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [{"id": 0}, {"id": 1}] "friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
}, },
"friends": [{"id": 0},{"id": 1}] "friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
// allocate jsonObj in webassembly
let ptr = jsonpath.alloc_json(jsonObj);
// `0` is invalid pointer
if(ptr == 0) {
console.error('invalid ptr');
}
let path = '$..friends[0]'; let path = '$..friends[0]';
let template = jsonpath.compile(path); let template = jsonpath.compile(path);
let selector = jsonpath.selector(jsonObj); let selector = jsonpath.selector(jsonObj);
// create selector as pointer
let ptr = jsonpath.alloc_json(jsonObj); let ptrSelector = jsonpath.selector(ptr);
if(ptr == 0) console.error('invalid ptr'); // `0` is invalid pointer
let selector2 = jsonpath.selector(ptr);
let ret1 = selector(path) let ret1 = selector(path)
let ret2 = selector2(path) let ret2 = ptrSelector(path)
let ret3 = template(jsonObj); let ret3 = template(jsonObj);
// select as pointer
let ret4 = template(ptr); let ret4 = template(ptr);
let ret5 = jsonpath.select(jsonObj, path); let ret5 = jsonpath.select(jsonObj, path);
// select as pointer
let ret6 = jsonpath.select(ptr, path); let ret6 = jsonpath.select(ptr, path);
console.log( console.log(
JSON.stringify(ret1) == JSON.stringify(ret2),// true JSON.stringify(ret1) == JSON.stringify(ret2),
JSON.stringify(ret1) == JSON.stringify(ret3),// true JSON.stringify(ret1) == JSON.stringify(ret3),
JSON.stringify(ret1) == JSON.stringify(ret4),// true JSON.stringify(ret1) == JSON.stringify(ret4),
JSON.stringify(ret1) == JSON.stringify(ret5),// true JSON.stringify(ret1) == JSON.stringify(ret5),
JSON.stringify(ret1) == JSON.stringify(ret6));// true JSON.stringify(ret1) == JSON.stringify(ret6));
// => true true true true true
jsonpath.dealloc_json(ptr); jsonpath.dealloc_json(ptr);
``` ```

View File

@ -24,56 +24,92 @@ Build from source instead of using pre-built binary, and if Rust is not installe
```javascript ```javascript
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [{"id": 0}, {"id": 1}] "friends": [
}, {"name": "친구1", "age": 20},
"friends": [{"id": 0}, {"id": 1}] {"name": "친구2", "age": 20}
]
},
"friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret = [{"id": 0}, {"id": 0}];
let a = jsonpath.select(JSON.stringify(jsonObj), "$..friends[0]"); let ret = [
let b = jsonpath.select(jsonObj, "$..friends[0]"); {"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let selectAsString = jsonpath.select(JSON.stringify(jsonObj), '$..friends[0]');
let selectAsObj = jsonpath.select(jsonObj, '$..friends[0]');
console.log( console.log(
JSON.stringify(ret) == JSON.stringify(a), JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(a) == JSON.stringify(b) JSON.stringify(ret) == JSON.stringify(selectAsObj)
); );
// => true, true
``` ```
### jsonpath.compile(jsonpath: string) ### jsonpath.compile(jsonpath: string)
```javascript ```javascript
let template = jsonpath.compile("$..friends[0]"); let template = jsonpath.compile('$..friends[0]');
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [ {"id": 0}, {"id": 1} ] "friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
}, },
"friends": [ {"id": 0}, {"id": 1} ] "friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret = JSON.stringify([ {"id": 0}, {"id": 0} ]); let ret = [
{"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
// 1. read as json object let selectAsString = template(JSON.stringify(jsonObj));
console.log(JSON.stringify(template(jsonObj)) == ret); let selectAsObj = template(jsonObj);
// 2. read as json string
console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == ret); console.log(
JSON.stringify(ret) == JSON.stringify(selectAsString),
JSON.stringify(ret) == JSON.stringify(selectAsObj)
);
// => true, true
let jsonObj2 = { let jsonObj2 = {
"school": { "school": {
"friends": [ "friends": [
{"name": "Millicent Norman"}, {"name": "Millicent Norman"},
{"name": "Vincent Cannon"} {"name": "Vincent Cannon"}
] ]
}, },
"friends": [ {"id": 0}, {"id": 1} ] "friends": [ {"age": 30}, {"age": 40} ]
}; };
let ret2 = JSON.stringify([ {"id": 0}, {"name": "Millicent Norman"} ]); let ret2 = [
{"age": 30},
{"name": "Millicent Norman"}
];
// 1. read as json object let selectAsString2 = template(JSON.stringify(jsonObj2));
console.log(JSON.stringify(template(jsonObj2)) == ret2); let selectAsObj2 = template(jsonObj2);
// 2. read as json string
console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); console.log(
JSON.stringify(ret2) == JSON.stringify(selectAsString2),
JSON.stringify(ret2) == JSON.stringify(selectAsObj2)
);
// => true, true
``` ```
### jsonpath.selector(json: string|object) ### jsonpath.selector(json: string|object)
@ -81,21 +117,38 @@ console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2);
```javascript ```javascript
let jsonObj = { let jsonObj = {
"school": { "school": {
"friends": [{"id": 0}, {"id": 1}] "friends": [
{"name": "친구1", "age": 20},
{"name": "친구2", "age": 20}
]
}, },
"friends": [{"id": 0},{"id": 1}] "friends": [
{"name": "친구3", "age": 30},
{"name": "친구4"}
]
}; };
let ret1 = JSON.stringify([ {"id": 0}, {"id": 0} ]); let ret1 = [
let ret2 = JSON.stringify([ {"id": 1}, {"id": 1} ]); {"name": "친구3", "age": 30},
{"name": "친구1", "age": 20}
];
let ret2 = [
{"name": "친구4"},
{"name": "친구2", "age": 20}
];
// 1. read as json object
let selector = jsonpath.selector(jsonObj); let selector = jsonpath.selector(jsonObj);
console.log(JSON.stringify(selector("$..friends[0]")) == ret1); // or as json string
console.log(JSON.stringify(selector("$..friends[1]")) == ret2); // let selector = jsonpath.selector(JSON.stringify(jsonObj));
// 2. read as json string let select1 = selector('$..friends[0]');
let selector = jsonpath.selector(JSON.stringify(jsonObj)); let select2 = selector('$..friends[1]');
console.log(JSON.stringify(selector("$..friends[0]")) == ret1);
console.log(JSON.stringify(selector("$..friends[1]")) == ret2); console.log(
JSON.stringify(ret1) == JSON.stringify(select1),
JSON.stringify(ret2) == JSON.stringify(select2)
);
// => true, true
``` ```

View File

@ -1,6 +1,6 @@
{ {
"name": "jsonpath-rs", "name": "jsonpath-rs",
"version": "0.1.1", "version": "0.1.5",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {