diff --git a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java index 02f19c18..ce69eb4d 100644 --- a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java +++ b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java @@ -13,24 +13,26 @@ import org.boon.json.implementation.ObjectMapperImpl; import scala.collection.Iterator; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; - -import static java.util.Arrays.asList; +import java.util.Map; public class Bench { protected final String json; protected final String path; private final boolean optionAsValues; - private final boolean optionAllwaysReturnList; - private final boolean optionMergeMultiProps; + private final boolean flagWrap; + private final boolean flagMerge; + private final boolean flagSuppress; - public Bench(String json, String path, boolean optionAsValues, boolean optionAllwaysReturnList, boolean optionMergeMultiProps) { + public Bench(String json, String path, boolean optionAsValues, boolean flagWrap, boolean flagMerge, boolean flagSuppress) { this.json = json; this.path = path; this.optionAsValues = optionAsValues; - this.optionAllwaysReturnList = optionAllwaysReturnList; - this.optionMergeMultiProps = optionMergeMultiProps; + this.flagWrap = flagWrap; + this.flagMerge = flagMerge; + this.flagSuppress = flagSuppress; } public Result runJayway() { @@ -41,12 +43,15 @@ public class Bench { Configuration configuration = Configuration.defaultConfiguration(); - if(optionAllwaysReturnList){ + if(flagWrap){ configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST); } - if(optionMergeMultiProps){ + if(flagMerge){ configuration = configuration.addOptions(Option.MERGE_MULTI_PROPS); } + if(flagSuppress){ + configuration = configuration.addOptions(Option.SUPPRESS_EXCEPTIONS); + } if (!optionAsValues) { configuration = configuration.addOptions(Option.AS_PATH_LIST); } @@ -129,8 +134,12 @@ public class Bench { } } - public List runAll() { - return asList(runJayway(), runBoon(), runNebhale()); + public Map runAll() { + Map res = new HashMap(); + res.put("jayway", runJayway()); + res.put("boon", runBoon()); + res.put("nebhale", runNebhale()); + return res; } private String getError(Exception e) { diff --git a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Result.java b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Result.java index bfaa6473..3aa8bebe 100644 --- a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Result.java +++ b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Result.java @@ -5,14 +5,12 @@ import com.jayway.jsonpath.internal.JsonFormatter; public class Result { public final String provider; - public final String active; public final long time; public final String result; public final String error; public Result(String provider, long time, String result, String error) { this.provider = provider; - this.active = provider == "jayway" ? "active" : ""; this.time = time; this.result = result != null ? JsonFormatter.prettyPrint(result) : result; this.error = error; diff --git a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/IndexResource.java b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/IndexResource.java index 502df7f6..264a65ce 100644 --- a/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/IndexResource.java +++ b/json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/IndexResource.java @@ -13,9 +13,11 @@ import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import java.util.HashMap; import java.util.List; @@ -23,7 +25,7 @@ import java.util.Map; import static java.util.Arrays.asList; -@Path("") +@Path("/") @Produces(MediaType.TEXT_HTML) public class IndexResource { @@ -32,22 +34,38 @@ public class IndexResource { public final static Map TEMPLATES = loadTemplates(); @GET - public Viewable get(@QueryParam("template") @DefaultValue("goessner") String template){ - return createView(TEMPLATES.get(template), "$.store.book[0].title", true, template, null); + @Path("templates/{template}") + @Produces(MediaType.APPLICATION_JSON) + public Response getTemplate(@PathParam("template") @DefaultValue("goessner") String template){ + return Response.ok(TEMPLATES.get(template)).build(); } + @POST + @Path("eval") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public Viewable post(@FormParam("json") String json, - @FormParam("path") String path, - @FormParam("type") String type, - @FormParam("template") String template){ + @Produces(MediaType.APPLICATION_JSON) + public Response getTemplate(@FormParam("json") String json, + @FormParam("path") String path, + @FormParam("type") String type, + @FormParam("flagWrap") boolean flagWrap, + @FormParam("flagMerge") boolean flagMerge, + @FormParam("flagSuppress") boolean flagSuppress ){ boolean value = "VALUE".equalsIgnoreCase(type); - return createView(json, path, value, template, new Bench(json, path, value, true, true).runAll()); + Map resultMap = new Bench(json, path, value, flagWrap, flagMerge, flagSuppress).runAll(); + + return Response.ok(resultMap).build(); } + @GET + public Viewable get(@QueryParam("template") @DefaultValue("goessner") String template){ + return createView(TEMPLATES.get(template), "$.store.book[0].title", true, template, null); + } + + + private Viewable createView(String json, String path, boolean value, String selectedTemplate, List results){ Map res = new HashMap(); res.put("results", results); @@ -70,6 +88,7 @@ public class IndexResource { String twentyK = IOUtils.toString(Thread.currentThread().getContextClassLoader().getResourceAsStream("json/20k.json")); Map templates = new HashMap(); + templates.put("blank", "null"); templates.put("goessner", goessner); templates.put("twitter", twitter); templates.put("webapp", webapp); diff --git a/json-path-web-test/src/main/resources/templates/index.mustache b/json-path-web-test/src/main/resources/templates/index.mustache index f8682c8d..3a6abbf2 100644 --- a/json-path-web-test/src/main/resources/templates/index.mustache +++ b/json-path-web-test/src/main/resources/templates/index.mustache @@ -6,79 +6,138 @@ JsonPath + + + + -
-
-

JSONPath evaluator

-
+

JSONPath evaluator

-
+
- + + + + +
- -
- -
- -
-
- +
- + - +
+ +
+ +
+
+
+ Result options +
+ +
+
+ +
+
+
+ +
+
+ Jayway options +
+ +
+
+ +
+
+ +
+
+
+
+
+ +
- {{#results}} -
-
- {{time}} millis -
- {{^error}} -
-
-
{{result}}
-
+ +
+
+  millis +
+
+
+

+                                
+
+

+
+ +
+
+  millis +
+
+
+

                                 
- {{/error}} - {{#error}} -

{{error}}

- {{/error}}
- {{/results}} -
+

+
+ +
+
+  millis +
+
+
+

+                                
+
+

+
+ +

 millis
@@ -88,31 +147,87 @@
+
\ No newline at end of file diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 3366120c..b0177052 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -165,9 +165,36 @@ public class JsonPath { * @param expected return type * @return object(s) matched by the given path */ + public T read(Object jsonObject, Configuration configuration) { - return (T)path.evaluate(jsonObject, configuration).getWithOptions(); + boolean optAsPathList = configuration.getOptions().contains(Option.AS_PATH_LIST); + boolean optAlwaysReturnList = configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST); + boolean optSuppressExceptions = configuration.getOptions().contains(Option.SUPPRESS_EXCEPTIONS); + boolean optThrowOnMissingProperty = configuration.getOptions().contains(Option.THROW_ON_MISSING_PROPERTY); + + Object result = null; + try { + result = path.evaluate(jsonObject, configuration).getWithOptions(); + } catch (RuntimeException e){ + if(optThrowOnMissingProperty || (!optThrowOnMissingProperty && !optSuppressExceptions)){ + throw e; + } else { + if(optSuppressExceptions){ + if(path.isDefinite() && !optAlwaysReturnList && !optAsPathList){ + result = null; + } else { + result = configuration.getProvider().createArray(); + } + } + } + } + return (T)result; } + /* + public T read(Object jsonObject, Configuration configuration) { + + return (T)path.evaluate(jsonObject, configuration).getWithOptions(); + }*/ /** * Applies this JsonPath to the provided json string diff --git a/json-path/src/main/java/com/jayway/jsonpath/Option.java b/json-path/src/main/java/com/jayway/jsonpath/Option.java index 89bf1c53..cde719cd 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Option.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Option.java @@ -34,7 +34,16 @@ public enum Option { /** * When multiple properties are queried eg @..['foo', 'bar'] these properties are extracted and put in a new Map. */ - MERGE_MULTI_PROPS + MERGE_MULTI_PROPS, + + /** + * Suppress all exceptions when evaluating path. + *
+ * If an exception is thrown and the option {@link Option#ALWAYS_RETURN_LIST} an empty list is returned. + * If an exception is thrown and the option {@link Option#ALWAYS_RETURN_LIST} is not present null is returned. + * The option {@link Option#THROW_ON_MISSING_PROPERTY} has precedence over this option. + */ + SUPPRESS_EXCEPTIONS } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java index 6e66050f..1cf04ef8 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/EvaluationContextImpl.java @@ -59,8 +59,10 @@ class EvaluationContextImpl implements EvaluationContext { @Override public Object getWithOptions() { - if(configuration.getOptions().contains(Option.AS_PATH_LIST)) { + boolean optAsPathList = configuration.getOptions().contains(Option.AS_PATH_LIST); + boolean optAlwaysReturnList = configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST); + if (optAsPathList) { Object array = configuration.getProvider().createArray(); int i = 0; for (String p : pathResult) { @@ -68,11 +70,8 @@ class EvaluationContextImpl implements EvaluationContext { i++; } return array; - - } else if(configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST)){ - + } else if (optAlwaysReturnList) { return objectResult; - } else { return get(); }