From f40063bf02c3af6634032183650c6783983cd085 Mon Sep 17 00:00:00 2001 From: Matt Greenwood Date: Sun, 28 Jun 2015 13:58:46 -0400 Subject: [PATCH] added case for length of map or JSONArray of values --- .../java/com/jayway/jsonpath/internal/function/Length.java | 7 +++++++ .../jayway/jsonpath/functions/JSONEntityFunctionTest.java | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java b/json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java index 747b93b4..1137049c 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/function/Length.java +++ b/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; } } \ No newline at end of file diff --git a/json-path/src/test/java/com/jayway/jsonpath/functions/JSONEntityFunctionTest.java b/json-path/src/test/java/com/jayway/jsonpath/functions/JSONEntityFunctionTest.java index 2bcf5179..bcd1d073 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/functions/JSONEntityFunctionTest.java +++ b/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