Browse Source

Removed MERGE_MULTI_PROP option (not part of spec).

pull/47/head
Kalle Stenflo 11 years ago
parent
commit
cb6a2e805a
  1. 1
      Procfile
  2. 7
      json-path-web-test/src/main/java/com/jayway/jsonpath/web/bench/Bench.java
  3. 3
      json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java
  4. 7
      json-path-web-test/src/main/resources/webapp/index.html
  5. 3
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  6. 30
      json-path/src/main/java/com/jayway/jsonpath/Option.java
  7. 26
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PathToken.java
  8. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PropertyPathToken.java
  9. 70
      json-path/src/test/java/com/jayway/jsonpath/OptionsTest.java
  10. 1
      system.properties

1
Procfile

@ -0,0 +1 @@
web: java $JAVA_OPTS -Dserver.http.port=$PORT -jar web/build/distributions/trumpet-server-1.0.0-shadow.jar

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

@ -23,16 +23,14 @@ public class Bench {
protected final String path; protected final String path;
private final boolean optionAsValues; private final boolean optionAsValues;
private final boolean flagWrap; private final boolean flagWrap;
private final boolean flagMerge;
private final boolean flagSuppress; private final boolean flagSuppress;
private final boolean flagNullLeaf; private final boolean flagNullLeaf;
public Bench(String json, String path, boolean optionAsValues, boolean flagWrap, boolean flagMerge, boolean flagSuppress, boolean flagNullLeaf) { public Bench(String json, String path, boolean optionAsValues, boolean flagWrap, boolean flagSuppress, boolean flagNullLeaf) {
this.json = json; this.json = json;
this.path = path; this.path = path;
this.optionAsValues = optionAsValues; this.optionAsValues = optionAsValues;
this.flagWrap = flagWrap; this.flagWrap = flagWrap;
this.flagMerge = flagMerge;
this.flagSuppress = flagSuppress; this.flagSuppress = flagSuppress;
this.flagNullLeaf = flagNullLeaf; this.flagNullLeaf = flagNullLeaf;
} }
@ -48,9 +46,6 @@ public class Bench {
if(flagWrap){ if(flagWrap){
configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST); configuration = configuration.addOptions(Option.ALWAYS_RETURN_LIST);
} }
if(flagMerge){
configuration = configuration.addOptions(Option.MERGE_MULTI_PROPS);
}
if(flagSuppress){ if(flagSuppress){
configuration = configuration.addOptions(Option.SUPPRESS_EXCEPTIONS); configuration = configuration.addOptions(Option.SUPPRESS_EXCEPTIONS);
} }

3
json-path-web-test/src/main/java/com/jayway/jsonpath/web/resource/ApiResource.java

@ -48,13 +48,12 @@ public class ApiResource {
@FormParam("path") String path, @FormParam("path") String path,
@FormParam("type") String type, @FormParam("type") String type,
@FormParam("flagWrap") boolean flagWrap, @FormParam("flagWrap") boolean flagWrap,
@FormParam("flagMerge") boolean flagMerge,
@FormParam("flagNullLeaf") boolean flagNullLeaf, @FormParam("flagNullLeaf") boolean flagNullLeaf,
@FormParam("flagSuppress") boolean flagSuppress ){ @FormParam("flagSuppress") boolean flagSuppress ){
boolean value = "VALUE".equalsIgnoreCase(type); boolean value = "VALUE".equalsIgnoreCase(type);
Map<String, Result> resultMap = new Bench(json, path, value, flagWrap, flagMerge, flagSuppress, flagNullLeaf).runAll(); Map<String, Result> resultMap = new Bench(json, path, value, flagWrap, flagSuppress, flagNullLeaf).runAll();
return Response.ok(resultMap).build(); return Response.ok(resultMap).build();
} }

7
json-path-web-test/src/main/resources/webapp/index.html

@ -77,12 +77,6 @@
Always return result list Always return result list
</label> </label>
</div> </div>
<div class="checkbox">
<label>
<input type="checkbox" name="flagMerge" id="cbFlagMerge" />
Merge multi props to new object
</label>
</div>
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" name="flagNullLeaf" id="cbFlagNullLeaf" /> <input type="checkbox" name="flagNullLeaf" id="cbFlagNullLeaf" />
@ -232,7 +226,6 @@
path: path, path: path,
type: $('input[name=rbType]:checked').val(), type: $('input[name=rbType]:checked').val(),
flagWrap: $('#cbFlagWrap').prop('checked'), flagWrap: $('#cbFlagWrap').prop('checked'),
flagMerge: $('#cbFlagMerge').prop('checked'),
flagNullLeaf: $('#cbFlagNullLeaf').prop('checked'), flagNullLeaf: $('#cbFlagNullLeaf').prop('checked'),
flagSuppress: $('#cbFlagSuppress').prop('checked') flagSuppress: $('#cbFlagSuppress').prop('checked')
} }

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

@ -383,8 +383,7 @@ public class JsonPath {
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
public static <T> T read(Object json, String jsonPath, Predicate... filters) { public static <T> T read(Object json, String jsonPath, Predicate... filters) {
//return compile(jsonPath, filters).read(json); return parse(json).read(jsonPath, filters);
return new JsonReader().parse(json).read(jsonPath, filters);
} }
/** /**

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

@ -17,9 +17,29 @@ package com.jayway.jsonpath;
public enum Option { public enum Option {
/** /**
* Throw {@link PathNotFoundException} when JsonPath tries to read a property that does not exists. * returns <code>null</code> for missing leaf.
*
* <pre>
* [
* {
* "foo" : "foo1",
* "bar" : "bar1"
* }
* {
* "foo" : "foo2"
* }
* ]
*</pre>
*
* the path :
*
* "$[*].bar"
*
* Without flag ["bar1"] is returned
* With flag ["bar1", null] is returned
*
*
*/ */
//THROW_ON_MISSING_PROPERTY,
DEFAULT_PATH_LEAF_TO_NULL, DEFAULT_PATH_LEAF_TO_NULL,
/** /**
@ -32,17 +52,11 @@ public enum Option {
*/ */
AS_PATH_LIST, AS_PATH_LIST,
/**
* When multiple properties are queried eg @..['foo', 'bar'] these properties are extracted and put in a new Map.
*/
MERGE_MULTI_PROPS,
/** /**
* Suppress all exceptions when evaluating path. * Suppress all exceptions when evaluating path.
* <br/> * <br/>
* 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} an empty list is returned.
* If an exception is thrown and the option {@link Option#ALWAYS_RETURN_LIST} is not present null 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 SUPPRESS_EXCEPTIONS

26
json-path/src/main/java/com/jayway/jsonpath/internal/compiler/PathToken.java

@ -52,10 +52,9 @@ public abstract class PathToken {
if (!isLeaf()) { if (!isLeaf()) {
throw new InvalidPathException("Multi properties can only be used as path leafs: " + evalPath); throw new InvalidPathException("Multi properties can only be used as path leafs: " + evalPath);
} }
for (String property : properties) {
if(ctx.configuration().containsOption(Option.MERGE_MULTI_PROPS)) { evalPath = currentPath + "['" + property + "']";
Object map = ctx.jsonProvider().createMap(); if(hasProperty(property, model, ctx)) {
for (String property : properties) {
Object propertyVal = readObjectProperty(property, model, ctx); Object propertyVal = readObjectProperty(property, model, ctx);
if(propertyVal == JsonProvider.UNDEFINED){ if(propertyVal == JsonProvider.UNDEFINED){
if(ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)){ if(ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)){
@ -64,24 +63,7 @@ public abstract class PathToken {
continue; continue;
} }
} }
ctx.jsonProvider().setProperty(map, property, propertyVal); ctx.addResult(evalPath, propertyVal);
}
ctx.addResult(evalPath, map);
} else {
for (String property : properties) {
evalPath = currentPath + "['" + property + "']";
if(hasProperty(property, model, ctx)) {
Object propertyVal = readObjectProperty(property, model, ctx);
if(propertyVal == JsonProvider.UNDEFINED){
if(ctx.options().contains(Option.DEFAULT_PATH_LEAF_TO_NULL)){
propertyVal = null;
} else {
continue;
}
}
ctx.addResult(evalPath, propertyVal);
}
} }
} }
} }

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

@ -31,7 +31,7 @@ public class PropertyPathToken extends PathToken {
@Override @Override
boolean isTokenDefinite() { boolean isTokenDefinite() {
return true; return properties.size() == 1;
} }
@Override @Override

70
json-path/src/test/java/com/jayway/jsonpath/OptionsTest.java

@ -2,15 +2,81 @@ package com.jayway.jsonpath;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.jayway.jsonpath.JsonPath.using;
import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST;
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
import static com.jayway.jsonpath.Option.DEFAULT_PATH_LEAF_TO_NULL;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
public class OptionsTest extends BaseTest { public class OptionsTest extends BaseTest {
@Test(expected = PathNotFoundException.class)
public void a_leafs_is_not_defaulted_to_null() {
Configuration conf = Configuration.defaultConfiguration();
assertThat(using(conf).parse(singletonMap("foo", "bar")).read("$.baz")).isNull();
}
@Test
public void a_leafs_can_be_defaulted_to_null() {
Configuration conf = Configuration.builder().options(DEFAULT_PATH_LEAF_TO_NULL).build();
assertThat(using(conf).parse(singletonMap("foo", "bar")).read("$.baz")).isNull();
}
@Test
public void a_definite_path_is_not_returned_as_list_by_default() {
Configuration conf = Configuration.defaultConfiguration();
assertThat(using(conf).parse(singletonMap("foo", "bar")).read("$.foo")).isInstanceOf(String.class);
}
@Test
public void a_definite_path_can_be_returned_as_list() {
Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build();
assertThat(using(conf).parse(singletonMap("foo", "bar")).read("$.foo")).isInstanceOf(List.class);
}
@Test @Test
public void leafs_can_be_defaulted_to_null() { public void a_path_evaluation_is_returned_as_VALUE_by_default() {
Configuration conf = Configuration.defaultConfiguration();
assertThat(using(conf).parse(singletonMap("foo", "bar")).read("$.foo")).isEqualTo("bar");
}
//assertThat(parse(singletonMap("foo", "bar"))). @Test
public void a_path_evaluation_can_be_returned_as_PATH_LIST() {
Configuration conf = Configuration.builder().options(AS_PATH_LIST).build();
List<String> pathList = using(conf).parse(singletonMap("foo", "bar")).read("$.foo");
assertThat(pathList).containsOnly("$['foo']");
} }
@Test
public void multi_properties_are_not_merged_by_default() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("a", "a");
model.put("b", "b");
model.put("c", "c");
Configuration conf = Configuration.defaultConfiguration();
Object result = using(conf).parse(model).read("$.['a', 'b']");
assertThat(result).isInstanceOf(List.class);
assertThat((List)result).containsOnly("a", "b");
}
} }

1
system.properties

@ -0,0 +1 @@
java.runtime.version=1.6
Loading…
Cancel
Save