/** * 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(); }
@Inject public SelectJsonPath(ObjectMapper objectMapper) { configuration = Configuration.builder() .options(Option.SUPPRESS_EXCEPTIONS) .jsonProvider(new JacksonJsonNodeJsonProvider(objectMapper)) .build(); jsonParam = ParameterDescriptor.type("json", JsonNode.class).description("A parsed JSON tree").build(); // sigh generics and type erasure //noinspection unchecked pathsParam = ParameterDescriptor.type("paths", (Class<Map<String, String>>) new TypeLiteral<Map<String, String>>() {}.getRawType(), (Class<Map<String, JsonPath>>) new TypeLiteral<Map<String, JsonPath>>() {}.getRawType()) .transform(inputMap -> inputMap .entrySet().stream() .collect(toMap(Map.Entry::getKey, e -> JsonPath.compile(e.getValue())))) .description("A map of names to a JsonPath expression, see http://jsonpath.com") .build(); }
/** * Creates a {@link ParseContext} that will parse a given JSON input. * * @param provider jsonProvider to use when parsing JSON * @return a parsing context based on given jsonProvider */ @Deprecated public static ParseContext using(JsonProvider provider) { return new ParseContextImpl(Configuration.builder().jsonProvider(provider).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;
@Test public void mapPropertyWithPOJO() { String someJson = "" + "{\n" + " \"a\": \"a\",\n" + " \"b\": \"b\"\n" + "}"; ObjectMapper om = new ObjectMapper(); om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); Configuration c = Configuration .builder() .mappingProvider(new JacksonMappingProvider()) .jsonProvider(new JacksonJsonNodeJsonProvider(om)) .build(); DocumentContext context = JsonPath.using(c).parse(someJson); String someJsonStr = context.jsonString(); DocumentContext altered = context.map("$['a', 'b', 'c']", new MapFunction() { @Override public Object map(Object currentValue, Configuration configuration) { return currentValue; } }); assertThat(altered.jsonString()).isEqualTo(someJsonStr); }
@Test public void issue_97() throws Exception { String json = "{ \"books\": [ " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"fiction\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" }, " + "{ \"category\": \"reference\" } ] }"; Configuration conf = Configuration.builder() .jsonProvider(new GsonJsonProvider()) .mappingProvider(new GsonMappingProvider()) .build(); DocumentContext context = JsonPath.using(conf).parse(json); context.delete("$.books[?(@.category == 'reference')]"); List<String> categories = context.read("$..category", List.class); assertThat(categories).containsOnly("fiction"); }
@Test public void issue_378(){ String json = "{\n" + " \"nodes\": {\n" + " \"unnamed1\": {\n" + " \"ntpServers\": [\n" + " \"1.2.3.4\"\n" + " ]\n" + " }\n" + " }\n" + "}"; Configuration configuration = Configuration.builder() .jsonProvider(new JacksonJsonNodeJsonProvider()) .mappingProvider(new JacksonMappingProvider()) .build(); DocumentContext ctx = JsonPath.using(configuration).parse(json); String path = "$.nodes[*][?(!([\"1.2.3.4\"] subsetof @.ntpServers))].ntpServers"; JsonPath jsonPath = JsonPath.compile(path); ctx.read(jsonPath); } }
@Test public void testJsonPathQuotesJsonSmart() throws Exception { final Configuration jsonSmart = Configuration.builder().jsonProvider(new JsonSmartJsonProvider()).mappingProvider(new JsonSmartMappingProvider()).build(); final DocumentContext ctx = JsonPath.using(jsonSmart).parse(JSON); final net.minidev.json.JSONArray doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER); assertEquals("bar", doubleQuoteEqualsResult.get(0)); final net.minidev.json.JSONArray singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER); assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult); final net.minidev.json.JSONArray doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); final net.minidev.json.JSONArray singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, singleQuoteInResult); } }
@Test // https://github.com/json-path/JsonPath/issues/351 public void no_error_when_mapping_null() throws IOException { Configuration configuration = Configuration .builder() .mappingProvider(new GsonMappingProvider()) .jsonProvider(new GsonJsonProvider()) .options(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS) .build(); String json = "{\"M\":[]}"; String result = JsonPath.using(configuration).parse(json).read("$.M[0].A[0]", String.class); assertThat(result).isNull(); }
@Test public void testJsonPathQuotesJsonOrg() throws Exception { final Configuration jsonOrg = Configuration.builder().jsonProvider(new JsonOrgJsonProvider()).mappingProvider(new JsonOrgMappingProvider()).build(); final DocumentContext ctx = JsonPath.using(jsonOrg).parse(JSON); final org.json.JSONArray doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER); assertEquals("bar", doubleQuoteEqualsResult.get(0)); final org.json.JSONArray singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER); assertEquals(doubleQuoteEqualsResult.get(0), singleQuoteEqualsResult.get(0)); final org.json.JSONArray doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult.get(0), doubleQuoteEqualsResult.get(0)); final org.json.JSONArray singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult.get(0), singleQuoteInResult.get(0)); }
@Test public void testJsonPathQuotesJacksonJsonNode() throws Exception { final Configuration jacksonJsonNode = Configuration.builder().jsonProvider(new JacksonJsonNodeJsonProvider()).mappingProvider(new JacksonMappingProvider()).build(); final DocumentContext ctx = JsonPath.using(jacksonJsonNode).parse(JSON); final ArrayNode doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER); assertEquals("bar", doubleQuoteEqualsResult.get(0).asText()); final ArrayNode singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER); assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult); final ArrayNode doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); final ArrayNode singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, singleQuoteInResult); }
@Test public void testJsonPathQuotesGson() throws Exception { final Configuration gson = Configuration.builder().jsonProvider(new GsonJsonProvider()).mappingProvider(new GsonMappingProvider()).build(); final DocumentContext ctx = JsonPath.using(gson).parse(JSON); final JsonArray doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER); assertEquals("bar", doubleQuoteEqualsResult.get(0).getAsString()); final JsonArray singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER); assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult); final JsonArray doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); final JsonArray singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, singleQuoteInResult); }
@Test public void testJsonPathQuotesJackson() throws Exception { final Configuration jackson = Configuration.builder().jsonProvider(new JacksonJsonProvider()).mappingProvider(new JacksonMappingProvider()).build(); final DocumentContext ctx = JsonPath.using(jackson).parse(JSON); final List<String> doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER); assertEquals(Lists.newArrayList("bar"), doubleQuoteEqualsResult); final List<String> singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER); assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult); final List<String> doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult); final List<String> singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER); assertEquals(doubleQuoteInResult, singleQuoteInResult); }
/** * 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(); }
/** * Creates a new Configuration with the provided evaluation listeners * @param evaluationListener listeners * @return a new configuration */ public Configuration setEvaluationListeners(EvaluationListener... evaluationListener){ return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build(); }
/** * Creates a new Configuration by the provided evaluation listeners to the current listeners * @param evaluationListener listeners * @return a new configuration */ public Configuration addEvaluationListeners(EvaluationListener... evaluationListener){ return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListener).build(); }
/** * Creates a new Configuration based on the given {@link com.jayway.jsonpath.spi.mapper.MappingProvider} * @param newMappingProvider mapping provider to use in new configuration * @return a new configuration */ public Configuration mappingProvider(MappingProvider newMappingProvider) { return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(newMappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
/** * Creates a new Configuration based on the given {@link com.jayway.jsonpath.spi.json.JsonProvider} * @param newJsonProvider json provider to use in new configuration * @return a new configuration */ public Configuration jsonProvider(JsonProvider newJsonProvider) { return Configuration.builder().jsonProvider(newJsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
/** * Creates a new configuration with the provided options. Options in this configuration are discarded. * @param options * @return */ public Configuration setOptions(Option... options) { return Configuration.builder().jsonProvider(jsonProvider).mappingProvider(mappingProvider).options(options).evaluationListener(evaluationListeners).build(); }
ParquetGroupFlattenerMaker(boolean binaryAsString) { this.converter = new ParquetGroupConverter(binaryAsString); this.jsonPathConfiguration = Configuration.builder() .jsonProvider(new ParquetGroupJsonProvider(converter)) .mappingProvider(new NotImplementedMappingProvider()) .options(EnumSet.of(Option.SUPPRESS_EXCEPTIONS)) .build(); }