Browse Source

Code clean up.

pull/41/head
Kalle Stenflo 11 years ago
parent
commit
5a4bf10940
  1. 15
      json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java
  2. 1
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  3. 3
      json-path/src/main/java/com/jayway/jsonpath/InvalidCriteriaException.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java
  5. 3
      json-path/src/main/java/com/jayway/jsonpath/InvalidModelException.java
  6. 3
      json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java
  7. 59
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  8. 17
      json-path/src/main/java/com/jayway/jsonpath/Option.java
  9. 19
      json-path/src/main/java/com/jayway/jsonpath/ReadContext.java
  10. 30
      json-path/src/main/java/com/jayway/jsonpath/Transformer.java
  11. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  12. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/CompiledPath.java
  13. 32
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java
  14. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java
  15. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonProvider.java
  16. 3
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java
  17. 31
      json-path/src/main/java/com/jayway/jsonpath/spi/compiler/EvaluationContext.java
  18. 3
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java
  19. 3
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProviderFactory.java
  20. 3
      json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java

15
json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java

@ -1,6 +1,8 @@
package com.jayway.jsonpath.web.bench; package com.jayway.jsonpath.web.bench;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.internal.spi.json.JacksonProvider; import com.jayway.jsonpath.internal.spi.json.JacksonProvider;
import com.jayway.jsonpath.spi.json.JsonProviderFactory; import com.jayway.jsonpath.spi.json.JsonProviderFactory;
import io.gatling.jsonpath.JsonPath$; import io.gatling.jsonpath.JsonPath$;
@ -32,14 +34,23 @@ public class Bench {
String error = null; String error = null;
long time; long time;
Object res = null; Object res = null;
long now = System.currentTimeMillis();
//Configuration configuration = Configuration.defaultConfiguration();
Configuration configuration = Configuration.defaultConfiguration().options(Option.ALWAYS_RETURN_LIST);
if(!value){
configuration = configuration.options(Option.AS_PATH_LIST);
}
long now = System.currentTimeMillis();
try { try {
res = JsonPath.using(configuration).parse(json).read(path);
/*
if(value) { if(value) {
res = JsonPath.parse(json).read(path); res = JsonPath.parse(json).read(path);
} else { } else {
res = JsonPath.parse(json).readPathList(path); res = JsonPath.parse(json).readPathList(path);
} }*/
} catch (Exception e){ } catch (Exception e){
error = getError(e); error = getError(e);

1
json-path/src/main/java/com/jayway/jsonpath/Configuration.java

@ -26,7 +26,6 @@ import static com.jayway.jsonpath.internal.Utils.notNull;
public class Configuration { public class Configuration {
private final JsonProvider provider; private final JsonProvider provider;
private final Set<Option> options; private final Set<Option> options;

3
json-path/src/main/java/com/jayway/jsonpath/InvalidCriteriaException.java

@ -14,9 +14,6 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
/**
* @author Kalle Stenflo
*/
public class InvalidCriteriaException extends RuntimeException { public class InvalidCriteriaException extends RuntimeException {
public InvalidCriteriaException() { public InvalidCriteriaException() {
} }

4
json-path/src/main/java/com/jayway/jsonpath/InvalidJsonException.java

@ -14,9 +14,7 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
/**
* @author Kalle Stenflo
*/
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class InvalidJsonException extends RuntimeException { public class InvalidJsonException extends RuntimeException {

3
json-path/src/main/java/com/jayway/jsonpath/InvalidModelException.java

@ -14,9 +14,6 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
/**
* @author Kalle Stenflo
*/
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class InvalidModelException extends RuntimeException { public class InvalidModelException extends RuntimeException {

3
json-path/src/main/java/com/jayway/jsonpath/InvalidPathException.java

@ -14,9 +14,6 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
/**
* @author Kalle Stenflo
*/
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class InvalidPathException extends RuntimeException { public class InvalidPathException extends RuntimeException {

59
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -28,8 +28,6 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.List;
import java.util.regex.Pattern;
import static com.jayway.jsonpath.internal.Utils.*; import static com.jayway.jsonpath.internal.Utils.*;
@ -91,13 +89,9 @@ import static com.jayway.jsonpath.internal.Utils.*;
* <code> * <code>
* String author = JsonPath.read(json, "$.store.book[1].author") * String author = JsonPath.read(json, "$.store.book[1].author")
* </code> * </code>
*
* @author Kalle Stenflo
*/ */
public class JsonPath { public class JsonPath {
private static final Pattern DEFINITE_PATH_PATTERN = Pattern.compile(".*(\\.\\.|\\*|\\[[\\\\/]|\\?|,|:\\s?]|\\[\\s?:|>|\\(|<|=|\\+).*");
private final Path path; private final Path path;
private JsonPath(String jsonPath, Filter[] filters) { private JsonPath(String jsonPath, Filter[] filters) {
@ -115,28 +109,10 @@ public class JsonPath {
} }
/** /**
* Checks if a path points to a single item or if it potentially returns multiple items * @see JsonPath#isDefinite()
* <p/>
* a path is considered <strong>not</strong> definite if it contains a scan fragment ".."
* or an array position fragment that is not based on a single index
* <p/>
* <p/>
* definite path examples are:
* <p/>
* $store.book
* $store.book[1].title
* <p/>
* not definite path examples are:
* <p/>
* $..book
* $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')]
*
* @return true if path is definite (points to single item)
*/ */
public static boolean isPathDefinite(String path) { public static boolean isPathDefinite(String path) {
String preparedPath = path.replaceAll("\"[^\"\\\\\\n\r]*\"", ""); return compile(path).isDefinite();
return !DEFINITE_PATH_PATTERN.matcher(preparedPath).matches();
} }
@ -155,12 +131,13 @@ public class JsonPath {
* not definite path examples are: * not definite path examples are:
* <p/> * <p/>
* $..book * $..book
* $.store.book[*]
* $.store.book[1,2] * $.store.book[1,2]
* $.store.book[?(@.category = 'fiction')] * $.store.book[?(@.category = 'fiction')]
* *
* @return true if path is definite (points to single item) * @return true if path is definite (points to single item)
*/ */
public boolean isPathDefinite() { public boolean isDefinite() {
return path.isDefinite(); return path.isDefinite();
} }
@ -189,33 +166,7 @@ public class JsonPath {
* @return object(s) matched by the given path * @return object(s) matched by the given path
*/ */
public <T> T read(Object jsonObject, Configuration configuration) { public <T> T read(Object jsonObject, Configuration configuration) {
return (T)path.evaluate(jsonObject, configuration).getWithOptions();
return path.evaluate(jsonObject, configuration).get();
}
/**
* Applies this JsonPath to the provided json document.
* Note that the document must be identified as either a List or Map by
* the {@link JsonProvider}
*
* @param jsonObject a container Object
* @return list of definite path strings to object matched by path
*/
public List<String> readPathList(Object jsonObject) {
return readPathList(jsonObject, Configuration.defaultConfiguration());
}
/**
* Applies this JsonPath to the provided json document.
* Note that the document must be identified as either a List or Map by
* the {@link JsonProvider}
*
* @param jsonObject a container Object
* @param configuration configuration to use
* @return list of definite path strings to object matched by path
*/
public List<String> readPathList(Object jsonObject, Configuration configuration) {
return path.evaluate(jsonObject, configuration).getPathList();
} }
/** /**

17
json-path/src/main/java/com/jayway/jsonpath/Option.java

@ -15,8 +15,23 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
public enum Option { public enum Option {
/** /**
* Throw {@link PathNotFoundException} when JsonPath tries to read a property that does not exists. * Throw {@link PathNotFoundException} when JsonPath tries to read a property that does not exists.
*/ */
THROW_ON_MISSING_PROPERTY THROW_ON_MISSING_PROPERTY,
/**
* Makes this implementation more compliant to the Goessner spec. All results are returned as Lists.
*
*/
ALWAYS_RETURN_LIST,
/**
* returns a list of path strings representing the path of the evaluation hits
*
*/
AS_PATH_LIST
} }

19
json-path/src/main/java/com/jayway/jsonpath/ReadContext.java

@ -14,8 +14,6 @@
*/ */
package com.jayway.jsonpath; package com.jayway.jsonpath;
import java.util.List;
public interface ReadContext { public interface ReadContext {
/** /**
@ -44,21 +42,4 @@ public interface ReadContext {
*/ */
<T> T read(JsonPath path); <T> T read(JsonPath path);
/**
* Reads the given path list from this context
*
* @param path a container Object
* @param filters filters
* @return list of definite path strings to object matched by path
*/
List<String> readPathList(String path, Filter... filters);
/**
* Reads the given path list from this context
*
* @param path path to apply
* @return list of definite path strings to object matched by path
*/
List<String> readPathList(JsonPath path);
} }

30
json-path/src/main/java/com/jayway/jsonpath/Transformer.java

@ -1,30 +0,0 @@
/*
* Copyright 2011 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.jsonpath;
/**
* @author Kalle Stenflo
*/
public interface Transformer<T> {
/**
* @param obj object to transform
* @param configuration configuration to use
* @return the transformed object
*/
public Object transform(T obj, Configuration configuration);
}

12
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -12,7 +12,6 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.List;
import static com.jayway.jsonpath.internal.Utils.notEmpty; import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull; import static com.jayway.jsonpath.internal.Utils.notNull;
@ -99,15 +98,4 @@ public class JsonReader implements ParseContext, ReadContext {
return path.read(json, configuration); return path.read(json, configuration);
} }
@Override
public List<String> readPathList(String path, Filter... filters) {
notEmpty(path, "path can not be null or empty");
return readPathList(JsonPath.compile(path, filters));
}
@Override
public List<String> readPathList(JsonPath path) {
notNull(path, "path can not be null");
return path.readPathList(json, configuration);
}
} }

2
json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/CompiledPath.java

@ -24,7 +24,7 @@ class CompiledPath implements Path {
logger.debug("Evaluating path: {}", toString()); logger.debug("Evaluating path: {}", toString());
} }
EvaluationContextImpl ctx = new EvaluationContextImpl(configuration, isDefinite()); EvaluationContextImpl ctx = new EvaluationContextImpl(this, configuration);
root.evaluate("", model, ctx); root.evaluate("", model, ctx);
if(logger.isDebugEnabled()) { if(logger.isDebugEnabled()) {

32
json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java

@ -3,6 +3,7 @@ package com.jayway.jsonpath.internal.spi.compiler;
import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option; import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.compiler.EvaluationContext; import com.jayway.jsonpath.spi.compiler.EvaluationContext;
import com.jayway.jsonpath.spi.compiler.Path;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
import java.util.ArrayList; import java.util.ArrayList;
@ -17,14 +18,15 @@ class EvaluationContextImpl implements EvaluationContext {
private final Configuration configuration; private final Configuration configuration;
private final Object objectResult; private final Object objectResult;
private final List<String> pathResult; private final List<String> pathResult;
private final boolean isDefinite; private final Path path;
private int resultIndex = 0; private int resultIndex = 0;
EvaluationContextImpl(Configuration configuration, boolean isDefinite) { EvaluationContextImpl(Path path, Configuration configuration) {
this.path = path;
this.configuration = configuration; this.configuration = configuration;
this.objectResult = configuration.getProvider().createArray(); this.objectResult = configuration.getProvider().createArray();
this.pathResult = new ArrayList<String>(); this.pathResult = new ArrayList<String>();
this.isDefinite = isDefinite;
} }
void addResult(String path, Object model) { void addResult(String path, Object model) {
@ -49,15 +51,35 @@ class EvaluationContextImpl implements EvaluationContext {
@Override @Override
public <T> T get() { public <T> T get() {
if (isDefinite) { if (path.isDefinite()) {
return (T) jsonProvider().getProperty(objectResult, 0); return (T) jsonProvider().getProperty(objectResult, 0);
} }
return (T) objectResult; return (T) objectResult;
} }
@Override
public Object getWithOptions() {
if(configuration.getOptions().contains(Option.AS_PATH_LIST)) {
Object array = configuration.getProvider().createArray();
int i = 0;
for (String p : pathResult) {
configuration.getProvider().setProperty(array, i, p);
i++;
}
return array;
} else if(configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST)){
return objectResult;
} else {
return get();
}
}
@Override @Override
public List<String> getPathList() { public List<String> getPathList() {
return pathResult; return pathResult;
} }
} }

3
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java

@ -23,9 +23,6 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* @author Kalle Stenflo
*/
public abstract class AbstractJsonProvider implements JsonProvider { public abstract class AbstractJsonProvider implements JsonProvider {
@Override @Override

3
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonProvider.java

@ -30,9 +30,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* @author Kalle Stenflo
*/
public class JacksonProvider extends AbstractJsonProvider { public class JacksonProvider extends AbstractJsonProvider {
private static final Logger logger = LoggerFactory.getLogger(JacksonProvider.class); private static final Logger logger = LoggerFactory.getLogger(JacksonProvider.class);

3
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java

@ -28,9 +28,6 @@ import java.io.Reader;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* @author Kalle Stenflo
*/
public class JsonSmartJsonProvider extends AbstractJsonProvider { public class JsonSmartJsonProvider extends AbstractJsonProvider {
private Mode mode; private Mode mode;

31
json-path/src/main/java/com/jayway/jsonpath/spi/compiler/EvaluationContext.java

@ -4,13 +4,38 @@ import com.jayway.jsonpath.Configuration;
import java.util.List; import java.util.List;
/**
*
*/
public interface EvaluationContext { public interface EvaluationContext {
/**
*
* @return the configuration used for this evaluation
*/
Configuration configuration(); Configuration configuration();
/**
* This method does not adhere to configuration settings. It will return a single object (not wrapped in a List) even if the
* configuration contains the {@link com.jayway.jsonpath.Option#ALWAYS_RETURN_LIST}
*
* @param <T> expected return type
* @return evaluation result
*/
<T> T get(); <T> T get();
/**
* This method does adhere to configuration settings like
*
* {@link com.jayway.jsonpath.Option#ALWAYS_RETURN_LIST}
* {@link com.jayway.jsonpath.Option#AS_PATH_LIST}
*
* @return evaluation result
*/
Object getWithOptions();
/**
* Convenience method to get list of hits as String path representations
*
* @return list of path representations
*/
List<String> getPathList(); List<String> getPathList();
} }

3
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

@ -20,9 +20,6 @@ import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.util.Collection; import java.util.Collection;
/**
* @author Kalle Stenflo
*/
public interface JsonProvider { public interface JsonProvider {
Mode getMode(); Mode getMode();

3
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProviderFactory.java

@ -16,9 +16,6 @@ package com.jayway.jsonpath.spi.json;
import com.jayway.jsonpath.internal.spi.json.JsonSmartJsonProvider; import com.jayway.jsonpath.internal.spi.json.JsonSmartJsonProvider;
/**
* @author Kalle Stenflo
*/
public abstract class JsonProviderFactory { public abstract class JsonProviderFactory {
private static JsonProvider provider = new JsonSmartJsonProvider(); private static JsonProvider provider = new JsonSmartJsonProvider();

3
json-path/src/main/java/com/jayway/jsonpath/spi/json/Mode.java

@ -14,9 +14,6 @@
*/ */
package com.jayway.jsonpath.spi.json; package com.jayway.jsonpath.spi.json;
/**
* @author Kalle Stenflo
*/
public enum Mode { public enum Mode {
SLACK(-1), SLACK(-1),

Loading…
Cancel
Save