Skip to content

Commit

Permalink
Add chaining API for convenient surfing
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Wang committed Nov 12, 2015
1 parent bf1907a commit e81904a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 160 deletions.
39 changes: 0 additions & 39 deletions jsurfer-core/src/main/java/org/jsfr/json/BuilderFactory.java

This file was deleted.

55 changes: 45 additions & 10 deletions jsurfer-core/src/main/java/org/jsfr/json/JsonSurfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.StringReader;
import java.util.Collection;

import static org.jsfr.json.SurfingConfiguration.builder;
import static org.jsfr.json.compiler.JsonPathCompiler.compile;


Expand Down Expand Up @@ -88,8 +87,21 @@ public JsonSurfer(JsonParserAdapter jsonParserAdapter, JsonProvider jsonProvider
this.errorHandlingStrategy = errorHandlingStrategy;
}

/**
* Create SurfingConfiguration builder associated with this surfer
*
* @return SurfingConfiguration builder
*/
public SurfingConfiguration.Builder builder() {
return SurfingConfiguration.builder().withSurfer(this);
}

/**
* @param json json
* @param configuration SurfingConfiguration that holds JsonPath binding
*/
public void surf(String json, SurfingConfiguration configuration) {
surf(new StringReader(json), configuration);
surf(read(json), configuration);
}

/**
Expand All @@ -102,7 +114,7 @@ public void surf(Reader reader, SurfingConfiguration configuration) {
}

public Collection<Object> collectAll(String json, JsonPath... paths) {
return collectAll(new StringReader(json), paths);
return collectAll(read(json), paths);
}

/**
Expand All @@ -116,8 +128,15 @@ public Collection<Object> collectAll(Reader reader, JsonPath... paths) {
return collectAll(reader, Object.class, paths);
}

/**
* @param json json
* @param tClass target class
* @param paths JsonPath
* @param <T> target class
* @return typed value
*/
public <T> Collection<T> collectAll(String json, Class<T> tClass, JsonPath... paths) {
return collectAll(new StringReader(json), tClass, paths);
return collectAll(read(json), tClass, paths);
}

/**
Expand All @@ -139,8 +158,15 @@ public <T> Collection<T> collectAll(Reader reader, Class<T> tClass, JsonPath...
return listener.getCollection();
}

/**
* @param json json
* @param tClass target class
* @param paths JsonPath
* @param <T> target class
* @return typed value
*/
public <T> Collection<T> collectAll(String json, Class<T> tClass, String... paths) {
return collectAll(new StringReader(json), tClass, paths);
return collectAll(read(json), tClass, paths);
}

/**
Expand All @@ -157,7 +183,7 @@ public <T> Collection<T> collectAll(Reader reader, Class<T> tClass, String... pa
}

public Collection<Object> collectAll(String json, String... paths) {
return collectAll(new StringReader(json), paths);
return collectAll(read(json), paths);
}

/**
Expand All @@ -172,7 +198,7 @@ public Collection<Object> collectAll(Reader reader, String... paths) {
}

public Object collectOne(String json, JsonPath... paths) {
return collectOne(new StringReader(json), paths);
return collectOne(read(json), paths);
}

/**
Expand All @@ -187,7 +213,7 @@ public Object collectOne(Reader reader, JsonPath... paths) {
}

public <T> T collectOne(String json, Class<T> tClass, JsonPath... paths) {
return collectOne(new StringReader(json), tClass, paths);
return collectOne(read(json), tClass, paths);
}

/**
Expand All @@ -212,7 +238,7 @@ public <T> T collectOne(Reader reader, Class<T> tClass, JsonPath... paths) {
}

public <T> T collectOne(String json, Class<T> tClass, String... paths) {
return collectOne(new StringReader(json), tClass, paths);
return collectOne(read(json), tClass, paths);
}

/**
Expand All @@ -228,8 +254,13 @@ 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
*/
public Object collectOne(String json, String... paths) {
return collectOne(new StringReader(json), paths);
return collectOne(read(json), paths);
}

/**
Expand All @@ -252,4 +283,8 @@ private void ensureSetting(SurfingConfiguration configuration) {
}
}

public Reader read(String json) {
return new StringReader(json);
}

}
28 changes: 28 additions & 0 deletions jsurfer-core/src/main/java/org/jsfr/json/SurfingConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jsfr.json.path.JsonPath;
import org.jsfr.json.provider.JsonProvider;

import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -78,6 +79,7 @@ public int compare(IndefinitePathBinding o1, IndefinitePathBinding o2) {

public static class Builder {

private JsonSurfer jsonSurfer;
private SurfingConfiguration configuration;
private Map<Integer, ArrayList<Binding>> definiteBindings = new HashMap<Integer, ArrayList<Binding>>();
private ArrayList<IndefinitePathBinding> indefiniteBindings = new ArrayList<IndefinitePathBinding>();
Expand All @@ -96,6 +98,32 @@ public SurfingConfiguration build() {
return configuration;
}

/**
* Associated with a JsonSurfer
* @param jsonSurfer JsonSurfer
* @return builder
*/
public Builder withSurfer(JsonSurfer jsonSurfer) {
this.jsonSurfer = jsonSurfer;
return this;
}

/**
* 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));
}

/**
* Build the configuration and then surf with it and the associated JsonSurfer
* @param jsonReader jsonReader
*/
public void surf(Reader jsonReader) {
this.jsonSurfer.surf(jsonReader, this.build());
}

public Builder bind(String path, JsonPathListener... jsonPathListeners) {
return bind(compile(path), jsonPathListeners);
}
Expand Down
Loading

0 comments on commit e81904a

Please sign in to comment.