public void set(Object newVal, Configuration configuration){ for (String property : properties) { configuration.jsonProvider().setProperty(parent, property, newVal); } } public void convert(MapFunction mapFunction, Configuration configuration) {
public void delete(Configuration configuration){ for (String property : properties) { configuration.jsonProvider().removeProperty(parent, property); } }
@Override public List<String> getPathList() { List<String> res = new ArrayList<String>(); if(resultIndex > 0){ Iterable<?> objects = configuration.jsonProvider().toIterable(pathResult); for (Object o : objects) { res.add((String)o); } } return res; }
@Override public void add(Object newVal, Configuration configuration) { if(configuration.jsonProvider().isArray(parent)){ configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal); } else { throw new InvalidModificationException("Invalid add operation. $ is not an array"); } }
public void add(Object value, Configuration configuration){ Object target = configuration.jsonProvider().getMapValue(parent, property); if(targetInvalid(target)){ return; } if(configuration.jsonProvider().isArray(target)){ configuration.jsonProvider().setArrayIndex(target, configuration.jsonProvider().length(target), value); } else { throw new InvalidModificationException("Can only add to an array"); } }
public void convert(MapFunction mapFunction, Configuration configuration) { for (String property : properties) { Object currentValue = configuration.jsonProvider().getMapValue(parent, property); if (currentValue != JsonProvider.UNDEFINED) { configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration)); } } }
@Override public void convert(MapFunction mapFunction, Configuration configuration) { Object currentValue = configuration.jsonProvider().getMapValue(parent, property); configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration)); }
@Override public void put(String key, Object newVal, Configuration configuration) { if(configuration.jsonProvider().isMap(parent)){ configuration.jsonProvider().setProperty(parent, key, newVal); } else { throw new InvalidModificationException("Invalid put operation. $ is not a map"); } }
public void put(String key, Object value, Configuration configuration){ Object target = configuration.jsonProvider().getArrayIndex(parent, index); if(targetInvalid(target)){ return; } if(configuration.jsonProvider().isMap(target)){ configuration.jsonProvider().setProperty(target, key, value); } else { throw new InvalidModificationException("Can only add properties to a map"); } }
public void put(String key, Object value, Configuration configuration){ Object target = configuration.jsonProvider().getMapValue(parent, property); if(targetInvalid(target)){ return; } if(configuration.jsonProvider().isMap(target)){ configuration.jsonProvider().setProperty(target, key, value); } else { throw new InvalidModificationException("Can only add properties to a map"); } }
@Override public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) { if(ctx.configuration().jsonProvider().isArray(model)){ return ctx.configuration().jsonProvider().length(model); } else if(ctx.configuration().jsonProvider().isMap(model)){ return ctx.configuration().jsonProvider().length(model); } return null; } }
@Override public DocumentContext parse(String json) { notEmpty(json, "json string can not be null or empty"); Object obj = configuration.jsonProvider().parse(json); return new JsonContext(obj, configuration); }
@Override public void renameKey(String oldKeyName, String newKeyName, Configuration configuration) { Object target = configuration.jsonProvider().getMapValue(parent, property); if(targetInvalid(target)){ return; } renameInMap(target, oldKeyName, newKeyName, configuration); }
@Test public void escaped_literals() { if(conf.jsonProvider().getClass().getSimpleName().startsWith("Jackson")){ return; } assertHasOneResult("[\"\\'foo\"]", "$[?(@ == '\\'foo')]", conf); }
@Override public DocumentContext parse(InputStream json, String charset) { notNull(json, "json input stream can not be null"); notNull(charset, "charset can not be null"); try { Object obj = configuration.jsonProvider().parse(json, charset); return new JsonContext(obj, configuration); } finally { Utils.closeQuietly(json); } }
@Override public boolean apply(PredicateContext ctx) { if (ctx.configuration().jsonProvider().getMapValue(ctx.item(), "name").equals("rootGrandChild_A")) { return true; } return false; } };
/** * Shortcut for counting found nodes. * @param json json to be parsed * @param path path to be evaluated * @param expectedResultCount expected number of nodes to be found * @param conf conf to use during evaluation */ public static void assertHasResults(final String json, final String path, final int expectedResultCount, Configuration conf) { Object result = JsonPath.using(conf).parse(json).read(path); assertThat(conf.jsonProvider().length(result)).isEqualTo(expectedResultCount); } }
@Test public void testFilterWithOrShortCircuit1() throws Exception { Object json = Configuration.defaultConfiguration().jsonProvider().parse( "{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}"); assertThat(Filter.parse("[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Doe')]").apply(createPredicateContext(json))).isFalse(); }
@Test public void testFilterWithOrShortCircuit2() throws Exception { Object json = Configuration.defaultConfiguration().jsonProvider().parse("{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}"); assertThat(Filter.parse("[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Smith')]").apply(createPredicateContext(json))).isTrue(); }
@Test public void filters_can_contain_json_path_expressions() throws Exception { Object doc = Configuration.defaultConfiguration().jsonProvider().parse(DOCUMENT); assertFalse(filter(where("$.store.bicycle.color").ne("red")).apply(createPredicateContext(doc))); }