Skip to content

Commit 1e18a13

Browse files
authored
Merge pull request #440 from sergey-sw/master
Add keys function for Map objects
2 parents ba35cf9 + 7355e30 commit 1e18a13

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jayway.jsonpath.InvalidPathException;
44
import com.jayway.jsonpath.internal.function.json.Append;
5+
import com.jayway.jsonpath.internal.function.json.KeySetFunction;
56
import com.jayway.jsonpath.internal.function.numeric.Average;
67
import com.jayway.jsonpath.internal.function.numeric.Max;
78
import com.jayway.jsonpath.internal.function.numeric.Min;
@@ -44,6 +45,7 @@ public class PathFunctionFactory {
4445
map.put("length", Length.class);
4546
map.put("size", Length.class);
4647
map.put("append", Append.class);
48+
map.put("keys", KeySetFunction.class);
4749

4850

4951
FUNCTIONS = Collections.unmodifiableMap(map);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.jayway.jsonpath.internal.function.json;
2+
3+
import com.jayway.jsonpath.internal.EvaluationContext;
4+
import com.jayway.jsonpath.internal.PathRef;
5+
import com.jayway.jsonpath.internal.function.Parameter;
6+
import com.jayway.jsonpath.internal.function.PathFunction;
7+
8+
import java.util.List;
9+
10+
/**
11+
* Author: Sergey Saiyan sergey.sova42@gmail.com
12+
* Created at 21/02/2018.
13+
*/
14+
public class KeySetFunction implements PathFunction {
15+
16+
@Override
17+
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
18+
if (ctx.configuration().jsonProvider().isMap(model)) {
19+
return ctx.configuration().jsonProvider().getPropertyKeys(model);
20+
}
21+
return null;
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.jayway.jsonpath.internal.function;
2+
3+
import com.jayway.jsonpath.Configuration;
4+
import com.jayway.jsonpath.Configurations;
5+
import org.apache.commons.io.IOUtils;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
import java.util.HashSet;
10+
11+
/**
12+
* Author: Sergey Saiyan sergey.sova42@gmail.com
13+
* Created at 21/02/2018.
14+
*/
15+
public class KeySetFunctionTest extends BaseFunctionTest {
16+
17+
private Configuration conf = Configurations.JACKSON_CONFIGURATION;
18+
19+
@Test
20+
public void testKeySet() throws Exception {
21+
String json = IOUtils.toString(getClass().getResourceAsStream("/keyset.json"));
22+
verifyFunction(conf, "$.data.keys()", json, new HashSet<String>(Arrays.asList("a", "b")));
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"data" : {
3+
"a" : "a",
4+
"b" : "b"
5+
}
6+
}

0 commit comments

Comments
 (0)