From d960ab3d9489cf53a72d0b6de4b114f8ac6c8181 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Wed, 9 Aug 2017 02:32:33 +0800 Subject: [PATCH 1/7] Update README.md --- README.md | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ab57f14..6d2a3e2 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,14 @@ or surfer.surf(sample1, config); surfer.surf(sample2, config); ``` +#### Compiled JsonPath +JsonPath object is immutable and can be reused safely. + +**Tips:** Most of JsonSurfer API have two version: accepting raw JsonPath string or JsonPath object. The latter is always faster than the former without compiling JsonPath. +```java + JsonPath compiledPath = JsonPathCompiler.compile("$..book[1,3]['author','title']"); + String value = surfer.collectOne(read("sample.json"), String.class, compiledPath); +``` #### Collect the first matched value and stop immediately ```java JsonSurfer jsonSurfer = JsonSurferGson.INSTANCE; @@ -144,11 +152,6 @@ or JsonSurfer jsonSurfer = JsonSurferGson.INSTANCE; Collection multipleResults = jsonSurfer.collectAll(sample, "$.store.book[*]"); ``` -#### Compile JsonPath -```java - JsonPath compiledPath = JsonPathCompiler.compile("$..book[1,3]['author','title']"); - String value = surfer.collectOne(read("sample.json"), String.class, compiledPath); -``` #### Filters * Filter operators @@ -180,12 +183,8 @@ which prints "Leo". System.out.println(compile("$.list[1]").resolve(map, JavaCollectionProvider.INSTANCE)); ``` which prints "bar". -#### Stop parsing on the fly -* Refer to [Stoppable parsing](#stoppable-parsing) #### Share data among processors - Since JsonSurfer emit data in the way of callback, it may become difficult if one of your processing depends one another. Therefore a simple transient map is added for sharing data among your processors. Following unit test shows how to use it: - ```java surfer.configBuilder().bind("$.store.book[1]", new JsonPathListener() { @Override @@ -204,7 +203,21 @@ Since JsonSurfer emit data in the way of callback, it may become difficult if on } }).buildAndSurf(read("sample.json")); ``` - +#### Control parsing +You can pause, resume or stop parsing anytime. +TODO +* Refer to [Stoppable parsing](#stoppable-parsing) +#### Stream support +As of 1.4, JsonSurfer can create an iterator from Json source and JsonPath. Matched value can be pulled from the iterator one by one without loading entire json into memory. +```java + Iterator iterator = surfer.iterator(read("sample.json"), JsonPathCompiler.compile("$.store.book[*]")); +``` +Java8 user can also convert the iterator into a Stream +```java + Stream targetStream = StreamSupport.stream( + Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), + false); +``` ### Examples Sample Json: @@ -437,7 +450,7 @@ $..book[0,1] @Override public void onValue(Object value, ParsingContext context) { System.out.println(value); - context.stopParsing(); + context.stop(); } }) .buildAndSurf(sample); From d7f2e149245c22cdb24ebc4d91252235a5963a00 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Wed, 9 Aug 2017 17:02:03 +0800 Subject: [PATCH 2/7] Update README.md --- README.md | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6d2a3e2..2328091 100644 --- a/README.md +++ b/README.md @@ -204,9 +204,35 @@ Since JsonSurfer emit data in the way of callback, it may become difficult if on }).buildAndSurf(read("sample.json")); ``` #### Control parsing -You can pause, resume or stop parsing anytime. -TODO -* Refer to [Stoppable parsing](#stoppable-parsing) +* Pause and resume parsing. +```java + SurfingConfiguration config = surfer.configBuilder() + .bind("$.store.book[0]", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + LOGGER.info("The first pause"); + context.pause(); + } + }) + .bind("$.store.book[1]", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) { + LOGGER.info("The second pause"); + context.pause(); + } + }).build(); + ResumableParser parser = surfer.getResumableParser(read("sample.json"), config); + assertFalse(parser.resume()); + LOGGER.info("Start parsing"); + parser.parse(); + LOGGER.info("Resume from the first pause"); + assertTrue(parser.resume()); + LOGGER.info("Resume from the second pause"); + assertTrue(parser.resume()); + LOGGER.info("Parsing stopped"); + assertFalse(parser.resume()); +``` +* Completely stop parsing. Refer to [Stoppable parsing](#stoppable-parsing) #### Stream support As of 1.4, JsonSurfer can create an iterator from Json source and JsonPath. Matched value can be pulled from the iterator one by one without loading entire json into memory. ```java From 90f9085b18d502d2666c7ad001581cc98c944bfb Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Wed, 9 Aug 2017 17:08:47 +0800 Subject: [PATCH 3/7] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2328091..3b4ac6c 100644 --- a/README.md +++ b/README.md @@ -46,25 +46,25 @@ JsonSurfer has drivers for most of popular json libraries including: Gson, Jacks     com.github.jsurfer     jsurfer-gson -    1.3.2 +    1.4     com.github.jsurfer     jsurfer-jackson -    1.3.2 +    1.4     com.github.jsurfer     jsurfer-fastjson -    1.3.2 +    1.4     com.github.jsurfer     jsurfer-jsonsimple -    1.3.2 +    1.4 ``` From 81142f05718899cb6de81d402ed82fa42e4239a4 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Fri, 11 Aug 2017 15:50:13 +0800 Subject: [PATCH 4/7] Update README.md --- README.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b4ac6c..3a298c8 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,10 @@ Jsonsurfer is dedicated in processing **big and complicated json** data with thr * Stoppable JsonSurfer is built on stoppable SAX-like interface that allows the processor to stop itself if necessary. + +* Non-Blocking + + JsonSurfer is event-driven and offers non-blocking parser interface. ## Getting started @@ -120,7 +124,8 @@ or }) .buildAndSurf(sample); ``` -#### Reuse listener binding +#### Reuse listener binding. +SurfingConfiguration is thread-safe as long as your listeners are stateless. ```java JsonSurfer surfer = JsonSurferGson.INSTANCE; SurfingConfiguration config = surfer.configBuilder() @@ -147,7 +152,7 @@ JsonPath object is immutable and can be reused safely. JsonSurfer jsonSurfer = JsonSurferGson.INSTANCE; Object singleResult = jsonSurfer.collectOne(sample, "$.store.book[0]"); ``` -#### Colllect every matched value in a collection +#### Colllect every matched value into a collection ```java JsonSurfer jsonSurfer = JsonSurferGson.INSTANCE; Collection multipleResults = jsonSurfer.collectAll(sample, "$.store.book[*]"); @@ -244,6 +249,11 @@ Java8 user can also convert the iterator into a Stream Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); ``` +#### Non-Blocking parsing +As of 1.4, JsonSurfer support non-blocking parsing for JacksonParser. You can achieve 100% non-blocking JSON processing with JsonSurfer in a NIO application. Let's take a Vertx request handler as an example: +```java +TODO +``` ### Examples Sample Json: From 82248caff58fbf8f2b790af984ce786502b641f7 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Fri, 11 Aug 2017 15:54:57 +0800 Subject: [PATCH 5/7] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3a298c8..5d5077c 100644 --- a/README.md +++ b/README.md @@ -303,7 +303,7 @@ Sample Json: | ```$..author``` | [All authors](#all-authors) | | ```$.store.*``` | [All things in store](#all-things-in-store) | | ```$.store..price``` | [The price of everything in the store](#the-price-of-everything-in-the-store) | -| ```$..book[2]``` | [The thrid book](#the-thrid-book) | +| ```$..book[2]``` | [The third book](#the-third-book) | | ```$..book[0,1]``` | [The first two books](#the-first-two-books) | | ```$.store.book[?(@.price==8.95)]``` | [Filter all books whose price equals to 8.95](#filter-all-books-whose-price-equals-to-8.95) | | ```$.store.book[?(@.category=='fiction')]```             | [Filter all books which belong to fiction category](#filter-all-books-which-belong-to-fiction-category)                   | @@ -395,7 +395,7 @@ Output 22.99 19.95 ``` -#### The thrid book +#### The third book ```javascript $..book[2] ``` From a979dd83037fdc70130a4a6fbbe09c425141a5c5 Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Fri, 11 Aug 2017 17:01:44 +0800 Subject: [PATCH 6/7] Update README.md --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d5077c..f6626df 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,27 @@ Java8 user can also convert the iterator into a Stream #### Non-Blocking parsing As of 1.4, JsonSurfer support non-blocking parsing for JacksonParser. You can achieve 100% non-blocking JSON processing with JsonSurfer in a NIO application. Let's take a Vertx request handler as an example: ```java -TODO + Vertx vertx = Vertx.vertx(); + HttpServer server = vertx.createHttpServer(new HttpServerOptions()); + JsonSurfer surfer = JsonSurferJackson.INSTANCE; + SurfingConfiguration config = surfer.configBuilder() + .bind("$[*]", (JsonPathListener) (value, context) -> { + // Handle json + System.out.println(value); + }).build(); + server.requestHandler(request -> { + NonBlockingParser parser = surfer.createNonBlockingParser(config); + request.handler(buffer -> { + byte[] bytes = buffer.getBytes(); + System.out.println("Received " + bytes.length + " bytes"); + parser.feed(bytes, 0, bytes.length); + }); + request.endHandler(aVoid -> { + parser.endOfInput(); + System.out.println("End of request"); + request.response().end(); + }); + }).listen(8080); ``` ### Examples From 5775b61811e38872f1dfa7ce1b4bc66a68d1a9be Mon Sep 17 00:00:00 2001 From: Leo Wang Date: Sat, 12 Aug 2017 14:23:40 +0800 Subject: [PATCH 7/7] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f6626df..86e0956 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,7 @@ JsonPath object is immutable and can be reused safely. JsonSurfer jsonSurfer = JsonSurferGson.INSTANCE; Collection multipleResults = jsonSurfer.collectAll(sample, "$.store.book[*]"); ``` -#### Filters +#### JsonPath Filters * Filter operators | Operator | Description | @@ -189,7 +189,7 @@ which prints "Leo". ``` which prints "bar". #### Share data among processors -Since JsonSurfer emit data in the way of callback, it may become difficult if one of your processing depends one another. Therefore a simple transient map is added for sharing data among your processors. Following unit test shows how to use it: +Since JsonSurfer emit data in the way of callback, it would be difficult if one of your processing depends one another. Therefore a simple transient map is added for sharing data among your processors. Following unit test shows how to use it: ```java surfer.configBuilder().bind("$.store.book[1]", new JsonPathListener() { @Override @@ -209,7 +209,7 @@ Since JsonSurfer emit data in the way of callback, it may become difficult if on }).buildAndSurf(read("sample.json")); ``` #### Control parsing -* Pause and resume parsing. +* How to pause and resume parsing. ```java SurfingConfiguration config = surfer.configBuilder() .bind("$.store.book[0]", new JsonPathListener() { @@ -239,7 +239,7 @@ Since JsonSurfer emit data in the way of callback, it may become difficult if on ``` * Completely stop parsing. Refer to [Stoppable parsing](#stoppable-parsing) #### Stream support -As of 1.4, JsonSurfer can create an iterator from Json source and JsonPath. Matched value can be pulled from the iterator one by one without loading entire json into memory. +As of 1.4, JsonSurfer can create an iterator from Json and JsonPath. Matched value can be pulled from the iterator one by one without loading entire json into memory. ```java Iterator iterator = surfer.iterator(read("sample.json"), JsonPathCompiler.compile("$.store.book[*]")); ```