Skip to content

Commit

Permalink
API clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglingsong committed Apr 6, 2017
1 parent 77e3f15 commit bb82f46
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 87 deletions.
5 changes: 3 additions & 2 deletions jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> book = surfer.collectAll(reader, Book.class, "$..book[0,1]");
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
assertEquals(2, book.size());
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> book = surfer.collectAll(reader, Book.class, "$..book[0,1]");
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
assertEquals(2, book.size());
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
}


}
5 changes: 3 additions & 2 deletions jsurfer-all/src/test/java/org/jsfr/json/JsonSurferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -446,7 +447,7 @@ public void testCollectOneRaw() throws Exception {

@Test
public void testCollectAll() throws Exception {
Collection<String> values = surfer.collectAll(read("sample.json"), String.class, "$..book[1,3][author, title]");
Collection<String> 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());
}
Expand All @@ -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);
}

Expand Down
149 changes: 69 additions & 80 deletions jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,26 @@ public void surf(Reader reader, SurfingConfiguration configuration) {
jsonParserAdapter.parse(reader, new SurfingContext(configuration));
}

public Collection<Object> 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<Object> 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<Object> collectAll(Reader reader, JsonPath... paths) {
return collectAll(reader, Object.class, paths);
public Collection<Object> collectAll(String json, JsonPath... paths) {
return collectAll(json, Object.class, paths);
}

/**
Expand All @@ -125,30 +132,22 @@ public <T> Collection<T> collectAll(String json, Class<T> tClass, JsonPath... pa
* Collect all matched value into a collection
*
* @param reader Json reader
* @param tClass type
* @param paths JsonPath
* @param <T> type
* @return typed value
* @return values
*/
public <T> Collection<T> collectAll(Reader reader, Class<T> tClass, JsonPath... paths) {
CollectAllListener<T> listener = new CollectAllListener<T>(jsonProvider, tClass);
SurfingConfiguration.Builder builder = configBuilder();
for (JsonPath jsonPath : paths) {
builder.bind(jsonPath, listener);
}
surf(reader, builder.build());
return listener.getCollection();
public Collection<Object> 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 <T> target class
* @return typed value
* @return All matched value
*/
public <T> Collection<T> collectAll(String json, Class<T> tClass, String... paths) {
return collectAll(json, tClass, compile(paths));
public Collection<Object> collectAll(Reader reader, JsonPath... paths) {
return collectAll(reader, Object.class, paths);
}

/**
Expand All @@ -160,40 +159,45 @@ public <T> Collection<T> collectAll(String json, Class<T> tClass, String... path
* @param <T> type
* @return typed value
*/
public <T> Collection<T> collectAll(Reader reader, Class<T> tClass, String... paths) {
return collectAll(reader, tClass, compile(paths));
}

public Collection<Object> collectAll(String json, String... paths) {
return collectAll(json, Object.class, paths);
public <T> Collection<T> collectAll(Reader reader, Class<T> tClass, JsonPath... paths) {
CollectAllListener<T> listener = new CollectAllListener<T>(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<Object> 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 <T> type
* @return value
*/
public <T> T collectOne(String json, Class<T> tClass, JsonPath... paths) {
CollectOneListener listener = new CollectOneListener(true);
SurfingConfiguration.Builder builder = configBuilder().skipOverlappedPath();
Expand All @@ -209,58 +213,43 @@ public <T> T collectOne(String json, Class<T> tClass, JsonPath... paths) {
* Collect the first matched value and stop parsing immediately
*
* @param reader Json reader
* @param tClass type
* @param paths JsonPath
* @param <T> type
* @return typed value
* @return Value
*/
@SuppressWarnings("unchecked")
public <T> T collectOne(Reader reader, Class<T> 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> T collectOne(String json, Class<T> 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 <T> type
* @return typed value
*/
public <T> T collectOne(Reader reader, Class<T> 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 <T> type
* @return typed value
*/
public Object collectOne(Reader reader, String... paths) {
return collectOne(reader, Object.class, paths);
@SuppressWarnings("unchecked")
public <T> T collectOne(Reader reader, Class<T> 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) {
Expand Down

0 comments on commit bb82f46

Please sign in to comment.