From 3e3d9e8770102d10523a4b152d2d9ba94ce35401 Mon Sep 17 00:00:00 2001 From: freestrings Date: Tue, 26 Mar 2019 18:28:54 +0900 Subject: [PATCH] =?UTF-8?q?nodejs/README.md=20-=20=EC=A0=95=EB=A6=AC,=20ve?= =?UTF-8?q?rsion=200.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodejs/README.md | 92 ++++++++++++++++++++++++++++++++++++++- nodejs/package.json | 5 ++- nodejs/test/index.spec.js | 69 +++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 3 deletions(-) diff --git a/nodejs/README.md b/nodejs/README.md index 02d46bd..64bf8ae 100644 --- a/nodejs/README.md +++ b/nodejs/README.md @@ -1,3 +1,93 @@ # jsonpath-rs -JsonPath engine for NodeJs with Rust native implementation. +[![Build Status](https://travis-ci.org/freestrings/jsonpath.svg?branch=master)](https://travis-ci.org/freestrings/jsonpath) + +It is [JsonPath](https://goessner.net/articles/JsonPath/) implementation. The core implementation is written in Rust-lang. + +## 목차 + +* [jsonpath.select(json: string|object, jsonpath: string)](#json-stringobject-jsonpath-string) +* [jsonpath.compile(jsonpath: string)](#compilejsonpath-string) +* [jsonpath.selector(json: string|object)](#selectorjson-stringobject) +* [Simple time check](https://github.com/freestrings/jsonpath/wiki/Simple-timecheck-jsonpath-native) +* [Other Examples](https://github.com/freestrings/jsonpath/wiki/Javascript-examples) + +### jsonpath.select(json: string|object, jsonpath: string) + +```javascript +let jsonObj = { + "school": { + "friends": [{"id": 0}, {"id": 1}] + }, + "friends": [{"id": 0}, {"id": 1}] +}; +let ret = [{"id": 0}, {"id": 0}]; + +let a = jsonpath.select(JSON.stringify(jsonObj), "$..friends[0]"); +let b = jsonpath.select(jsonObj, "$..friends[0]"); +console.log( + JSON.stringify(ret) == JSON.stringify(a), + JSON.stringify(a) == JSON.stringify(b) +); +``` + +### jsonpath.compile(jsonpath: string) + +```javascript +let template = jsonpath.compile("$..friends[0]"); + +let jsonObj = { + "school": { + "friends": [ {"id": 0}, {"id": 1} ] + }, + "friends": [ {"id": 0}, {"id": 1} ] +}; + +let ret = JSON.stringify([ {"id": 0}, {"id": 0} ]); + +// 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 jsonObj2 = { + "school": { + "friends": [ + {"name": "Millicent Norman"}, + {"name": "Vincent Cannon"} + ] + }, + "friends": [ {"id": 0}, {"id": 1} ] +}; + +let ret2 = JSON.stringify([ {"id": 0}, {"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); +``` + +### jsonpath.selector(json: string|object) + +```javascript +let jsonObj = { + "school": { + "friends": [{"id": 0}, {"id": 1}] + }, + "friends": [{"id": 0},{"id": 1}] +}; + +let ret1 = JSON.stringify([ {"id": 0}, {"id": 0} ]); +let ret2 = JSON.stringify([ {"id": 1}, {"id": 1} ]); + +// 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); + +// 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); +``` diff --git a/nodejs/package.json b/nodejs/package.json index 9b7c317..2bc78dd 100644 --- a/nodejs/package.json +++ b/nodejs/package.json @@ -1,10 +1,11 @@ { "name": "jsonpath-rs", - "version": "0.1.0", - "description": "JsonPath engine for NodeJs with Rust native implementation.", + "version": "0.1.1", + "description": "It is native binding of JsonPath engine written in Rust", "author": "Changseok Han ", "repository": "git+https://github.com/freestrings/jsonpath", "license": "MIT", + "keywords": ["jsonpath", "native", "rustlang", "json"], "main": "lib/index.js", "dependencies": { "neon-cli": "^0.2.0", diff --git a/nodejs/test/index.spec.js b/nodejs/test/index.spec.js index dc3cbad..08b026f 100644 --- a/nodejs/test/index.spec.js +++ b/nodejs/test/index.spec.js @@ -27,4 +27,73 @@ describe('select test', () => { done(); } }); +}); + +describe('filter test', () => { + it('complex filter1', (done) => { + let json = { + 'store': { + 'book': [ + { + 'category': 'reference', + 'author': 'Nigel Rees', + 'title': 'Sayings of the Century', + 'price': 8.95, + }, + { + 'category': 'fiction', + 'author': 'Evelyn Waugh', + 'title': 'Sword of Honour', + 'price': 12.99, + }, + { + 'category': 'fiction', + 'author': 'Herman Melville', + 'title': 'Moby Dick', + 'isbn': '0-553-21311-3', + 'price': 8.99, + }, + { + 'category': 'fiction', + 'author': 'J. R. R. Tolkien', + 'title': 'The Lord of the Rings', + 'isbn': '0-395-19395-8', + 'price': 22.99, + }, + ], + 'bicycle': { + 'color': 'red', + 'price': 19.95, + }, + }, + 'expensive': 10, + }; + + let target = [ + { + category: 'fiction', + author: 'Evelyn Waugh', + title: 'Sword of Honour', + price: 12.99, + }, + { + category: 'fiction', + author: 'J. R. R. Tolkien', + title: 'The Lord of the Rings', + isbn: '0-395-19395-8', + price: 22.99, + }, + { + category: 'reference', + author: 'Nigel Rees', + title: 'Sayings of the Century', + price: 8.95, + }] + ; + + let result = jsonpath.select(json, '$..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")]'); + if (JSON.stringify(result) === JSON.stringify(target)) { + done(); + } + }); }); \ No newline at end of file