diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8fac43f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,84 @@ +language: rust +sudo: false + +cache: cargo + +matrix: + include: + + - rust: stable + env: RUST_BACKTRACE=1 + # addons: + # firefox: latest + # chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo build --verbose --all + - cargo test --verbose --all + - cd wasm + - wasm-pack build +# - wasm-pack test --chrome --firefox --headless + + # Builds with wasm-pack. + - rust: beta + env: RUST_BACKTRACE=1 +# addons: +# firefox: latest +# chrome: stable + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f + script: + - cargo build --verbose --all + - cargo test --verbose --all + - cd wasm + - wasm-pack build +# - wasm-pack test --chrome --firefox --headless + + # Builds on nightly. + - rust: nightly + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo build --verbose --all + - cargo test --verbose --all + - cd wasm + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" + - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" + + # Builds on beta. + - rust: beta + env: RUST_BACKTRACE=1 + before_script: + - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) + - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) + - cargo install-update -a + - rustup target add wasm32-unknown-unknown + script: + - cargo build --verbose --all + - cargo test --verbose --all + - cd wasm + - cargo check + - cargo check --target wasm32-unknown-unknown + - cargo check --no-default-features + - cargo check --target wasm32-unknown-unknown --no-default-features + - cargo check --no-default-features --features console_error_panic_hook + - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook + # Note: no enabling the `wee_alloc` feature here because it requires + # nightly for now. diff --git a/Cargo.toml b/Cargo.toml index 842dd0e..3c85d6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "jsonpath_lib" -version = "0.1.0" +version = "0.1.1" authors = ["Changseok Han "] -description = "JSONPath for Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath" +description = "JsonPath for Rust and Webassembly - Webassembly Demo: https://freestrings.github.io/jsonpath" readme = "README.md" keywords = ["library", "jsonpath", "json", "webassembly"] @@ -11,7 +11,9 @@ keywords = ["library", "jsonpath", "json", "webassembly"] repository = "https://github.com/freestrings/jsonpath" documentation = "https://docs.rs/jsonpath_lib/0.1.0/jsonpath_lib" license = "MIT" -license-file = "LICENSE" + +[badges] +travis-ci = { repository = "freestrings/jsonpath", branch = "master" } [dependencies] log = "0.4" diff --git a/README.md b/README.md index cb36f9d..86c69e4 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,40 @@ -# Jsonpath Library +# jsonpath-lib -[JsonPath](https://goessner.net/articles/JsonPath/) Rust 구현 +[![Build Status](https://travis-ci.org/freestrings/jsonpath.svg?branch=master)](https://travis-ci.org/freestrings/jsonpath) + +`Rust` 버전 [JsonPath](https://goessner.net/articles/JsonPath/) 구현이다. Rust 구현과 동일한 기능을 `Webassembly` 로 제공하는 것 또한 목적이다. + +The `Rust` version is a [JsonPath](https://goessner.net/articles/JsonPath/) implementation. It is also aimed to provide the same functionality as `Webassembly` in Rust implementation. ## 왜? -To enjoy Rust -## 사용법 +To enjoy Rust! + +## 목차 + +[With Javascript (Webassembly)](#with-javascript-webassembly) + +- [jsonpath-wasm library](#jsonpath-wasm-library) +- [javascript - jsonpath.read(json: string|object, jsonpath: string)](#javascript---jsonpathreadjson-stringobject-jsonpath-string) +- [javascript - jsonpath.compile(jsonpath: string)](#javascript---jsonpathcompilejsonpath-string) +- [javascript - jsonpath.reader(json: string|object)](#javascript---jsonpathreaderjson-stringobject) +- [javascript - examples](#javascript---examples) + +[With Rust (as library)](#with-rust-as-library) + +- [jsonpath_lib library](#jsonpath_lib-library) +- [rust - jsonpath::read(json: serde_json::value::Value, jsonpath: &str)](#rust---jsonpathreadjson-serde_jsonvaluevalue-jsonpath-str) +- [rust - jsonpath::compile(jsonpath: &str)](#rust---jsonpathcompilejsonpath-str) +- [rust - jsonpath::reader(json: serde_json::value::Value)](#rust---jsonpathreaderjson-serde_jsonvaluevalue) +- [rust - examples](#rust---examples) + +[With AWS API Gateway](#with-aws-api-gateway) + +[Benchmark](#benchmark) ## With Javascript (WebAssembly) -#### `jsonpath-wasm` 라이브리러 +### jsonpath-wasm library *(not yet published `jsonpath-wasm`)* ```javascript @@ -19,7 +44,7 @@ import * as jsonpath from "jsonpath-wasm"; let jsonpath = require('jsonpath-wasm'); ``` -#### `read` 함수 +### javascript - jsonpath.read(json: string|object, jsonpath: string) ```javascript let jsonObj = { @@ -38,7 +63,7 @@ console.log( ); ``` -#### JsonPath 재사용 +### javascript - jsonpath.compile(jsonpath: string) ```javascript let template = jsonpath.compile("$..friends[0]"); @@ -50,29 +75,32 @@ let jsonObj = { "friends": [ {"id": 0}, {"id": 1} ] }; -let ret = [ {"id": 0}, {"id": 0} ]; +let ret = JSON.stringify([ {"id": 0}, {"id": 0} ]); // 1. read as json object -console.log(JSON.stringify(template(jsonObj)) == JSON.stringify(ret)); +console.log(JSON.stringify(template(jsonObj)) == ret); // 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == JSON.stringify(ret)); +console.log(JSON.stringify(template(JSON.stringify(jsonObj))) == ret); let jsonObj2 = { "school": { - "friends": [ {"name": "Millicent Norman"}, {"name": "Vincent Cannon"} ] + "friends": [ + {"name": "Millicent Norman"}, + {"name": "Vincent Cannon"} + ] }, "friends": [ {"id": 0}, {"id": 1} ] }; -let ret2 = [ {"id": 0}, {"name": "Millicent Norman"} ]; +let ret2 = JSON.stringify([ {"id": 0}, {"name": "Millicent Norman"} ]); // 1. read as json object -console.log(JSON.stringify(template(jsonObj2)) == JSON.stringify(ret2)); +console.log(JSON.stringify(template(jsonObj2)) == ret2); // 2. read as json string -console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == JSON.stringify(ret2)); +console.log(JSON.stringify(template(JSON.stringify(jsonObj2))) == ret2); ``` -#### Json 재사용 +### javascript - jsonpath.reader(json: string|object) ```javascript let jsonObj = { @@ -82,18 +110,21 @@ let jsonObj = { "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 reader = jsonpath.reader(jsonObj); -console.log(JSON.stringify(reader("$..friends[0]")) == JSON.stringify([ {"id": 0}, {"id": 0} ])); -console.log(JSON.stringify(reader("$..friends[1]")) == JSON.stringify([ {"id": 1}, {"id": 1} ])); +console.log(JSON.stringify(reader("$..friends[0]")) == ret1); +console.log(JSON.stringify(reader("$..friends[1]")) == ret2); // 2. read as json string let reader2 = jsonpath.reader(JSON.stringify(jsonObj)); -console.log(JSON.stringify(reader2("$..friends[0]")) == JSON.stringify([ {"id": 0}, {"id": 0} ])); -console.log(JSON.stringify(reader2("$..friends[1]")) == JSON.stringify([ {"id": 1}, {"id": 1} ])); +console.log(JSON.stringify(reader2("$..friends[0]")) == ret1); +console.log(JSON.stringify(reader2("$..friends[1]")) == ret2); ``` -### 예제 +### javascript - examples **Demo**: https://freestrings.github.io/jsonpath/ @@ -156,18 +187,20 @@ json 데이터 *(참고 사이트: https://github.com/json-path/JsonPath)* | $..book[?(@.isbn)] | All books with an ISBN number | | $.store.book[?(@.price < 10)] | All books in store cheaper than 10 | | $..* | Give me every thing -| $..book[?((@.price == 12.99 | | $.store.bicycle.price < @.price) || @.category == "reference")] | Complex filter +| $..book[ ?(
(@.price == 12.99 | | $.store.bicycle.price < @.price)
|| @.category == "reference"
)]
| Complex filter ## With Rust (as library) +### jsonpath_lib library + ```rust extern crate jsonpath_lib as jsonpath; #[macro_use] extern crate serde_json; ``` -#### `read` 함수 +### rust - jsonpath::read(json: serde_json::value::Value, jsonpath: &str) ```rust let json_obj = json!({ @@ -181,7 +214,7 @@ let ret = json!([ {"id": 0}, {"id": 0} ]); assert_eq!(json, ret) ``` -#### JsonPath 재사용 +### rust - jsonpath::compile(jsonpath: &str) ```rust let mut template = jsonpath::compile("$..friends[0]"); @@ -209,7 +242,7 @@ let ret = json!([ {"id": 0}, {"name": "Millicent Norman"} ]); assert_eq!(json, ret); ``` -#### Json 재사용 +### rust - jsonpath::reader(json: serde_json::value::Value) ```rust let json_obj = json!({ @@ -230,7 +263,7 @@ let ret = json!([ {"id": 1}, {"id": 1} ]); assert_eq!(json, ret); ``` -#### 예제 +### rust - examples ```rust let json_obj = json!({ @@ -273,9 +306,10 @@ let json_obj = json!({ let mut reader = jsonpath::reader(json_obj); -// -// $.store.book[*].author -// +``` + +#### $.store.book[*].author +```rust let json = reader("$.store.book[*].author").unwrap(); let ret = json!([ "Nigel Rees", @@ -284,10 +318,10 @@ let ret = json!([ "J. R. R. Tolkien" ]); assert_eq!(json, ret); +``` -// -// $..author -// +#### $..author +```rust let json = reader("$..author").unwrap(); let ret = json!([ "Nigel Rees", @@ -296,10 +330,10 @@ let ret = json!([ "J. R. R. Tolkien" ]); assert_eq!(json, ret); +``` -// -// $.store.* -// +#### $.store.* +```rust let json = reader("$.store.*").unwrap(); let ret = json!([ [ @@ -336,17 +370,17 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $.store..price -// +#### $.store..price +```rust let json = reader("$.store..price").unwrap(); let ret = json!([8.95, 12.99, 8.99, 22.99, 19.95]); assert_eq!(ret, json); +``` -// -// $..book[2] -// +#### $..book[2] +```rust let json = reader("$..book[2]").unwrap(); let ret = json!([{ "category" : "fiction", @@ -356,10 +390,10 @@ let ret = json!([{ "price" : 8.99 }]); assert_eq!(ret, json); +``` -// -// $..book[-2] -// +#### $..book[-2] +```rust let json = reader("$..book[-2]").unwrap(); let ret = json!([{ "category" : "fiction", @@ -369,10 +403,10 @@ let ret = json!([{ "price" : 8.99 }]); assert_eq!(ret, json); +``` -// -// $..book[0,1] -// +#### $..book[0,1] +```rust let json = reader("$..book[0,1]").unwrap(); let ret = json!([ { @@ -389,10 +423,10 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $..book[:2] -// +#### $..book[:2] +```rust let json = reader("$..book[:2]").unwrap(); let ret = json!([ { @@ -409,10 +443,10 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $..book[2:] -// +#### $..book[2:] +```rust let json = reader("$..book[2:]").unwrap(); let ret = json!([ { @@ -431,10 +465,10 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $..book[?(@.isbn)] -// +#### $..book[?(@.isbn)] +```rust let json = reader("$..book[?(@.isbn)]").unwrap(); let ret = json!([ { @@ -453,10 +487,10 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $.store.book[?(@.price < 10)] -// +#### $.store.book[?(@.price < 10)] +```rust let json = reader("$.store.book[?(@.price < 10)]").unwrap(); let ret = json!([ { @@ -474,11 +508,15 @@ let ret = json!([ } ]); assert_eq!(ret, json); +``` -// -// $..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")] -// -let json = reader("$..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")]").unwrap(); +#### $..book[?((@.price == 12.99 || $.store.bicycle.price < @.price) || @.category == "reference")] +```rust +let json = reader(r#"$..book[ + ?( + (@.price == 12.99 || $.store.bicycle.price < @.price) + || @.category == "reference" + )]"#).unwrap(); let ret = json!([ { "category": "fiction", @@ -507,5 +545,5 @@ assert_eq!(ret, json); - -# 성능테스트 +## Benchmark diff --git a/src/lib.rs b/src/lib.rs index 81e0f89..9232cb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,9 @@ -//! JSONPath implementation for Rust +//! JsonPath implementation for Rust //! //! # Example //! ``` //! extern crate jsonpath_lib as jsonpath; -//! #[macro_use] -//! extern crate serde_json; +//! #[macro_use] extern crate serde_json; //! //! let json_obj = json!({ //! "store": { @@ -158,14 +157,14 @@ //! ]); //! assert_eq!(ret, json); //! ``` + + #[macro_use] extern crate log; extern crate env_logger; -extern crate serde; #[cfg(test)] -#[macro_use] -extern crate serde_json; +#[macro_use] extern crate serde_json; #[cfg(not(test))] extern crate serde_json; @@ -189,6 +188,9 @@ type Result = result::Result; /// # Read multiple Json multiple times with the same JsonPath /// /// ```rust +/// extern crate jsonpath_lib as jsonpath; +/// #[macro_use] extern crate serde_json; +/// /// let mut template = jsonpath::compile("$..friends[0]"); /// /// @@ -234,6 +236,9 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(Value) -> Result + 'a { /// # Read the same Json multiple times using different JsonPath /// /// ```rust +/// extern crate jsonpath_lib as jsonpath; +/// #[macro_use] extern crate serde_json; +/// /// let json_obj = json!({ /// "school": { /// "friends": [{"id": 0}, {"id": 1}] @@ -264,6 +269,9 @@ pub fn reader(json: Value) -> impl FnMut(&str) -> Result { /// # Read Json using JsonPath /// /// ```rust +/// extern crate jsonpath_lib as jsonpath; +/// #[macro_use] extern crate serde_json; +/// /// let json_obj = json!({ /// "school": { /// "friends": [{"id": 0}, {"id": 1}] diff --git a/wasm/.travis.yml b/wasm/.travis.yml deleted file mode 100644 index 7a91325..0000000 --- a/wasm/.travis.yml +++ /dev/null @@ -1,69 +0,0 @@ -language: rust -sudo: false - -cache: cargo - -matrix: - include: - - # Builds with wasm-pack. - - rust: beta - env: RUST_BACKTRACE=1 - addons: - firefox: latest - chrome: stable - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f - script: - - cargo generate --git . --name testing - # Having a broken Cargo.toml (in that it has curlies in fields) anywhere - # in any of our parent dirs is problematic. - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - wasm-pack build - - wasm-pack test --chrome --firefox --headless - - # Builds on nightly. - - rust: nightly - env: RUST_BACKTRACE=1 - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - rustup target add wasm32-unknown-unknown - script: - - cargo generate --git . --name testing - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - cargo check - - cargo check --target wasm32-unknown-unknown - - cargo check --no-default-features - - cargo check --target wasm32-unknown-unknown --no-default-features - - cargo check --no-default-features --features console_error_panic_hook - - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook - - cargo check --no-default-features --features "console_error_panic_hook wee_alloc" - - cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc" - - # Builds on beta. - - rust: beta - env: RUST_BACKTRACE=1 - before_script: - - (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update) - - (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate) - - cargo install-update -a - - rustup target add wasm32-unknown-unknown - script: - - cargo generate --git . --name testing - - mv Cargo.toml Cargo.toml.tmpl - - cd testing - - cargo check - - cargo check --target wasm32-unknown-unknown - - cargo check --no-default-features - - cargo check --target wasm32-unknown-unknown --no-default-features - - cargo check --no-default-features --features console_error_panic_hook - - cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook - # Note: no enabling the `wee_alloc` feature here because it requires - # nightly for now. diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 67a01b9..7c2a7d8 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "jsonpath-wasm" -version = "0.1.0" +version = "0.1.1" authors = ["Changseok Han "] -description = "JSONPath Webassembly compiled by Rust - Demo: https://freestrings.github.io/jsonpath" +description = "JsonPath Webassembly version compiled by Rust - Demo: https://freestrings.github.io/jsonpath" keywords = ["library", "jsonpath", "json", "webassembly"] diff --git a/wasm/www/.travis.yml b/wasm/www/.travis.yml deleted file mode 100644 index 04d40b4..0000000 --- a/wasm/www/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -language: node_js -node_js: "10" - -script: - - ./node_modules/.bin/webpack