Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for getting the length of an array. #100

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ allprojects {
ext.displayName = null
ext.buildTimestamp = new Date().format('yyyy-MM-dd HH:mm:ss')

apply plugin: 'maven'
group = 'com.jayway.jsonpath'
version = '2.0.1' + (snapshotVersion ? "-SNAPSHOT" : "")

Expand Down Expand Up @@ -75,4 +76,4 @@ task wrapper(type: Wrapper) {

//Task used by Heroku for staging
task stage(dependsOn: [':json-path-web-test:clean', 'json-path-web-test:jar', 'json-path-web-test:shadowJar']) {}
apply from: "$rootDir/gradle/binaryCompatibility.gradle"
apply from: "$rootDir/gradle/binaryCompatibility.gradle"
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ void handleArrayIndex(int index, String currentPath, Object model, EvaluationCon
}
}

void handleArrayLength(String currentPath, Object model, EvaluationContextImpl ctx) {
String evalPath = currentPath + ".length";
PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, "length") : PathRef.NO_OP;
Object evalHit = ctx.jsonProvider().length(model);
if (isLeaf()) {
ctx.addResult(evalPath, pathRef, evalHit);
} else {
next().evaluate(evalPath, pathRef, evalHit, ctx);
}
}

PathToken prev(){
return prev;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public List<String> getProperties() {
@Override
public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
if (!ctx.jsonProvider().isMap(model)) {
// Special case handling of the 'length' property of arrays.
if (isLeaf() && ctx.jsonProvider().isArray(model) && properties.size() == 1 && properties.get(0).equals("length")) {
handleArrayLength(currentPath, model, ctx);
return;
}
throw new PathNotFoundException("Property " + getPathFragment() + " not found in path " + currentPath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public void test_type_ref() throws IOException {
assertThat(using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2");
}

@Test
public void length_of_array() {
assertThat(using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.length", Integer.class)).isEqualTo(4);
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.length", Integer.class)).isEqualTo(4);
assertThat(using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.length", Integer.class)).isEqualTo(4);
assertThat(using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book.length", Integer.class)).isEqualTo(4);
}

public static class FooBarBaz<T> {
public T gen;
Expand Down