From 0d8192170ab37771d05f4c006b2060764913b572 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Thu, 12 Jun 2014 10:36:23 +0200 Subject: [PATCH] Code clean up and JacksonProvider improvements. --- .../com/jayway/jsonpath/web/bench/Bench.java | 58 +++---- .../jsonpath/web/resource/IndexResource.java | 2 +- .../com/jayway/jsonpath/Configuration.java | 10 +- .../main/java/com/jayway/jsonpath/Option.java | 11 +- .../internal/spi/compiler/ArrayPathToken.java | 148 ++++++++++-------- .../internal/spi/compiler/CompiledPath.java | 10 -- .../internal/spi/compiler/PathCompiler.java | 16 +- .../internal/spi/compiler/PathToken.java | 56 ++++++- .../spi/compiler/PropertyPathToken.java | 23 +-- .../internal/spi/compiler/ScanPathToken.java | 3 +- .../spi/compiler/WildcardPathToken.java | 4 +- .../internal/spi/json/JacksonProvider.java | 21 +-- .../jayway/jsonpath/spi/compiler/Path.java | 3 +- .../spi/json/JsonProviderFactory.java | 6 +- .../test/java/com/jayway/jsonpath/Runner.java | 4 +- 15 files changed, 215 insertions(+), 160 deletions(-) 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 d3912d33..02f19c18 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 @@ -21,12 +21,16 @@ public class Bench { protected final String json; protected final String path; - private final boolean value; + private final boolean optionAsValues; + private final boolean optionAllwaysReturnList; + private final boolean optionMergeMultiProps; - public Bench(String json, String path, boolean value) { + public Bench(String json, String path, boolean optionAsValues, boolean optionAllwaysReturnList, boolean optionMergeMultiProps) { this.json = json; this.path = path; - this.value = value; + this.optionAsValues = optionAsValues; + this.optionAllwaysReturnList = optionAllwaysReturnList; + this.optionMergeMultiProps = optionMergeMultiProps; } public Result runJayway() { @@ -35,33 +39,31 @@ public class Bench { long time; Object res = null; - //Configuration configuration = Configuration.defaultConfiguration(); - Configuration configuration = Configuration.defaultConfiguration().options(Option.ALWAYS_RETURN_LIST); - if(!value){ - configuration = configuration.options(Option.AS_PATH_LIST); + + Configuration configuration = Configuration.defaultConfiguration(); + if(optionAllwaysReturnList){ + configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST); + } + if(optionMergeMultiProps){ + configuration = configuration.addOptions(Option.MERGE_MULTI_PROPS); + } + if (!optionAsValues) { + configuration = configuration.addOptions(Option.AS_PATH_LIST); } long now = System.currentTimeMillis(); try { - res = JsonPath.using(configuration).parse(json).read(path); - /* - if(value) { - res = JsonPath.parse(json).read(path); - } else { - res = JsonPath.parse(json).readPathList(path); - }*/ - - } catch (Exception e){ + } catch (Exception e) { error = getError(e); } finally { time = System.currentTimeMillis() - now; - if(res instanceof String) { + if (res instanceof String) { result = "\"" + res + "\""; - } else if(res instanceof Number) { + } else if (res instanceof Number) { result = res.toString(); - } else if(res instanceof Boolean){ + } else if (res instanceof Boolean) { result = res.toString(); } else { result = res != null ? JsonProviderFactory.createProvider().toJson(res) : "null"; @@ -78,7 +80,7 @@ public class Bench { Iterator query = null; long now = System.currentTimeMillis(); try { - if(!value){ + if (!optionAsValues) { throw new UnsupportedOperationException("Not supported!"); } io.gatling.jsonpath.JsonPath jsonPath = JsonPath$.MODULE$.compile(path).right().get(); @@ -87,12 +89,12 @@ public class Bench { Object jsonModel = jsonParser.parse(json); query = jsonPath.query(jsonModel); - } catch (Exception e){ + } catch (Exception e) { error = getError(e); } finally { time = System.currentTimeMillis() - now; - if(query != null) { + if (query != null) { List res = new ArrayList(); while (query.hasNext()) { res.add(query.next()); @@ -113,27 +115,27 @@ public class Bench { long now = System.currentTimeMillis(); try { - if(!value){ + if (!optionAsValues) { throw new UnsupportedOperationException("Not supported!"); } com.nebhale.jsonpath.JsonPath compiled = com.nebhale.jsonpath.JsonPath.compile(path); res = compiled.read(json, Object.class); - } catch (Exception e){ + } catch (Exception e) { error = getError(e); } finally { time = System.currentTimeMillis() - now; - result = res!=null? jacksonProvider.toJson(res):null; + result = res != null ? jacksonProvider.toJson(res) : null; return new Result("nebhale", time, result, error); } } - public List runAll(){ + public List runAll() { return asList(runJayway(), runBoon(), runNebhale()); } - private String getError(Exception e){ + private String getError(Exception e) { String ex = e.getMessage(); - if(ex == null || ex.trim().isEmpty()){ + if (ex == null || ex.trim().isEmpty()) { ex = "Undefined error"; } return ex; 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 167ca590..502df7f6 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 @@ -45,7 +45,7 @@ public class IndexResource { boolean value = "VALUE".equalsIgnoreCase(type); - return createView(json, path, value, template, new Bench(json, path, value).runAll()); + return createView(json, path, value, template, new Bench(json, path, value, true, true).runAll()); } private Viewable createView(String json, String path, boolean value, String selectedTemplate, List results){ diff --git a/json-path/src/main/java/com/jayway/jsonpath/Configuration.java b/json-path/src/main/java/com/jayway/jsonpath/Configuration.java index 7fb70bc9..a1d1a092 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Configuration.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Configuration.java @@ -17,12 +17,12 @@ package com.jayway.jsonpath; import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProviderFactory; -import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.Set; import static com.jayway.jsonpath.internal.Utils.notNull; +import static java.util.Arrays.asList; public class Configuration { @@ -44,6 +44,12 @@ public class Configuration { return provider; } + public Configuration addOptions(Option... options) { + EnumSet