diff --git a/jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java b/jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java index 7605a2c..a001adf 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java @@ -26,6 +26,7 @@ import com.google.gson.Gson; import com.google.gson.stream.JsonReader; +import org.jsfr.json.compiler.JsonPathCompiler; import org.jsfr.json.provider.GsonProvider; import org.junit.Before; import org.junit.Test; @@ -72,14 +73,14 @@ public void testLargeJsonRawGson() throws Exception { @Test public void testGsonTypeBindingOne() throws Exception { Reader reader = read("sample.json"); - Book book = surfer.collectOne(reader, Book.class, "$..book[1]"); + Book book = surfer.collectOne(reader, Book.class, JsonPathCompiler.compile("$..book[1]")); assertEquals("Evelyn Waugh", book.getAuthor()); } @Test public void testGsonTypeBindingCollection() throws Exception { Reader reader = read("sample.json"); - Collection book = surfer.collectAll(reader, Book.class, "$..book[0,1]"); + Collection book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]")); assertEquals(2, book.size()); assertEquals("Nigel Rees", book.iterator().next().getAuthor()); } diff --git a/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java b/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java index 1a0020c..65b04de 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java @@ -29,6 +29,7 @@ import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.databind.ObjectMapper; +import org.jsfr.json.compiler.JsonPathCompiler; import org.jsfr.json.provider.JacksonProvider; import org.junit.Before; import org.junit.Test; @@ -75,17 +76,16 @@ public void testLargeJsonJackson() throws Exception { @Test public void testJacksonTypeBindingOne() throws Exception { Reader reader = read("sample.json"); - Book book = surfer.collectOne(reader, Book.class, "$..book[1]"); + Book book = surfer.collectOne(reader, Book.class, JsonPathCompiler.compile("$..book[1]")); assertEquals("Evelyn Waugh", book.getAuthor()); } @Test public void testJacksonTypeBindingCollection() throws Exception { Reader reader = read("sample.json"); - Collection book = surfer.collectAll(reader, Book.class, "$..book[0,1]"); + Collection book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]")); assertEquals(2, book.size()); assertEquals("Nigel Rees", book.iterator().next().getAuthor()); } - } diff --git a/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java b/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java index 0ddd1e1..c2141d9 100644 --- a/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java +++ b/jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java @@ -27,6 +27,7 @@ import com.google.common.io.Resources; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; +import org.jsfr.json.compiler.JsonPathCompiler; import org.jsfr.json.provider.JavaCollectionProvider; import org.jsfr.json.provider.JsonProvider; import org.jsfr.json.provider.JsonSimpleProvider; @@ -446,7 +447,7 @@ public void testCollectOneRaw() throws Exception { @Test public void testCollectAll() throws Exception { - Collection values = surfer.collectAll(read("sample.json"), String.class, "$..book[1,3][author, title]"); + Collection values = surfer.collectAll(read("sample.json"), String.class, JsonPathCompiler.compile("$..book[1,3][author, title]")); assertEquals(4, values.size()); assertEquals("Evelyn Waugh", values.iterator().next()); } @@ -460,7 +461,7 @@ public void testCollectAllFromString() throws Exception { @Test public void testCollectOne() throws Exception { - String value = surfer.collectOne(read("sample.json"), String.class, "$..book[1,3][author,title]"); + String value = surfer.collectOne(read("sample.json"), String.class, JsonPathCompiler.compile("$..book[1,3][author,title]")); assertEquals("Evelyn Waugh", value); } diff --git a/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java b/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java index 6cc587f..9fb60a8 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java @@ -89,19 +89,26 @@ public void surf(Reader reader, SurfingConfiguration configuration) { jsonParserAdapter.parse(reader, new SurfingContext(configuration)); } - public Collection collectAll(String json, JsonPath... paths) { - return collectAll(json, Object.class, paths); + /** + * Collect all matched value into a collection + * + * @param json Json string + * @param paths JsonPath + * @return collection + */ + public Collection collectAll(String json, String... paths) { + return collectAll(json, compile(paths)); } /** * Collect all matched value into a collection * - * @param reader Json reader - * @param paths JsonPath - * @return All matched value + * @param json Json string + * @param paths JsonPath + * @return collection */ - public Collection collectAll(Reader reader, JsonPath... paths) { - return collectAll(reader, Object.class, paths); + public Collection collectAll(String json, JsonPath... paths) { + return collectAll(json, Object.class, paths); } /** @@ -125,30 +132,22 @@ public Collection collectAll(String json, Class tClass, JsonPath... pa * Collect all matched value into a collection * * @param reader Json reader - * @param tClass type * @param paths JsonPath - * @param type - * @return typed value + * @return values */ - public Collection collectAll(Reader reader, Class tClass, JsonPath... paths) { - CollectAllListener listener = new CollectAllListener(jsonProvider, tClass); - SurfingConfiguration.Builder builder = configBuilder(); - for (JsonPath jsonPath : paths) { - builder.bind(jsonPath, listener); - } - surf(reader, builder.build()); - return listener.getCollection(); + public Collection collectAll(Reader reader, String... paths) { + return collectAll(reader, compile(paths)); } /** - * @param json json - * @param tClass target class + * Collect all matched value into a collection + * + * @param reader Json reader * @param paths JsonPath - * @param target class - * @return typed value + * @return All matched value */ - public Collection collectAll(String json, Class tClass, String... paths) { - return collectAll(json, tClass, compile(paths)); + public Collection collectAll(Reader reader, JsonPath... paths) { + return collectAll(reader, Object.class, paths); } /** @@ -160,40 +159,45 @@ public Collection collectAll(String json, Class tClass, String... path * @param type * @return typed value */ - public Collection collectAll(Reader reader, Class tClass, String... paths) { - return collectAll(reader, tClass, compile(paths)); - } - - public Collection collectAll(String json, String... paths) { - return collectAll(json, Object.class, paths); + public Collection collectAll(Reader reader, Class tClass, JsonPath... paths) { + CollectAllListener listener = new CollectAllListener(jsonProvider, tClass); + SurfingConfiguration.Builder builder = configBuilder(); + for (JsonPath jsonPath : paths) { + builder.bind(jsonPath, listener); + } + surf(reader, builder.build()); + return listener.getCollection(); } /** - * Collect all matched value into a collection + * Collect the first matched value and stop parsing immediately * - * @param reader Json reader - * @param paths JsonPath - * @return values + * @param json json + * @param paths JsonPath + * @return value */ - public Collection collectAll(Reader reader, String... paths) { - return collectAll(reader, Object.class, paths); - } - - public Object collectOne(String json, JsonPath... paths) { - return collectOne(json, Object.class, paths); + public Object collectOne(String json, String... paths) { + return collectOne(json, compile(paths)); } /** * Collect the first matched value and stop parsing immediately * - * @param reader json reader - * @param paths JsonPath - * @return Matched value + * @param json json + * @param paths JsonPath + * @return value */ - public Object collectOne(Reader reader, JsonPath... paths) { - return collectOne(reader, Object.class, paths); + public Object collectOne(String json, JsonPath... paths) { + return collectOne(json, Object.class, paths); } + /** + * @param json json + * @param tClass type + * @param paths JsonPath + * @param type + * @return value + */ public T collectOne(String json, Class tClass, JsonPath... paths) { CollectOneListener listener = new CollectOneListener(true); SurfingConfiguration.Builder builder = configBuilder().skipOverlappedPath(); @@ -209,58 +213,43 @@ public T collectOne(String json, Class tClass, JsonPath... paths) { * Collect the first matched value and stop parsing immediately * * @param reader Json reader - * @param tClass type * @param paths JsonPath - * @param type - * @return typed value + * @return Value */ - @SuppressWarnings("unchecked") - public T collectOne(Reader reader, Class tClass, JsonPath... paths) { - CollectOneListener listener = new CollectOneListener(true); - SurfingConfiguration.Builder builder = configBuilder().skipOverlappedPath(); - for (JsonPath jsonPath : paths) { - builder.bind(jsonPath, listener); - } - surf(reader, builder.build()); - Object value = listener.getValue(); - return tClass.cast(jsonProvider.cast(value, tClass)); - } - - public T collectOne(String json, Class tClass, String... paths) { - return collectOne(json, tClass, compile(paths)); + public Object collectOne(Reader reader, String... paths) { + return collectOne(reader, compile(paths)); } /** * Collect the first matched value and stop parsing immediately * - * @param reader Json reader - * @param tClass type + * @param reader json reader * @param paths JsonPath - * @param type - * @return typed value - */ - public T collectOne(Reader reader, Class tClass, String... paths) { - return collectOne(reader, tClass, compile(paths)); - } - - /** - * @param json json - * @param paths JsonPath - * @return value + * @return Matched value */ - public Object collectOne(String json, String... paths) { - return collectOne(json, Object.class, paths); + public Object collectOne(Reader reader, JsonPath... paths) { + return collectOne(reader, Object.class, paths); } /** * Collect the first matched value and stop parsing immediately * * @param reader Json reader + * @param tClass type * @param paths JsonPath - * @return Value + * @param type + * @return typed value */ - public Object collectOne(Reader reader, String... paths) { - return collectOne(reader, Object.class, paths); + @SuppressWarnings("unchecked") + public T collectOne(Reader reader, Class tClass, JsonPath... paths) { + CollectOneListener listener = new CollectOneListener(true); + SurfingConfiguration.Builder builder = configBuilder().skipOverlappedPath(); + for (JsonPath jsonPath : paths) { + builder.bind(jsonPath, listener); + } + surf(reader, builder.build()); + Object value = listener.getValue(); + return tClass.cast(jsonProvider.cast(value, tClass)); } private void ensureSetting(SurfingConfiguration configuration) {