Browse Source

added case for length of map or JSONArray of values

pull/103/head
Matt Greenwood 9 years ago
parent
commit
f40063bf02
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java
  2. 6
      json-path/src/test/java/com/jayway/jsonpath/functions/JSONEntityFunctionTest.java

7
json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java

@ -5,6 +5,10 @@ import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import net.minidev.json.JSONArray;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Provides the length of a JSONArray Object
*
@ -18,6 +22,9 @@ public class Length implements Function {
JSONArray array = (JSONArray)model;
return Integer.valueOf(array.size());
}
else if (model instanceof Map) {
return Integer.valueOf(((Map) model).size());
}
return null;
}
}

6
json-path/src/test/java/com/jayway/jsonpath/functions/JSONEntityFunctionTest.java

@ -60,6 +60,12 @@ public class JSONEntityFunctionTest extends BaseFunctionTest {
verifyFunction("$.numbers.%length()", NUMBER_SERIES, 10);
}
@Test
public void testLengthOfStructure() {
verifyFunction("$.batches.%length()", BATCH_JSON, 2);
}
/**
* The fictitious use-case/story - is we have a collection of batches with values indicating some quality metric.
* We want to determine the average of the values for only the batch's values where the number of items in the batch

Loading…
Cancel
Save