jsonpath-rs add "map", "get" function

This commit is contained in:
freestrings
2019-05-16 14:14:09 +09:00
parent 5b653ab8a0
commit 802640a6da
11 changed files with 142 additions and 17 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "jsonpath_lib"
version = "0.1.12"
version = "0.1.13"
authors = ["Changseok Han <freestrings@gmail.com>"]
description = "It is JsonPath engine written in Rust. it provide a similar API interface in Webassembly and Javascript also. - Webassembly Demo: https://freestrings.github.io/jsonpath"

View File

@ -34,6 +34,9 @@ __extra () {
printf "\n"
sleep 1
cd "${DIR}"/javascript && echo "NodeJs - jsonpath-rs - compile: " && time ./bench.sh nativeCompile ${ITER}
printf "\n"
sleep 1
cd "${DIR}"/javascript && echo "NodeJs - jsonpath-rs - selectorClassMap: " && time ./bench.sh nativeSelectorClassMap ${ITER}
}
if [ "$1" = "extra" ]; then

View File

@ -37,8 +37,12 @@ __extra () {
printf "\n"
sleep 1
cd "${DIR}"/javascript && echo "NodeJs - jsonpath-wasm - compile-alloc: " && time ./bench.sh wasmCompileAlloc ${ITER}
printf "\n"
sleep 1
cd "${DIR}"/javascript && echo "NodeJs - jsonpath-wasm - Selector: " && time ./bench.sh wasmSelectorClass ${ITER}
printf "\n"
sleep 1
cd "${DIR}"/javascript && echo "NodeJs - jsonpath-wasm - Selector map: " && time ./bench.sh wasmSelectorClassMap ${ITER}
}
if [ "$1" = "extra" ]; then

View File

@ -71,6 +71,13 @@ function nativeSelect() {
}
}
function nativeSelectorClassMap() {
let selector = new jpwRs.Selector();
for (var i = 0; i < iter; i++) {
let _ = selector.path(path).value(jsonStr).map((v) => v).get();
}
}
function wasmSelector() {
let selector = jpw.selector(getJson());
for (var i = 0; i < iter; i++) {
@ -133,6 +140,16 @@ function wasmSelectorClass() {
}
}
function wasmSelectorClassMap() {
let selector = new jpw.Selector();
for (var i = 0; i < iter; i++) {
selector.path(path);
selector.value(jsonStr);
let _1 = selector.map((v) => v);
let _2 = selector.get();
}
}
const functionName = process.argv[2];
const iter = parseInt(process.argv[3], 10);
eval(functionName + "()");

View File

@ -41,22 +41,38 @@ let jsonObj = {
let selector = new jsonpath.Selector().value(jsonObj);
{
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectTo();
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs();
let resultObj = [{"name": "친구3", "age": 30}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
{
let jsonObj = selector.path('$..[?(@.age == 20)]').selectTo();
let jsonObj = selector.path('$..[?(@.age == 20)]').selectAs();
let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
{
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectTo();
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs();
let resultObj = [{"name": "친구5", "age": 20}];
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
}
{
let jsonObj1 = selector.value(jsonObj).map(function(v) {
let f1 = v[0];
f1.age = 30;
return v;
}).get();
let resultObj1 = [{"name": "친구1", "age": 30}, {"name": "친구2", "age": 20}];
console.log(JSON.stringify(jsonObj1) === JSON.stringify(resultObj1));
selector.path('$..[?(@.age == 20)]');
let jsonObj2 = selector.selectAs();
let resultObj2 = [{"name": "친구2", "age": 20}];
console.log(JSON.stringify(jsonObj2) === JSON.stringify(resultObj2));
}
```
### jsonpath.select(json: string|object, jsonpath: string)

View File

@ -42,18 +42,40 @@ class Selector {
if(typeof json != 'string') {
json = JSON.stringify(json)
}
this._selector.value_from_str(json);
this._selector.valueFromStr(json);
return this;
}
selectToStr() {
return this._selector.select_to_str();
return this.selectAsStr();
}
selectTo() {
return JSON.parse(this.selectToStr());
return this.selectAs();
}
selectAsStr() {
return this._selector.selectAsStr();
}
selectAs() {
return JSON.parse(this.selectAsStr());
}
map(func) {
this._selector.map((json) => {
var result = func.call(null, JSON.parse(json));
if(typeof result !== 'string') {
result = JSON.stringify(result);
}
return result;
});
return this;
}
get() {
return JSON.parse(this._selector.get());
}
}
module.exports = {

View File

@ -1,6 +1,6 @@
[package]
name = "jsonpath4nodejs"
version = "0.1.2"
version = "0.1.3"
authors = ["Changseok Han <freestrings@gmail.com>"]
description = "jsonpath_lib bindings for nodejs"
keywords = ["library", "jsonpath", "json", "nodejs"]
@ -14,7 +14,7 @@ exclude = ["artifacts.json", "index.node"]
neon-build = "0.2.0"
[dependencies]
jsonpath_lib = "0.1.12"
jsonpath_lib = "0.1.13"
neon = "0.2.0"
neon-serde = "0.1.1"
serde_json = { version = "1.0", features = ["preserve_order"] }

View File

@ -139,7 +139,7 @@ declare_types! {
Ok(JsUndefined::new().upcast())
}
method value_from_str(mut ctx) {
method valueFromStr(mut ctx) {
let mut this = ctx.this();
let json_str = ctx.argument::<JsString>(0)?.value();
@ -151,13 +151,13 @@ declare_types! {
Ok(JsUndefined::new().upcast())
}
method select_to_str(mut ctx) {
method selectAsStr(mut ctx) {
let mut this = ctx.this();
let result = {
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
this.selector.select_to_str()
this.selector.select_as_str()
};
match result {
@ -165,6 +165,50 @@ declare_types! {
Err(e) => panic!("{:?}", e)
}
}
method map(mut ctx) {
let null = ctx.null();
let mut this = ctx.this();
let func = ctx.argument::<JsFunction>(0)?;
let value = {
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
match this.selector.select_as_str() {
Ok(v) => v,
Err(e) => panic!("{:?}", e)
}
};
let js_value = JsString::new(&mut ctx, &value);
let json_str = func.call(&mut ctx, null, vec![js_value])?
.downcast::<JsString>()
.or_throw(&mut ctx)?
.value();
{
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
let _ = this.selector.value_from_str(&json_str);
}
Ok(JsUndefined::new().upcast())
}
method get(mut ctx) {
let mut this = ctx.this();
let result = {
let guard = ctx.lock();
let mut this = this.borrow_mut(&guard);
match this.selector.get() {
Ok(v) => v,
Err(e) => panic!("{:?}", e)
}
};
Ok(JsString::new(&mut ctx, &result.to_string()).upcast())
}
}
}
register_module!(mut m, {

View File

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

View File

@ -1,6 +1,6 @@
{
"name": "jsonpath-rs",
"version": "0.1.10",
"version": "0.1.11",
"description": "It is JsonPath implementation. The core implementation is written in Rust",
"author": "Changseok Han <freestrings@gmail.com>",
"license": "MIT",

View File

@ -444,7 +444,7 @@ describe('README test', () => {
let selector = new jsonpath.Selector().value(jsonObj);
{
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectTo();
let jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs();
let resultObj = [{"name": "친구3", "age": 30}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: $..[?(@.age >= 30)]';
@ -452,7 +452,7 @@ describe('README test', () => {
}
{
let jsonObj = selector.path('$..[?(@.age == 20)]').selectTo();
let jsonObj = selector.path('$..[?(@.age == 20)]').selectAs();
let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: $..[?(@.age >= 20)]';
@ -460,13 +460,32 @@ describe('README test', () => {
}
{
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectTo();
let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs();
let resultObj = [{"name": "친구5", "age": 20}];
if(JSON.stringify(jsonObj) !== JSON.stringify(resultObj)) {
throw 'jsonpath.Selector: change value';
}
}
{
let jsonObj1 = selector.value(jsonObj).map(function(v) {
let f1 = v[0];
f1.age = 30;
return v;
}).get();
let resultObj1 = [{"name": "친구1", "age": 30}, {"name": "친구2", "age": 20}];
if(JSON.stringify(jsonObj1) !== JSON.stringify(resultObj1)) {
throw 'jsonpath.Selector.map';
}
let jsonObj2 = selector.path('$..[?(@.age == 20)]').selectAs();
let resultObj2 = [{"name": "친구2", "age": 20}];
if(JSON.stringify(jsonObj2) !== JSON.stringify(resultObj2)) {
throw 'jsonpath.Selector.map and then select';
}
}
done();
});