diff --git a/README.md b/README.md index 653911b..30273d6 100644 --- a/README.md +++ b/README.md @@ -69,56 +69,92 @@ const jsonpath = require('jsonpath-rs'); ```javascript let jsonObj = { - "school": { - "friends": [{"id": 0}, {"id": 1}] - }, - "friends": [{"id": 0}, {"id": 1}] + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"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 b = jsonpath.select(jsonObj, "$..friends[0]"); +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]'); + console.log( - JSON.stringify(ret) == JSON.stringify(a), - JSON.stringify(a) == JSON.stringify(b) + JSON.stringify(ret) == JSON.stringify(selectAsString), + JSON.stringify(ret) == JSON.stringify(selectAsObj) ); + +// => true, true ``` ### javascript - jsonpath.compile(jsonpath: string) ```javascript -let template = jsonpath.compile("$..friends[0]"); +let template = jsonpath.compile('$..friends[0]'); let jsonObj = { "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 -console.log(JSON.stringify(template(jsonObj)) == ret); -// 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == ret); +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 let jsonObj2 = { "school": { - "friends": [ - {"name": "Millicent Norman"}, - {"name": "Vincent Cannon"} + "friends": [ + {"name": "Millicent Norman"}, + {"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 -console.log(JSON.stringify(template(jsonObj2)) == ret2); -// 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); +let selectAsString2 = template(JSON.stringify(jsonObj2)); +let selectAsObj2 = template(jsonObj2); + +console.log( + JSON.stringify(ret2) == JSON.stringify(selectAsString2), + JSON.stringify(ret2) == JSON.stringify(selectAsObj2) +); + +// => true, true ``` ### javascript - jsonpath.selector(json: string|object) @@ -126,23 +162,40 @@ console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); ```javascript let jsonObj = { "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 ret2 = JSON.stringify([ {"id": 1}, {"id": 1} ]); +let ret1 = [ + {"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); -console.log(JSON.stringify(selector("$..friends[0]")) == ret1); -console.log(JSON.stringify(selector("$..friends[1]")) == ret2); +// or as json string +// let selector = jsonpath.selector(JSON.stringify(jsonObj)); -// 2. read as json string -let selector = jsonpath.selector(JSON.stringify(jsonObj)); -console.log(JSON.stringify(selector("$..friends[0]")) == ret1); -console.log(JSON.stringify(selector("$..friends[1]")) == ret2); +let select1 = selector('$..friends[0]'); +let select2 = selector('$..friends[1]'); + +console.log( + JSON.stringify(ret1) == JSON.stringify(select1), + JSON.stringify(ret2) == JSON.stringify(select2) +); + +// => true, true ``` ### 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. ```javascript +const jsonpath = require('@nodejs/jsonpath-wasm'); + let jsonObj = { "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 template = jsonpath.compile(path); let selector = jsonpath.selector(jsonObj); - -let ptr = jsonpath.alloc_json(jsonObj); -if(ptr == 0) console.error('invalid ptr'); // `0` is invalid pointer -let selector2 = jsonpath.selector(ptr); +// create selector as pointer +let ptrSelector = jsonpath.selector(ptr); let ret1 = selector(path) -let ret2 = selector2(path) +let ret2 = ptrSelector(path) let ret3 = template(jsonObj); +// select as pointer let ret4 = template(ptr); let ret5 = jsonpath.select(jsonObj, path); +// select as pointer let ret6 = jsonpath.select(ptr, path); console.log( - JSON.stringify(ret1) == JSON.stringify(ret2),// true - JSON.stringify(ret1) == JSON.stringify(ret3),// true - JSON.stringify(ret1) == JSON.stringify(ret4),// true - JSON.stringify(ret1) == JSON.stringify(ret5),// true - JSON.stringify(ret1) == JSON.stringify(ret6));// true + 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 jsonpath.dealloc_json(ptr); ``` diff --git a/nodejs/README.md b/nodejs/README.md index a05a5c5..facda90 100644 --- a/nodejs/README.md +++ b/nodejs/README.md @@ -24,56 +24,92 @@ Build from source instead of using pre-built binary, and if Rust is not installe ```javascript let jsonObj = { - "school": { - "friends": [{"id": 0}, {"id": 1}] - }, - "friends": [{"id": 0}, {"id": 1}] + "school": { + "friends": [ + {"name": "친구1", "age": 20}, + {"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 b = jsonpath.select(jsonObj, "$..friends[0]"); +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]'); + console.log( - JSON.stringify(ret) == JSON.stringify(a), - JSON.stringify(a) == JSON.stringify(b) + JSON.stringify(ret) == JSON.stringify(selectAsString), + JSON.stringify(ret) == JSON.stringify(selectAsObj) ); + +// => true, true ``` ### jsonpath.compile(jsonpath: string) ```javascript -let template = jsonpath.compile("$..friends[0]"); +let template = jsonpath.compile('$..friends[0]'); let jsonObj = { "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 -console.log(JSON.stringify(template(jsonObj)) == ret); -// 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == ret); +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 let jsonObj2 = { "school": { - "friends": [ - {"name": "Millicent Norman"}, - {"name": "Vincent Cannon"} + "friends": [ + {"name": "Millicent Norman"}, + {"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 -console.log(JSON.stringify(template(jsonObj2)) == ret2); -// 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); +let selectAsString2 = template(JSON.stringify(jsonObj2)); +let selectAsObj2 = template(jsonObj2); + +console.log( + JSON.stringify(ret2) == JSON.stringify(selectAsString2), + JSON.stringify(ret2) == JSON.stringify(selectAsObj2) +); + +// => true, true ``` ### jsonpath.selector(json: string|object) @@ -81,21 +117,38 @@ console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); ```javascript let jsonObj = { "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 ret2 = JSON.stringify([ {"id": 1}, {"id": 1} ]); +let ret1 = [ + {"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); -console.log(JSON.stringify(selector("$..friends[0]")) == ret1); -console.log(JSON.stringify(selector("$..friends[1]")) == ret2); +// or as json string +// let selector = jsonpath.selector(JSON.stringify(jsonObj)); -// 2. read as json string -let selector = jsonpath.selector(JSON.stringify(jsonObj)); -console.log(JSON.stringify(selector("$..friends[0]")) == ret1); -console.log(JSON.stringify(selector("$..friends[1]")) == ret2); +let select1 = selector('$..friends[0]'); +let select2 = selector('$..friends[1]'); + +console.log( + JSON.stringify(ret1) == JSON.stringify(select1), + JSON.stringify(ret2) == JSON.stringify(select2) +); + +// => true, true ``` diff --git a/nodejs/package-lock.json b/nodejs/package-lock.json index b78dc49..b7e0acc 100644 --- a/nodejs/package-lock.json +++ b/nodejs/package-lock.json @@ -1,6 +1,6 @@ { "name": "jsonpath-rs", - "version": "0.1.1", + "version": "0.1.5", "lockfileVersion": 1, "requires": true, "dependencies": {