-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from sergey-sw/master
Add keys function for Map objects
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
json-path/src/main/java/com/jayway/jsonpath/internal/function/json/KeySetFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.jayway.jsonpath.internal.function.json; | ||
|
||
import com.jayway.jsonpath.internal.EvaluationContext; | ||
import com.jayway.jsonpath.internal.PathRef; | ||
import com.jayway.jsonpath.internal.function.Parameter; | ||
import com.jayway.jsonpath.internal.function.PathFunction; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Author: Sergey Saiyan [email protected] | ||
* Created at 21/02/2018. | ||
*/ | ||
public class KeySetFunction implements PathFunction { | ||
|
||
@Override | ||
public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { | ||
if (ctx.configuration().jsonProvider().isMap(model)) { | ||
return ctx.configuration().jsonProvider().getPropertyKeys(model); | ||
} | ||
return null; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
json-path/src/test/java/com/jayway/jsonpath/internal/function/KeySetFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.jayway.jsonpath.internal.function; | ||
|
||
import com.jayway.jsonpath.Configuration; | ||
import com.jayway.jsonpath.Configurations; | ||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
|
||
/** | ||
* Author: Sergey Saiyan [email protected] | ||
* Created at 21/02/2018. | ||
*/ | ||
public class KeySetFunctionTest extends BaseFunctionTest { | ||
|
||
private Configuration conf = Configurations.JACKSON_CONFIGURATION; | ||
|
||
@Test | ||
public void testKeySet() throws Exception { | ||
String json = IOUtils.toString(getClass().getResourceAsStream("/keyset.json")); | ||
verifyFunction(conf, "$.data.keys()", json, new HashSet<String>(Arrays.asList("a", "b"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"data" : { | ||
"a" : "a", | ||
"b" : "b" | ||
} | ||
} |