From dd23be25e20bdb5e12673ce91f46b311ae39ba41 Mon Sep 17 00:00:00 2001 From: lwang Date: Tue, 31 Mar 2015 01:06:49 +0800 Subject: [PATCH] 1. New API: collectAll and collectOne 2. Refactor source structure --- .../jsfr/json/{parse => }/GsonProvider.java | 14 ++- .../main/java/org/jsfr/json/GsonSurfer.java | 25 ++-- .../{ => org/jsfr/json}/GsonSurferTest.java | 10 +- .../json/{parse => }/JacksonProvider.java | 2 +- .../java/org/jsfr/json/JacksonSurfer.java | 28 +++-- .../jsfr/json}/JacksonSurferTest.java | 6 +- .../java/org/jsfr/json/AbstractSurfer.java | 75 ++++++++++++ .../java/org/jsfr/json/BuilderFactory.java | 1 - .../org/jsfr/json/CollectAllListener.java | 46 +++++++ .../org/jsfr/json/CollectOneListener.java | 44 +++++++ .../json/{parse => }/ContentDispatcher.java | 4 +- .../{parse => }/JavaCollectionProvider.java | 25 ++-- .../jsfr/json/{parse => }/JsonCollector.java | 4 +- .../jsfr/json/{parse => }/JsonGenerator.java | 2 +- .../json/{parse => }/JsonPathListener.java | 2 +- .../jsfr/json/{parse => }/JsonPosition.java | 12 +- .../jsfr/json/{parse => }/JsonProvider.java | 2 +- .../json/{parse => }/JsonSimpleProvider.java | 2 +- .../java/org/jsfr/json/JsonSimpleSurfer.java | 14 ++- .../main/java/org/jsfr/json/JsonSurfer.java | 7 +- .../jsfr/json/{parse => }/ParsingContext.java | 2 +- .../jsfr/json/{parse => }/SurfingContext.java | 26 ++-- .../java/org/jsfr/json/TypedListener.java | 39 ++++++ .../java/org/jsfr/json/path/ArrayIndexes.java | 2 +- .../java/org/jsfr/json/path/ChildrenNode.java | 2 +- .../java/org/jsfr/json/path/JsonPath.java | 5 +- .../{ => org/jsfr/json}/JsonSurferTest.java | 112 +++++++++++------- 27 files changed, 389 insertions(+), 124 deletions(-) rename jsurfer-gson/src/main/java/org/jsfr/json/{parse => }/GsonProvider.java (89%) rename jsurfer-gson/src/test/java/{ => org/jsfr/json}/GsonSurferTest.java (88%) rename jsurfer-jackson/src/main/java/org/jsfr/json/{parse => }/JacksonProvider.java (98%) rename jsurfer-jackson/src/test/java/{ => org/jsfr/json}/JacksonSurferTest.java (95%) create mode 100644 jsurfer-simple/src/main/java/org/jsfr/json/AbstractSurfer.java create mode 100644 jsurfer-simple/src/main/java/org/jsfr/json/CollectAllListener.java create mode 100644 jsurfer-simple/src/main/java/org/jsfr/json/CollectOneListener.java rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/ContentDispatcher.java (98%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JavaCollectionProvider.java (85%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonCollector.java (97%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonGenerator.java (99%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonPathListener.java (97%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonPosition.java (92%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonProvider.java (98%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/JsonSimpleProvider.java (98%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/ParsingContext.java (97%) rename jsurfer-simple/src/main/java/org/jsfr/json/{parse => }/SurfingContext.java (96%) create mode 100644 jsurfer-simple/src/main/java/org/jsfr/json/TypedListener.java rename jsurfer-simple/src/test/java/{ => org/jsfr/json}/JsonSurferTest.java (82%) diff --git a/jsurfer-gson/src/main/java/org/jsfr/json/parse/GsonProvider.java b/jsurfer-gson/src/main/java/org/jsfr/json/GsonProvider.java similarity index 89% rename from jsurfer-gson/src/main/java/org/jsfr/json/parse/GsonProvider.java rename to jsurfer-gson/src/main/java/org/jsfr/json/GsonProvider.java index 5e0cf0d..27c2d5e 100644 --- a/jsurfer-gson/src/main/java/org/jsfr/json/parse/GsonProvider.java +++ b/jsurfer-gson/src/main/java/org/jsfr/json/GsonProvider.java @@ -22,17 +22,19 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; -import com.google.gson.*; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; /** * Created by Administrator on 2015/3/25. */ public class GsonProvider implements JsonProvider { - private final static Gson DEFAULT_GSON = new Gson(); - @Override public JsonObject createObject() { return new JsonObject(); @@ -65,7 +67,7 @@ public void consumeArrayElement(JsonArray array, JsonElement value) { @Override public JsonElement primitive(boolean value) { - return new JsonPrimitive(value); + return new JsonPrimitive(value); } @Override @@ -75,7 +77,7 @@ public JsonElement primitive(int value) { @Override public JsonElement primitive(double value) { - return new JsonPrimitive(value); + return new JsonPrimitive(value); } @Override diff --git a/jsurfer-gson/src/main/java/org/jsfr/json/GsonSurfer.java b/jsurfer-gson/src/main/java/org/jsfr/json/GsonSurfer.java index 678a815..b1b8911 100644 --- a/jsurfer-gson/src/main/java/org/jsfr/json/GsonSurfer.java +++ b/jsurfer-gson/src/main/java/org/jsfr/json/GsonSurfer.java @@ -26,16 +26,14 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; -import org.jsfr.json.parse.SurfingContext; -import org.json.simple.parser.ParseException; import org.jsfr.json.exception.JsonSurfingException; -import org.jsfr.json.parse.JsonProvider; +import org.json.simple.parser.ParseException; import java.io.IOException; import java.io.Reader; import java.util.Stack; -public class GsonSurfer implements JsonSurfer { +public class GsonSurfer extends AbstractSurfer { private enum EntryType { ROOT, @@ -44,12 +42,19 @@ private enum EntryType { PRIMITIVE } - // TODO Implement gson parsing context + public GsonSurfer() { + super(new GsonProvider()); + } + + + public GsonSurfer(JsonProvider jsonProvider) { + super(jsonProvider); + } @Override public void surf(Reader reader, SurfingContext context) { + ensureJsonProvider(context); try { - JsonProvider provider = context.getJsonProvider(); JsonReader jsonReader = new JsonReader(reader); Stack entryStack = new Stack(); entryStack.push(EntryType.ROOT); @@ -112,7 +117,7 @@ public void surf(Reader reader, SurfingContext context) { break; case STRING: String s = jsonReader.nextString(); - if (!context.primitive(provider.primitive(s))) { + if (!context.primitive(jsonProvider.primitive(s))) { return; } if (entryStack.peek() == EntryType.PRIMITIVE) { @@ -124,7 +129,7 @@ public void surf(Reader reader, SurfingContext context) { break; case NUMBER: double n = jsonReader.nextDouble(); - if (!context.primitive(provider.primitive(n))) { + if (!context.primitive(jsonProvider.primitive(n))) { return; } if (entryStack.peek() == EntryType.PRIMITIVE) { @@ -136,7 +141,7 @@ public void surf(Reader reader, SurfingContext context) { break; case BOOLEAN: boolean b = jsonReader.nextBoolean(); - if (!context.primitive(provider.primitive(b))) { + if (!context.primitive(jsonProvider.primitive(b))) { return; } if (entryStack.peek() == EntryType.PRIMITIVE) { @@ -148,7 +153,7 @@ public void surf(Reader reader, SurfingContext context) { break; case NULL: jsonReader.nextNull(); - if (!context.primitive(provider.primitiveNull())) { + if (!context.primitive(jsonProvider.primitiveNull())) { return; } if (entryStack.peek() == EntryType.PRIMITIVE) { diff --git a/jsurfer-gson/src/test/java/GsonSurferTest.java b/jsurfer-gson/src/test/java/org/jsfr/json/GsonSurferTest.java similarity index 88% rename from jsurfer-gson/src/test/java/GsonSurferTest.java rename to jsurfer-gson/src/test/java/org/jsfr/json/GsonSurferTest.java index a17b6eb..78f27dd 100644 --- a/jsurfer-gson/src/test/java/GsonSurferTest.java +++ b/jsurfer-gson/src/test/java/org/jsfr/json/GsonSurferTest.java @@ -22,13 +22,13 @@ * THE SOFTWARE. */ +package org.jsfr.json; + import com.google.common.io.Resources; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import org.junit.Before; import org.junit.Test; -import org.jsfr.json.GsonSurfer; -import org.jsfr.json.parse.GsonProvider; import java.io.InputStreamReader; import java.util.Map; @@ -42,8 +42,8 @@ public class GsonSurferTest extends JsonSurferTest { @Override @Before public void setUp() throws Exception { - surfer = new GsonSurfer(); provider = new GsonProvider(); + surfer = new GsonSurfer(provider); } @Test @@ -61,9 +61,9 @@ public void testLargeJsonRawGson() throws Exception { Map element = gson.fromJson(reader, Map.class); Object value = ((Map) element.get("properties")).get("branch"); counter.incrementAndGet(); - LOGGER.trace("JsonPath: {} value: {}", reader.getPath(), value); + JsonSurferTest.LOGGER.trace("JsonPath: {} value: {}", reader.getPath(), value); } - LOGGER.info("Gson processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - start); + JsonSurferTest.LOGGER.info("Gson processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - start); } } diff --git a/jsurfer-jackson/src/main/java/org/jsfr/json/parse/JacksonProvider.java b/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonProvider.java similarity index 98% rename from jsurfer-jackson/src/main/java/org/jsfr/json/parse/JacksonProvider.java rename to jsurfer-jackson/src/main/java/org/jsfr/json/JacksonProvider.java index 88867e1..303079c 100644 --- a/jsurfer-jackson/src/main/java/org/jsfr/json/parse/JacksonProvider.java +++ b/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonProvider.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; diff --git a/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonSurfer.java b/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonSurfer.java index b4bce2e..2e684b9 100644 --- a/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonSurfer.java +++ b/jsurfer-jackson/src/main/java/org/jsfr/json/JacksonSurfer.java @@ -27,10 +27,8 @@ import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; -import org.jsfr.json.parse.SurfingContext; -import org.json.simple.parser.ParseException; import org.jsfr.json.exception.JsonSurfingException; -import org.jsfr.json.parse.JsonProvider; +import org.json.simple.parser.ParseException; import java.io.IOException; import java.io.Reader; @@ -38,12 +36,20 @@ /** * Created by Leo on 2015/3/29. */ -public class JacksonSurfer implements JsonSurfer { +public class JacksonSurfer extends AbstractSurfer { + + public JacksonSurfer() { + super(new JacksonProvider()); + } + + public JacksonSurfer(JsonProvider jsonProvider) { + super(jsonProvider); + } @Override public void surf(Reader reader, SurfingContext context) { + ensureJsonProvider(context); try { - JsonProvider provider = context.getJsonProvider(); JsonFactory f = new JsonFactory(); JsonParser jp = f.createParser(reader); context.startJSON(); @@ -94,7 +100,7 @@ public void surf(Reader reader, SurfingContext context) { case VALUE_EMBEDDED_OBJECT: throw new IllegalStateException("Unexpected token"); case VALUE_STRING: - if (!context.primitive(provider.primitive(jp.getText()))) { + if (!context.primitive(jsonProvider.primitive(jp.getText()))) { return; } if (jp.getCurrentName() != null) { @@ -104,7 +110,7 @@ public void surf(Reader reader, SurfingContext context) { } break; case VALUE_NUMBER_INT: - if (!context.primitive(provider.primitive(jp.getIntValue()))) { + if (!context.primitive(jsonProvider.primitive(jp.getIntValue()))) { return; } if (jp.getCurrentName() != null) { @@ -114,7 +120,7 @@ public void surf(Reader reader, SurfingContext context) { } break; case VALUE_NUMBER_FLOAT: - if (!context.primitive(provider.primitive(jp.getDoubleValue()))) { + if (!context.primitive(jsonProvider.primitive(jp.getDoubleValue()))) { return; } if (jp.getCurrentName() != null) { @@ -124,7 +130,7 @@ public void surf(Reader reader, SurfingContext context) { } break; case VALUE_TRUE: - if (!context.primitive(provider.primitive(true))) { + if (!context.primitive(jsonProvider.primitive(true))) { return; } if (jp.getCurrentName() != null) { @@ -134,7 +140,7 @@ public void surf(Reader reader, SurfingContext context) { } break; case VALUE_FALSE: - if (!context.primitive(provider.primitive(false))) { + if (!context.primitive(jsonProvider.primitive(false))) { return; } if (jp.getCurrentName() != null) { @@ -144,7 +150,7 @@ public void surf(Reader reader, SurfingContext context) { } break; case VALUE_NULL: - if (!context.primitive(provider.primitiveNull())) { + if (!context.primitive(jsonProvider.primitiveNull())) { return; } if (jp.getCurrentName() != null) { diff --git a/jsurfer-jackson/src/test/java/JacksonSurferTest.java b/jsurfer-jackson/src/test/java/org/jsfr/json/JacksonSurferTest.java similarity index 95% rename from jsurfer-jackson/src/test/java/JacksonSurferTest.java rename to jsurfer-jackson/src/test/java/org/jsfr/json/JacksonSurferTest.java index 48cb280..c5e043a 100644 --- a/jsurfer-jackson/src/test/java/JacksonSurferTest.java +++ b/jsurfer-jackson/src/test/java/org/jsfr/json/JacksonSurferTest.java @@ -22,6 +22,8 @@ * THE SOFTWARE. */ +package org.jsfr.json; + import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; @@ -30,8 +32,6 @@ import com.google.common.io.Resources; import org.junit.Before; import org.junit.Test; -import org.jsfr.json.JacksonSurfer; -import org.jsfr.json.parse.JacksonProvider; import java.util.concurrent.atomic.AtomicLong; @@ -43,8 +43,8 @@ public class JacksonSurferTest extends JsonSurferTest { @Override @Before public void setUp() throws Exception { - surfer = new JacksonSurfer(); provider = new JacksonProvider(); + surfer = new JacksonSurfer(provider); } @Test diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/AbstractSurfer.java b/jsurfer-simple/src/main/java/org/jsfr/json/AbstractSurfer.java new file mode 100644 index 0000000..48978a2 --- /dev/null +++ b/jsurfer-simple/src/main/java/org/jsfr/json/AbstractSurfer.java @@ -0,0 +1,75 @@ +/* + * The MIT License + * + * Copyright (c) 2015 WANG Lingsong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jsfr.json; + +import org.jsfr.json.SurfingContext.Builder; +import org.jsfr.json.path.JsonPath; + +import java.io.Reader; +import java.util.Collection; + +import static org.jsfr.json.SurfingContext.Builder.builder; + + +/** + * Created by Leo on 2015/3/30. + */ +public abstract class AbstractSurfer implements JsonSurfer { + + protected JsonProvider jsonProvider; + + public AbstractSurfer(JsonProvider jsonProvider) { + this.jsonProvider = jsonProvider; + } + + @Override + public Collection collect(Reader reader, JsonPath... paths) { + CollectAllListener listener = new CollectAllListener(); + Builder builder = builder().withJsonProvider(jsonProvider); + for (JsonPath jsonPath : paths) { + builder.bind(jsonPath, listener); + } + surf(reader, builder.build()); + return listener.getCollection(); + } + + @Override + public T collectOne(Reader reader, JsonPath... paths) { + CollectOneListener listener = new CollectOneListener(); + Builder builder = builder().withJsonProvider(jsonProvider); + for (JsonPath jsonPath : paths) { + builder.bind(jsonPath, listener); + } + surf(reader, builder.build()); + return listener.getValue(); + } + + protected void ensureJsonProvider(SurfingContext context) { + if (context.getJsonProvider() == null) { + context.setJsonProvider(jsonProvider); + } + } + +} diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/BuilderFactory.java b/jsurfer-simple/src/main/java/org/jsfr/json/BuilderFactory.java index 413fc64..dfca0a3 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/BuilderFactory.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/BuilderFactory.java @@ -24,7 +24,6 @@ package org.jsfr.json; -import org.jsfr.json.parse.SurfingContext; import org.jsfr.json.path.JsonPath; public class BuilderFactory { diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/CollectAllListener.java b/jsurfer-simple/src/main/java/org/jsfr/json/CollectAllListener.java new file mode 100644 index 0000000..6ecc203 --- /dev/null +++ b/jsurfer-simple/src/main/java/org/jsfr/json/CollectAllListener.java @@ -0,0 +1,46 @@ +/* + * The MIT License + * + * Copyright (c) 2015 WANG Lingsong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jsfr.json; + +import java.util.ArrayList; +import java.util.Collection; + +/** + * Created by Leo on 2015/3/30. + */ +public class CollectAllListener extends TypedListener { + + private Collection collection = new ArrayList(); + + @Override + public void onTypedValue(T value, ParsingContext context) { + collection.add(value); + } + + public Collection getCollection() { + return collection; + } + +} diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/CollectOneListener.java b/jsurfer-simple/src/main/java/org/jsfr/json/CollectOneListener.java new file mode 100644 index 0000000..a50e72d --- /dev/null +++ b/jsurfer-simple/src/main/java/org/jsfr/json/CollectOneListener.java @@ -0,0 +1,44 @@ +/* + * The MIT License + * + * Copyright (c) 2015 WANG Lingsong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jsfr.json; + +/** + * Created by Leo on 2015/3/30. + */ +public class CollectOneListener extends TypedListener { + + private T value; + + @Override + public void onTypedValue(T value, ParsingContext context) { + this.value = value; + context.stopParsing(); + } + + public T getValue() { + return value; + } + +} diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/ContentDispatcher.java b/jsurfer-simple/src/main/java/org/jsfr/json/ContentDispatcher.java similarity index 98% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/ContentDispatcher.java rename to jsurfer-simple/src/main/java/org/jsfr/json/ContentDispatcher.java index c5b596c..3396cc0 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/ContentDispatcher.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/ContentDispatcher.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import org.json.simple.parser.ContentHandler; import org.json.simple.parser.ParseException; @@ -34,7 +34,7 @@ /** * Created by Administrator on 2015/3/21. */ -public class ContentDispatcher implements ContentHandler { +class ContentDispatcher implements ContentHandler { LinkedList receiver = new LinkedList(); diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JavaCollectionProvider.java b/jsurfer-simple/src/main/java/org/jsfr/json/JavaCollectionProvider.java similarity index 85% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JavaCollectionProvider.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JavaCollectionProvider.java index d355bed..f501914 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JavaCollectionProvider.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JavaCollectionProvider.java @@ -22,64 +22,65 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import java.util.ArrayList; +import java.util.HashMap; import java.util.Map; /** * Created by Leo on 2015/3/27. */ public class JavaCollectionProvider implements JsonProvider, ArrayList, Object> { - // TODO + @Override public Map createObject() { - return null; + return new HashMap(); } @Override public ArrayList createArray() { - return null; + return new ArrayList(); } @Override public boolean isObject(Object object) { - return false; + return object instanceof HashMap; } @Override public boolean isArray(Object array) { - return false; + return array instanceof ArrayList; } @Override public void consumeObjectEntry(Map object, String key, Object value) { - + object.put(key, value); } @Override public void consumeArrayElement(ArrayList array, Object value) { - + array.add(value); } @Override public Object primitive(boolean value) { - return null; + return value; } @Override public Object primitive(int value) { - return null; + return value; } @Override public Object primitive(double value) { - return null; + return value; } @Override public Object primitive(String value) { - return null; + return value; } @Override diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonCollector.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonCollector.java similarity index 97% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonCollector.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonCollector.java index 43ccfec..5c06c7e 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonCollector.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonCollector.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import org.json.simple.parser.ParseException; @@ -32,7 +32,7 @@ /** * Created by Administrator on 2015/3/21. */ -public class JsonCollector extends JsonGenerator { +class JsonCollector extends JsonGenerator { private Collection jsonPathListeners; private ParsingContext context; diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonGenerator.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonGenerator.java similarity index 99% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonGenerator.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonGenerator.java index 95313d2..3af2ca6 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonGenerator.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonGenerator.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import org.json.simple.parser.ContentHandler; import org.json.simple.parser.ParseException; diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPathListener.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonPathListener.java similarity index 97% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPathListener.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonPathListener.java index fcf14e1..682025a 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPathListener.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonPathListener.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; public interface JsonPathListener { diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPosition.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonPosition.java similarity index 92% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPosition.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonPosition.java index 10df896..4b11975 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonPosition.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonPosition.java @@ -22,9 +22,13 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; -import org.jsfr.json.path.*; +import org.jsfr.json.path.ArrayIndex; +import org.jsfr.json.path.ChildNode; +import org.jsfr.json.path.JsonPath; +import org.jsfr.json.path.PathOperator; +import org.jsfr.json.path.Root; import java.lang.ref.SoftReference; import java.util.Stack; @@ -64,7 +68,7 @@ void stepOut() { } } - void stepInArray() { + void stepOverNextIndex() { Stack stack = arrayNodeCache.get(); ArrayIndex node = null; if (stack != null && !stack.isEmpty()) { @@ -77,7 +81,7 @@ void stepInArray() { operators.push(node); } - void stepInObject(String key) { + void stepIntoChild(String key) { Stack stack = childNodeCache.get(); ChildNode node = null; if (stack != null && !stack.isEmpty()) { diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonProvider.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonProvider.java similarity index 98% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonProvider.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonProvider.java index 1176f4d..ea3d354 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonProvider.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonProvider.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; /** * Created by Administrator on 2015/3/25. diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonSimpleProvider.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleProvider.java similarity index 98% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonSimpleProvider.java rename to jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleProvider.java index 9dd13bd..19e92ee 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/JsonSimpleProvider.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleProvider.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; import org.json.simple.JSONArray; import org.json.simple.JSONObject; diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleSurfer.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleSurfer.java index 80fc010..85bcef5 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleSurfer.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSimpleSurfer.java @@ -24,18 +24,26 @@ package org.jsfr.json; -import org.jsfr.json.parse.SurfingContext; +import org.jsfr.json.exception.JsonSurfingException; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; -import org.jsfr.json.exception.JsonSurfingException; import java.io.IOException; import java.io.Reader; -public class JsonSimpleSurfer implements JsonSurfer { +public class JsonSimpleSurfer extends AbstractSurfer { + + public JsonSimpleSurfer() { + super(new JsonSimpleProvider()); + } + + public JsonSimpleSurfer(JsonProvider jsonProvider) { + super(jsonProvider); + } @Override public void surf(Reader reader, SurfingContext context) { + ensureJsonProvider(context); JSONParser parser = new JSONParser(); try { parser.parse(reader, context); diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/JsonSurfer.java b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSurfer.java index 6460be3..690e67a 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/JsonSurfer.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/JsonSurfer.java @@ -24,9 +24,10 @@ package org.jsfr.json; -import org.jsfr.json.parse.SurfingContext; +import org.jsfr.json.path.JsonPath; import java.io.Reader; +import java.util.Collection; /** * Created by Administrator on 2015/3/23. @@ -35,4 +36,8 @@ public interface JsonSurfer { void surf(Reader reader, SurfingContext context); + Collection collect(Reader reader, JsonPath... paths); + + T collectOne(Reader reader, JsonPath... paths); + } diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/ParsingContext.java b/jsurfer-simple/src/main/java/org/jsfr/json/ParsingContext.java similarity index 97% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/ParsingContext.java rename to jsurfer-simple/src/main/java/org/jsfr/json/ParsingContext.java index a68f5af..fe29763 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/ParsingContext.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/ParsingContext.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; /** * Created by Leo on 2015/3/22. diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/parse/SurfingContext.java b/jsurfer-simple/src/main/java/org/jsfr/json/SurfingContext.java similarity index 96% rename from jsurfer-simple/src/main/java/org/jsfr/json/parse/SurfingContext.java rename to jsurfer-simple/src/main/java/org/jsfr/json/SurfingContext.java index be33517..8ccdb53 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/parse/SurfingContext.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/SurfingContext.java @@ -22,17 +22,21 @@ * THE SOFTWARE. */ -package org.jsfr.json.parse; +package org.jsfr.json; -import org.json.simple.parser.ContentHandler; -import org.json.simple.parser.ParseException; import org.jsfr.json.path.ArrayIndex; import org.jsfr.json.path.JsonPath; import org.jsfr.json.path.PathOperator; import org.jsfr.json.path.PathOperator.Type; +import org.json.simple.parser.ContentHandler; +import org.json.simple.parser.ParseException; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; public class SurfingContext implements ParsingContext, ContentHandler { @@ -51,9 +55,6 @@ public static Builder builder() { public SurfingContext build() { if (!context.built) { - if (context.jsonProvider == null) { - context.jsonProvider = new JavaCollectionProvider(); - } if (!indefiniteBindings.isEmpty()) { Collections.sort(indefiniteBindings); context.indefinitePathLookup = indefiniteBindings.toArray(new IndefinitePathBinding[indefiniteBindings.size()]); @@ -278,7 +279,7 @@ public boolean startObjectEntry(String key) throws ParseException, IOException { if (stopped) { return false; } - currentPosition.stepInObject(key); + currentPosition.stepIntoChild(key); dispatcher.startObjectEntry(key); doMatching(true, false, null); return true; @@ -304,7 +305,7 @@ public boolean startArray() throws ParseException, IOException { ((ArrayIndex) top).increaseArrayIndex(); doMatching(true, false, null); } - currentPosition.stepInArray(); + currentPosition.stepOverNextIndex(); dispatcher.startArray(); return true; } @@ -338,7 +339,6 @@ public String getPath() { return this.currentPosition.toString(); } - @Override public void stopParsing() { this.stopped = true; @@ -349,7 +349,11 @@ public boolean isStopped() { return this.stopped; } - public JsonProvider getJsonProvider() { + void setJsonProvider(JsonProvider jsonProvider) { + this.jsonProvider = jsonProvider; + } + + JsonProvider getJsonProvider() { return jsonProvider; } } diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/TypedListener.java b/jsurfer-simple/src/main/java/org/jsfr/json/TypedListener.java new file mode 100644 index 0000000..468eda7 --- /dev/null +++ b/jsurfer-simple/src/main/java/org/jsfr/json/TypedListener.java @@ -0,0 +1,39 @@ +/* + * The MIT License + * + * Copyright (c) 2015 WANG Lingsong + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package org.jsfr.json; + +/** + * Created by Leo on 2015/3/30. + */ +public abstract class TypedListener implements JsonPathListener { + + public abstract void onTypedValue(T value, ParsingContext context); + + @Override + public void onValue(Object value, ParsingContext context) { + onTypedValue((T) value, context); + } + +} diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/path/ArrayIndexes.java b/jsurfer-simple/src/main/java/org/jsfr/json/path/ArrayIndexes.java index 4fe366d..215b95b 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/path/ArrayIndexes.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/path/ArrayIndexes.java @@ -40,7 +40,7 @@ protected ArrayIndexes(HashSet indexes) { @Override public boolean match(PathOperator pathOperator) { - return super.match(pathOperator) && indexes.contains(((ArrayIndex)pathOperator).getArrayIndex()); + return super.match(pathOperator) && indexes.contains(((ArrayIndex) pathOperator).getArrayIndex()); } @Override diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/path/ChildrenNode.java b/jsurfer-simple/src/main/java/org/jsfr/json/path/ChildrenNode.java index 41fd7fe..919fd6b 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/path/ChildrenNode.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/path/ChildrenNode.java @@ -40,7 +40,7 @@ public ChildrenNode(HashSet children) { @Override public boolean match(PathOperator pathOperator) { - return super.match(pathOperator) && children.contains(((ChildNode)pathOperator).getKey()); + return super.match(pathOperator) && children.contains(((ChildNode) pathOperator).getKey()); } @Override diff --git a/jsurfer-simple/src/main/java/org/jsfr/json/path/JsonPath.java b/jsurfer-simple/src/main/java/org/jsfr/json/path/JsonPath.java index 1bf3db2..1e04e00 100644 --- a/jsurfer-simple/src/main/java/org/jsfr/json/path/JsonPath.java +++ b/jsurfer-simple/src/main/java/org/jsfr/json/path/JsonPath.java @@ -24,7 +24,10 @@ package org.jsfr.json.path; -import java.util.*; +import java.util.Arrays; +import java.util.HashSet; +import java.util.ListIterator; +import java.util.Stack; public class JsonPath { diff --git a/jsurfer-simple/src/test/java/JsonSurferTest.java b/jsurfer-simple/src/test/java/org/jsfr/json/JsonSurferTest.java similarity index 82% rename from jsurfer-simple/src/test/java/JsonSurferTest.java rename to jsurfer-simple/src/test/java/org/jsfr/json/JsonSurferTest.java index db50257..7072bbb 100644 --- a/jsurfer-simple/src/test/java/JsonSurferTest.java +++ b/jsurfer-simple/src/test/java/org/jsfr/json/JsonSurferTest.java @@ -22,32 +22,34 @@ * THE SOFTWARE. */ +package org.jsfr.json; + import com.google.common.io.Resources; import org.hamcrest.Description; import org.hamcrest.TypeSafeMatcher; +import org.jsfr.json.SurfingContext.Builder; import org.junit.Before; import org.junit.Test; -import org.jsfr.json.BuilderFactory; -import org.jsfr.json.JsonSimpleSurfer; -import org.jsfr.json.JsonSurfer; -import org.jsfr.json.parse.JsonPathListener; -import org.jsfr.json.parse.JsonProvider; -import org.jsfr.json.parse.JsonSimpleProvider; -import org.jsfr.json.parse.ParsingContext; -import org.jsfr.json.parse.SurfingContext.Builder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.InputStreamReader; +import java.util.Collection; +import java.util.HashMap; import java.util.concurrent.atomic.AtomicLong; import static org.jsfr.json.BuilderFactory.context; import static org.jsfr.json.BuilderFactory.root; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyObject; import static org.mockito.Matchers.argThat; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.isA; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; public class JsonSurferTest { @@ -64,13 +66,13 @@ public void onValue(Object value, ParsingContext context) { @Before public void setUp() throws Exception { - surfer = new JsonSimpleSurfer(); provider = new JsonSimpleProvider(); + surfer = new JsonSimpleSurfer(provider); } @Test public void testSampleJson() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").child("book").index(0).child("category"), mockListener) .bind(root().child("store").child("book").index(0), mockListener) @@ -100,7 +102,7 @@ public void testSampleJson() throws Exception { @Test public void testStoppableParsing() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").child("book").indexes(0, 1, 2), mockListener) .bind(root().child("store").child("book").index(3), mockListener); @@ -127,7 +129,7 @@ public void describeTo(Description description) { @Test public void testChildNodeWildcard() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").anyChild(), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -137,7 +139,7 @@ public void testChildNodeWildcard() throws Exception { @Test public void testAnyIndex() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").child("book").anyIndex(), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -147,7 +149,7 @@ public void testAnyIndex() throws Exception { @Test public void testWildcardCombination() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").child("book").anyIndex().anyChild(), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -157,7 +159,7 @@ public void testWildcardCombination() throws Exception { @Test public void testParsingArray() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener wholeArray = mock(JsonPathListener.class); JsonPathListener stringElement = mock(JsonPathListener.class); JsonPathListener numberElement = mock(JsonPathListener.class); @@ -191,7 +193,7 @@ public void testParsingArray() throws Exception { @Test public void testDeepScan() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().scan().child("author"), mockListener); builder.bind(root().scan().child("store").scan().child("bicycle").scan().child("color"), mockListener); @@ -206,7 +208,7 @@ public void testDeepScan() throws Exception { @Test public void testDeepScan2() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().scan().child("store").scan().child("price"), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -219,7 +221,7 @@ public void testDeepScan2() throws Exception { @Test public void testAny() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().child("store").scan().child("bicycle").scan().any(), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -229,7 +231,7 @@ public void testAny() throws Exception { @Test public void testFindEverything() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); builder.bind(root().scan().any(), new JsonPathListener() { @Override public void onValue(Object value, ParsingContext context) { @@ -241,7 +243,7 @@ public void onValue(Object value, ParsingContext context) { @Test public void testIndexesAndChildrenOperator() throws Exception { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); JsonPathListener mockListener = mock(JsonPathListener.class); builder.bind(root().scan().child("book").indexes(1, 3).children("author", "title"), mockListener); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); @@ -251,51 +253,64 @@ public void testIndexesAndChildrenOperator() throws Exception { verify(mockListener).onValue(eq(provider.primitive("The Lord of the Rings")), any(ParsingContext.class)); } + + @Test + public void testCollectAll() throws Exception { + Collection values = surfer.collect(new InputStreamReader(Resources.getResource("sample.json").openStream()), root().scan().child("book").indexes(1, 3).children("author", "title").build()); + assertEquals(4, values.size()); + } + + @Test + public void testCollectOne() throws Exception { + Object value = surfer.collectOne(new InputStreamReader(Resources.getResource("sample.json").openStream()), root().scan().child("book").indexes(1, 3).children("author", "title").build()); + assertEquals(provider.primitive("Evelyn Waugh"), value); + } + @Test public void testExample1() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().child("store").child("book").anyIndex().child("author"), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testExample2() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().scan().child("author"), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testExample3() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().child("store").any(), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testExample4() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().child("store").scan().child("price"), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testExample5() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().scan().child("book").index(2), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testExample6() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().scan().child("book").indexes(0, 1), print); surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } @Test public void testStoppable() throws Exception { - Builder builder = BuilderFactory.context().withJsonProvider(provider); + Builder builder = BuilderFactory.context(); builder.bind(root().scan().child("book").indexes(0, 1), new JsonPathListener() { @Override public void onValue(Object value, ParsingContext parsingContext) { @@ -306,31 +321,40 @@ public void onValue(Object value, ParsingContext parsingContext) { surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); } + @Test + public void testPlugableProvider() throws Exception { + JsonPathListener mockListener = mock(JsonPathListener.class); + Builder builder = BuilderFactory.context().withJsonProvider(new JavaCollectionProvider()); + builder.bind(root().child("store"), mockListener); + surfer.surf(new InputStreamReader(Resources.getResource("sample.json").openStream()), builder.build()); + verify(mockListener).onValue(isA(HashMap.class), any(ParsingContext.class)); + } + @Test public void performanceTest() throws Exception { // for (int i=0;i < 1000;i++) { - Builder builder = context().withJsonProvider(provider); - final AtomicLong counter = new AtomicLong(); - JsonPathListener printListener = new JsonPathListener() { - - @Override - public void onValue(Object value, ParsingContext context) { - counter.incrementAndGet(); - LOGGER.trace("value: {}", value); - } - }; - builder.bind(root().child("builders").anyChild().child("properties"), printListener).skipOverlappedPath(); - long start = System.currentTimeMillis(); - surfer.surf(new InputStreamReader(Resources.getResource("allthethings.json").openStream()), builder.build()); - LOGGER.info(surfer.getClass().getSimpleName() + " processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - - start); + Builder builder = context(); + final AtomicLong counter = new AtomicLong(); + JsonPathListener printListener = new JsonPathListener() { + + @Override + public void onValue(Object value, ParsingContext context) { + counter.incrementAndGet(); + LOGGER.trace("value: {}", value); + } + }; + builder.bind(root().child("builders").anyChild().child("properties"), printListener).skipOverlappedPath(); + long start = System.currentTimeMillis(); + surfer.surf(new InputStreamReader(Resources.getResource("allthethings.json").openStream()), builder.build()); + LOGGER.info(surfer.getClass().getSimpleName() + " processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() + - start); // } } @Test public void performanceTestWithDeepScanOperator() throws Exception { // for (int i=0;i < 1000;i++) { - Builder builder = context().withJsonProvider(provider); + Builder builder = context(); final AtomicLong counter = new AtomicLong(); JsonPathListener printListener = new JsonPathListener() { @Override