未验证 提交 1e18a13b 编写于 作者: K kallestenflo 提交者: GitHub

Merge pull request #440 from sergey-sw/master

Add keys function for Map objects
......@@ -2,6 +2,7 @@ package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.internal.function.json.Append;
import com.jayway.jsonpath.internal.function.json.KeySetFunction;
import com.jayway.jsonpath.internal.function.numeric.Average;
import com.jayway.jsonpath.internal.function.numeric.Max;
import com.jayway.jsonpath.internal.function.numeric.Min;
......@@ -44,6 +45,7 @@ public class PathFunctionFactory {
map.put("length", Length.class);
map.put("size", Length.class);
map.put("append", Append.class);
map.put("keys", KeySetFunction.class);
FUNCTIONS = Collections.unmodifiableMap(map);
......
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 sergey.sova42@gmail.com
* 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;
}
}
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 sergey.sova42@gmail.com
* 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")));
}
}
{
"data" : {
"a" : "a",
"b" : "b"
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册