diff --git a/jsurfer-benchmark/pom.xml b/jsurfer-benchmark/pom.xml index 9d9c483..6819abd 100644 --- a/jsurfer-benchmark/pom.xml +++ b/jsurfer-benchmark/pom.xml @@ -5,7 +5,7 @@ jsurfer com.github.jsurfer - 1.2.6 + 1.2.7 4.0.0 diff --git a/jsurfer-core/pom.xml b/jsurfer-core/pom.xml index 4165f5a..c7534b8 100644 --- a/jsurfer-core/pom.xml +++ b/jsurfer-core/pom.xml @@ -5,7 +5,7 @@ jsurfer com.github.jsurfer - 1.2.6 + 1.2.7 4.0.0 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 73f4aac..600d333 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java @@ -92,12 +92,12 @@ public JsonSurfer(JsonParserAdapter jsonParserAdapter, JsonProvider jsonProvider * * @return SurfingConfiguration builder */ - public SurfingConfiguration.Builder builder() { + public SurfingConfiguration.Builder configBuilder() { return SurfingConfiguration.builder().withSurfer(this); } /** - * @param json json + * @param json json * @param configuration SurfingConfiguration that holds JsonPath binding */ public void surf(String json, SurfingConfiguration configuration) { @@ -129,10 +129,10 @@ public Collection collectAll(Reader reader, JsonPath... paths) { } /** - * @param json json + * @param json json * @param tClass target class - * @param paths JsonPath - * @param target class + * @param paths JsonPath + * @param target class * @return typed value */ public Collection collectAll(String json, Class tClass, JsonPath... paths) { @@ -150,7 +150,7 @@ public Collection collectAll(String json, Class tClass, JsonPath... pa */ public Collection collectAll(Reader reader, Class tClass, JsonPath... paths) { CollectAllListener listener = new CollectAllListener(jsonProvider, tClass); - SurfingConfiguration.Builder builder = builder(); + SurfingConfiguration.Builder builder = configBuilder(); for (JsonPath jsonPath : paths) { builder.bind(jsonPath, listener); } @@ -159,10 +159,10 @@ public Collection collectAll(Reader reader, Class tClass, JsonPath... } /** - * @param json json + * @param json json * @param tClass target class - * @param paths JsonPath - * @param target class + * @param paths JsonPath + * @param target class * @return typed value */ public Collection collectAll(String json, Class tClass, String... paths) { @@ -228,7 +228,7 @@ public T collectOne(String json, Class tClass, JsonPath... paths) { @SuppressWarnings("unchecked") public T collectOne(Reader reader, Class tClass, JsonPath... paths) { CollectOneListener listener = new CollectOneListener(true); - SurfingConfiguration.Builder builder = builder().skipOverlappedPath(); + SurfingConfiguration.Builder builder = configBuilder().skipOverlappedPath(); for (JsonPath jsonPath : paths) { builder.bind(jsonPath, listener); } @@ -255,7 +255,7 @@ public T collectOne(Reader reader, Class tClass, String... paths) { } /** - * @param json json + * @param json json * @param paths JsonPath * @return value */ diff --git a/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java b/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java index 81adc9f..c14176b 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java @@ -100,6 +100,7 @@ public SurfingConfiguration build() { /** * Associated with a JsonSurfer + * * @param jsonSurfer JsonSurfer * @return builder */ @@ -110,17 +111,19 @@ public Builder withSurfer(JsonSurfer jsonSurfer) { /** * Build the configuration and then surf with it and the associated JsonSurfer + * * @param json json */ - public void surf(String json) { - this.surf(this.jsonSurfer.read(json)); + public void buildAndSurf(String json) { + this.buildAndSurf(this.jsonSurfer.read(json)); } /** * Build the configuration and then surf with it and the associated JsonSurfer + * * @param jsonReader jsonReader */ - public void surf(Reader jsonReader) { + public void buildAndSurf(Reader jsonReader) { this.jsonSurfer.surf(jsonReader, this.build()); } diff --git a/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java b/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java index b4f49c6..13cc652 100644 --- a/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java +++ b/jsurfer-core/src/main/java/org/jsfr/json/SurfingContext.java @@ -160,6 +160,7 @@ public boolean endObject() { if (stopped) { return false; } + this.currentKey = null; currentPosition.stepOutObject(); dispatcher.endObject(); return true; diff --git a/jsurfer-core/src/test/java/org/jsfr/json/JsonSurferTest.java b/jsurfer-core/src/test/java/org/jsfr/json/JsonSurferTest.java index e28f648..76b1c8d 100644 --- a/jsurfer-core/src/test/java/org/jsfr/json/JsonSurferTest.java +++ b/jsurfer-core/src/test/java/org/jsfr/json/JsonSurferTest.java @@ -42,6 +42,7 @@ import java.util.Iterator; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.argThat; @@ -74,11 +75,11 @@ public void setUp() throws Exception { @Test public void testSampleJson() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$.store.book[0].category", mockListener) + surfer.configBuilder().bind("$.store.book[0].category", mockListener) .bind("$.store.book[0]", mockListener) .bind("$.store.car", mockListener) .bind("$.store.bicycle", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); Object book = provider.createObject(); provider.put(book, "category", provider.primitive("reference")); @@ -103,9 +104,9 @@ public void testSampleJson() throws Exception { @Test public void testSample2() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder() + surfer.configBuilder() .bind("$[0].aiRuleEditorOriginal.+.barrierLevel", mockListener) - .surf(read("sample2.json")); + .buildAndSurf(read("sample2.json")); verify(mockListener).onValue(eq(provider.primitive("0.8065")), any(ParsingContext.class)); } @@ -126,10 +127,10 @@ public void describeTo(Description description) { } })); - surfer.builder() + surfer.configBuilder() .bind("$.store.book[0,1,2]", mockListener) .bind("$.store.book[3]", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mockListener, times(1)) .onValue(anyObject(), any(ParsingContext.class)); @@ -138,9 +139,9 @@ public void describeTo(Description description) { @Test public void testChildNodeWildcard() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder() + surfer.configBuilder() .bind("$.store.*", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mockListener, times(3)) .onValue(anyObject(), any(ParsingContext.class)); } @@ -148,9 +149,9 @@ public void testChildNodeWildcard() throws Exception { @Test public void testAnyIndex() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder() + surfer.configBuilder() .bind("$.store.book[*]", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mockListener, times(4)) .onValue(anyObject(), any(ParsingContext.class)); } @@ -162,8 +163,8 @@ private String read(String resourceName) throws IOException { @Test public void testWildcardCombination() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$.store.book[*].*", mockListener) - .surf(read("sample.json")); + surfer.configBuilder().bind("$.store.book[*].*", mockListener) + .buildAndSurf(read("sample.json")); verify(mockListener, times(18)).onValue(anyObject(), any(ParsingContext.class)); } @@ -174,12 +175,12 @@ public void testArraySlicing() throws Exception { JsonPathListener mock2 = mock(JsonPathListener.class); JsonPathListener mock3 = mock(JsonPathListener.class); JsonPathListener mock4 = mock(JsonPathListener.class); - surfer.builder() + surfer.configBuilder() .bind("$[:2]", mock1) .bind("$[0:2]", mock2) .bind("$[2:]", mock3) .bind("$[:]", mock4) - .surf(read("array.json")); + .buildAndSurf(read("array.json")); verify(mock1, times(2)).onValue(anyObject(), any(ParsingContext.class)); verify(mock2, times(2)).onValue(anyObject(), any(ParsingContext.class)); verify(mock3, times(3)).onValue(anyObject(), any(ParsingContext.class)); @@ -195,14 +196,14 @@ public void testParsingArray() throws Exception { JsonPathListener nullElement = mock(JsonPathListener.class); JsonPathListener objectElement = mock(JsonPathListener.class); - surfer.builder().bind("$", wholeArray) + surfer.configBuilder().bind("$", wholeArray) .bind("$[0]", stringElement) .bind("$[1]", numberElement) .bind("$[2]", booleanElement) .bind("$[3]", nullElement) .bind("$[4]", objectElement) - .surf(read("array.json")); - + .buildAndSurf(read("array.json")); + Object object = provider.createObject(); provider.put(object, "key", provider.primitive("value")); Object array = provider.createArray(); @@ -223,9 +224,9 @@ public void testParsingArray() throws Exception { @Test public void testDeepScan() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$..author", mockListener) + surfer.configBuilder().bind("$..author", mockListener) .bind("$..store..bicycle..color", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mockListener).onValue(eq(provider.primitive("Nigel Rees")), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive("Evelyn Waugh")), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive("Herman Melville")), any(ParsingContext.class)); @@ -237,8 +238,8 @@ public void testDeepScan() throws Exception { @Test public void testDeepScan2() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$..store..price", mockListener) - .surf(read("sample.json")); + surfer.configBuilder().bind("$..store..price", mockListener) + .buildAndSurf(read("sample.json")); verify(mockListener).onValue(eq(provider.primitive(8.95)), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive(12.99)), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive(8.99)), any(ParsingContext.class)); @@ -249,29 +250,29 @@ public void testDeepScan2() throws Exception { @Test public void testAny() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$.store..bicycle..*", mockListener) - .surf(read("sample.json")); + surfer.configBuilder().bind("$.store..bicycle..*", mockListener) + .buildAndSurf(read("sample.json")); verify(mockListener).onValue(eq(provider.primitive("red")), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive(19.95)), any(ParsingContext.class)); } @Test public void testFindEverything() throws Exception { - surfer.builder() + surfer.configBuilder() .bind("$..*", new JsonPathListener() { @Override public void onValue(Object value, ParsingContext context) { LOGGER.trace("value: {}", value); } }) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); } @Test public void testIndexesAndChildrenOperator() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().bind("$..book[1,3][author,title]", mockListener) - .surf(read("sample.json")); + surfer.configBuilder().bind("$..book[1,3][author,title]", mockListener) + .buildAndSurf(read("sample.json")); verify(mockListener).onValue(eq(provider.primitive("Evelyn Waugh")), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive("Sword of Honour")), any(ParsingContext.class)); verify(mockListener).onValue(eq(provider.primitive("J. R. R. Tolkien")), any(ParsingContext.class)); @@ -308,83 +309,97 @@ public void testCollectOne() throws Exception { @Test public void testGetCurrentFieldName() throws Exception { - surfer.builder() + surfer.configBuilder() .bind("$.store.book[0].title", new JsonPathListener() { @Override public void onValue(Object value, ParsingContext context) throws Exception { assertEquals(context.getCurrentFieldName(), "title"); } }) - .surf(read("sample.json")); + .bind("$.store.book[0]", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) throws Exception { + assertNull(context.getCurrentFieldName()); + } + }) + .buildAndSurf(read("sample.json")); } @Test public void testGetCurrentArrayIndex() throws Exception { - surfer.builder().bind("$.store.book[3]", new JsonPathListener() { - @Override - public void onValue(Object value, ParsingContext context) throws Exception { - assertEquals(context.getCurrentArrayIndex(), 3); - } - }).surf(read("sample.json")); + surfer.configBuilder() + .bind("$.store.book[3]", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) throws Exception { + assertEquals(context.getCurrentArrayIndex(), 3); + } + }) + .bind("$.store", new JsonPathListener() { + @Override + public void onValue(Object value, ParsingContext context) throws Exception { + assertEquals(context.getCurrentArrayIndex(), -1); + } + }) + .buildAndSurf(read("sample.json")); } @Test public void testExample1() throws Exception { - surfer.builder().bind("$.store.book[*].author", print).surf(read("sample.json")); + surfer.configBuilder().bind("$.store.book[*].author", print).buildAndSurf(read("sample.json")); } @Test public void testExample2() throws Exception { - surfer.builder().bind("$..author", print).surf(read("sample.json")); + surfer.configBuilder().bind("$..author", print).buildAndSurf(read("sample.json")); } @Test public void testExample3() throws Exception { - surfer.builder().bind("$.store.*", print).surf(read("sample.json")); + surfer.configBuilder().bind("$.store.*", print).buildAndSurf(read("sample.json")); } @Test public void testExample4() throws Exception { - surfer.builder().bind("$.store..price", print).surf(read("sample.json")); + surfer.configBuilder().bind("$.store..price", print).buildAndSurf(read("sample.json")); } @Test public void testExample5() throws Exception { - surfer.builder().bind("$..book[2]", print).surf(read("sample.json")); + surfer.configBuilder().bind("$..book[2]", print).buildAndSurf(read("sample.json")); } @Test public void testExample6() throws Exception { - surfer.builder().bind("$..book[0,1]", print).surf(read("sample.json")); + surfer.configBuilder().bind("$..book[0,1]", print).buildAndSurf(read("sample.json")); } @Test public void testStoppable() throws Exception { - surfer.builder().bind("$..book[0,1]", new JsonPathListener() { + surfer.configBuilder().bind("$..book[0,1]", new JsonPathListener() { @Override public void onValue(Object value, ParsingContext parsingContext) { parsingContext.stopParsing(); System.out.println(value); } - }).surf(read("sample.json")); + }).buildAndSurf(read("sample.json")); } @Test public void testPlugableProvider() throws Exception { JsonPathListener mockListener = mock(JsonPathListener.class); - surfer.builder().withJsonProvider(JavaCollectionProvider.INSTANCE) + surfer.configBuilder().withJsonProvider(JavaCollectionProvider.INSTANCE) .bind("$.store", mockListener) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mockListener).onValue(isA(HashMap.class), any(ParsingContext.class)); } @Test public void testErrorStrategySuppressException() throws Exception { - + JsonPathListener mock = mock(JsonPathListener.class); doNothing().doThrow(Exception.class).doThrow(Exception.class).when(mock).onValue(anyObject(), any(ParsingContext.class)); - surfer.builder().bind("$.store.book[*]", mock) + surfer.configBuilder().bind("$.store.book[*]", mock) .withErrorStrategy(new ErrorHandlingStrategy() { @Override public void handleParsingException(Exception e) { @@ -396,17 +411,17 @@ public void handleExceptionFromListener(Exception e, ParsingContext context) { // suppress exception } }) - .surf(read("sample.json")); + .buildAndSurf(read("sample.json")); verify(mock, times(4)).onValue(anyObject(), any(ParsingContext.class)); } @Test public void testErrorStrategyThrowException() throws Exception { - + JsonPathListener mock = mock(JsonPathListener.class); doNothing().doThrow(Exception.class).doThrow(Exception.class).when(mock).onValue(anyObject(), any(ParsingContext.class)); try { - surfer.builder().bind("$.store.book[*]", mock).surf(read("sample.json")); + surfer.configBuilder().bind("$.store.book[*]", mock).buildAndSurf(read("sample.json")); } catch (Exception e) { // catch mock exception } diff --git a/pom.xml b/pom.xml index ca9d829..922009e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.github.jsurfer jsurfer - 1.2.6 + 1.2.7 pom JsonSurfer Let's surf on json