Skip to content

Commit

Permalink
1. New API: collectAll and collectOne
Browse files Browse the repository at this point in the history
2. Refactor source structure
  • Loading branch information
wanglingsong committed Mar 30, 2015
1 parent 1368072 commit dd23be2
Show file tree
Hide file tree
Showing 27 changed files with 389 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonObject, JsonArray, JsonElement> {

private final static Gson DEFAULT_GSON = new Gson();

@Override
public JsonObject createObject() {
return new JsonObject();
Expand Down Expand Up @@ -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
Expand All @@ -75,7 +77,7 @@ public JsonElement primitive(int value) {

@Override
public JsonElement primitive(double value) {
return new JsonPrimitive(value);
return new JsonPrimitive(value);
}

@Override
Expand Down
25 changes: 15 additions & 10 deletions jsurfer-gson/src/main/java/org/jsfr/json/GsonSurfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<EntryType> entryStack = new Stack<EntryType>();
entryStack.push(EntryType.ROOT);
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 17 additions & 11 deletions jsurfer-jackson/src/main/java/org/jsfr/json/JacksonSurfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,29 @@
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;

/**
* 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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down
75 changes: 75 additions & 0 deletions jsurfer-simple/src/main/java/org/jsfr/json/AbstractSurfer.java
Original file line number Diff line number Diff line change
@@ -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 <T> Collection<T> collect(Reader reader, JsonPath... paths) {
CollectAllListener<T> listener = new CollectAllListener<T>();
Builder builder = builder().withJsonProvider(jsonProvider);
for (JsonPath jsonPath : paths) {
builder.bind(jsonPath, listener);
}
surf(reader, builder.build());
return listener.getCollection();
}

@Override
public <T> T collectOne(Reader reader, JsonPath... paths) {
CollectOneListener<T> listener = new CollectOneListener<T>();
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.jsfr.json;

import org.jsfr.json.parse.SurfingContext;
import org.jsfr.json.path.JsonPath;

public class BuilderFactory {
Expand Down
Loading

0 comments on commit dd23be2

Please sign in to comment.