Refine search
/** * Creates a new configuration by adding the new options to the options used in this configuration. * @param options options to add * @return a new configuration */ public Configuration addOptions(Option... options) { EnumSet<Option> opts = EnumSet.noneOf(Option.class); opts.addAll(this.options); opts.addAll(asList(options)); return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(opts).evaluationListener(evaluationListeners).build(); }
public ValueNode evaluate(Predicate.PredicateContext ctx) { if (isExistsCheck()) { try { Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options(Option.REQUIRE_PROPERTIES).build(); Object result = path.evaluate(ctx.item(), ctx.root(), c).getValue(false); return result == JsonProvider.UNDEFINED ? FALSE : TRUE; res = path.evaluate(doc, ctx.root(), ctx.configuration()).getValue(); res = ctx.configuration().jsonProvider().unwrap(res);
public JsonPathTest() { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); _baseConfiguration = b.build(); _oneNtpServerNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/oneNtpServer.json"); _oneNtpServerNodesAnswerJsonObject = JsonPath.parse(_oneNtpServerNodesAnswerStr, _baseConfiguration).json(); _twoNtpServersNodesAnswerStr = CommonUtil.readResource("org/batfish/question/jsonpath/twoNtpServers.json"); _twoNtpServersNodesAnswerJsonObject = JsonPath.parse(_twoNtpServersNodesAnswerStr, _baseConfiguration).json(); ConfigurationBuilder prefixCb = new ConfigurationBuilder(); prefixCb.mappingProvider(_baseConfiguration.mappingProvider()); prefixCb.jsonProvider(_baseConfiguration.jsonProvider()); prefixCb.evaluationListener(_baseConfiguration.getEvaluationListeners()); prefixCb.options(_baseConfiguration.getOptions()); prefixCb.options(Option.ALWAYS_RETURN_LIST); prefixCb.options(Option.AS_PATH_LIST); _prefixConfiguration = prefixCb.build(); }
private static void maskList(DocumentContext ctx, String jsonPath, String expression) { ctx.configuration().addOptions(Option.AS_PATH_LIST); Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build(); DocumentContext context = JsonPath.using(conf).parse(ctx.jsonString()); List<String> pathList = context.read(jsonPath); /** * when reach here, ctx.read(jsonPath) should only give us a list of strings so that we can replace with MASK_REPLACEMENT_CHAR * list of values can belongs to a same path or different paths, we should treat differently. * two situations: * an array contains multiple String values like: "list": ["ab", "cd", "ef] * or single value belongs to different paths. */ if(pathList != null && pathList.size() == 1) { String path = pathList.get(0); List values = ctx.read(path); JSONArray maskedValue = new JSONArray(); //mask each value in the list of the same path values.forEach(o -> maskedValue.add(replaceWithMask(o.toString(), MASK_REPLACEMENT_CHAR.charAt(0), expression))); ctx.set(path, maskedValue); } else { for (String path : Optional.ofNullable(pathList).orElse(Collections.emptyList())) { Object value = ctx.read(path); ctx.set(path, replaceWithMask(value.toString(), MASK_REPLACEMENT_CHAR.charAt(0), expression)); } } }
@Test public void evaluation_listeners_can_be_cleared() { EvaluationListener listener = new EvaluationListener() { @Override public EvaluationContinuation resultFound(FoundResult found) { return EvaluationContinuation.CONTINUE; } }; Configuration configuration1 = Configuration.builder().evaluationListener(listener).build(); Configuration configuration2 = configuration1.setEvaluationListeners(); assertThat(configuration1.getEvaluationListeners()).hasSize(1); assertThat(configuration2.getEvaluationListeners()).hasSize(0); } }
public ValueNode evaluate(Predicate.PredicateContext ctx) { if (isExistsCheck()) { try { Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options(Option.REQUIRE_PROPERTIES).build(); Object result = path.evaluate(ctx.item(), ctx.root(), c).getValue(false); return result == JsonProvider.UNDEFINED ? ValueNode.FALSE : ValueNode.TRUE; res = path.evaluate(doc, ctx.root(), ctx.configuration()).getValue(); res = ctx.configuration().jsonProvider().unwrap(res);
/** * Creates a new configuration based on default values * @return a new configuration based on defaults */ public static Configuration defaultConfiguration() { Defaults defaults = getEffectiveDefaults(); return Configuration.builder().jsonProvider(defaults.jsonProvider()).options(defaults.options()).build(); }
public static Configuration getDefaulConfiguration() { Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); ConfigurationBuilder b = new ConfigurationBuilder(); final Configuration c = b.build(); return c; }
/** * Returns a new ConfigurationBuilder * @return a builder */ public static ConfigurationBuilder builder() { return new ConfigurationBuilder(); }
/** * Returns a new ConfigurationBuilder * @return a builder */ public static ConfigurationBuilder builder() { return new ConfigurationBuilder(); }
protected Configuration createJsonPathConfiguration() { return new Configuration.ConfigurationBuilder() .jsonProvider(new GsonJsonProvider()) .mappingProvider(new GsonMappingProvider()) .build(); }
/** * Returns a new ConfigurationBuilder * @return a builder */ public static ConfigurationBuilder builder() { return new ConfigurationBuilder(); }
public static Object computePathFunction(Object jsonObject, JsonPathQuery query) { ConfigurationBuilder cb = new ConfigurationBuilder(); Configuration configuration = cb.build(); JsonPath jsonPath; try { jsonPath = JsonPath.compile(query.getPath()); } catch (InvalidPathException e) { throw new BatfishException("Invalid JsonPath: " + query.getPath(), e); } try { return jsonPath.read(jsonObject, configuration); } catch (PathNotFoundException e) { return null; } catch (Exception e) { throw new BatfishException("Error reading JSON path: " + jsonPath, e); } }
private static Object computePathFunctionPvt(String queryPath, Object jsonObject) { ConfigurationBuilder cb = new ConfigurationBuilder(); Configuration configuration = cb.build(); JsonPath jsonPath; try { jsonPath = JsonPath.compile(queryPath); } catch (InvalidPathException e) { throw new BatfishException("Invalid JsonPath: " + queryPath, e); } try { return jsonPath.read(jsonObject, configuration); } catch (PathNotFoundException e) { return null; } catch (Exception e) { throw new BatfishException("Error reading JSON path: " + jsonPath, e); } }
private static List<JsonPathResult> getJsonPathResultsPvt(String queryPath, Object jsonObject) { ConfigurationBuilder prefixCb = new ConfigurationBuilder(); prefixCb.options(Option.ALWAYS_RETURN_LIST); prefixCb.options(Option.AS_PATH_LIST); Configuration prefixC = prefixCb.build(); ConfigurationBuilder suffixCb = new ConfigurationBuilder(); suffixCb.options(Option.ALWAYS_RETURN_LIST); Configuration suffixC = suffixCb.build();
public static JsonPathResult computeResult(Object jsonObject, JsonPathQuery query) { ConfigurationBuilder prefixCb = new ConfigurationBuilder(); prefixCb.options(Option.ALWAYS_RETURN_LIST); prefixCb.options(Option.AS_PATH_LIST); Configuration prefixC = prefixCb.build(); ConfigurationBuilder suffixCb = new ConfigurationBuilder(); suffixCb.options(Option.ALWAYS_RETURN_LIST); Configuration suffixC = suffixCb.build();
public ValueNode evaluate(Predicate.PredicateContext ctx) { if (isExistsCheck()) { try { Configuration c = Configuration.builder().jsonProvider(ctx.configuration().jsonProvider()).options(Option.REQUIRE_PROPERTIES).build(); Object result = path.evaluate(ctx.item(), ctx.root(), c).getValue(false); return result == JsonProvider.UNDEFINED ? ValueNode.FALSE : ValueNode.TRUE; res = path.evaluate(doc, ctx.root(), ctx.configuration()).getValue(); res = ctx.configuration().jsonProvider().unwrap(res);
private static void maskList(DocumentContext ctx, String jsonPath, String expression) { ctx.configuration().addOptions(Option.AS_PATH_LIST); Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build(); DocumentContext context = JsonPath.using(conf).parse(ctx.jsonString()); List<String> pathList = context.read(jsonPath); /** * when reach here, ctx.read(jsonPath) should only give us a list of strings so that we can replace with MASK_REPLACEMENT_CHAR * list of values can belongs to a same path or different paths, we should treat differently. * two situations: * an array contains multiple String values like: "list": ["ab", "cd", "ef] * or single value belongs to different paths. */ if(pathList != null && pathList.size() == 1) { String path = pathList.get(0); List values = ctx.read(path); JSONArray maskedValue = new JSONArray(); //mask each value in the list of the same path values.forEach(o -> maskedValue.add(replaceWithMask(o.toString(), MASK_REPLACEMENT_CHAR.charAt(0), expression))); ctx.set(path, maskedValue); } else { for (String path : Optional.ofNullable(pathList).orElse(Collections.emptyList())) { Object value = ctx.read(path); ctx.set(path, replaceWithMask(value.toString(), MASK_REPLACEMENT_CHAR.charAt(0), expression)); } } }
/** * Creates a new configuration based on default values * @return a new configuration based on defaults */ public static Configuration defaultConfiguration() { Defaults defaults = getEffectiveDefaults(); return Configuration.builder().jsonProvider(defaults.jsonProvider()).options(defaults.options()).build(); }
+ "(Was suffix set to True in the original JsonPath Query?)"); Configuration.setDefaults(BatfishJsonPathDefaults.INSTANCE); Configuration c = (new ConfigurationBuilder()).build(); Object jsonObject = JsonPath.parse(entry.getValue().getSuffix(), c).json();