/** * 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(); }
@Test public void an_indefinite_path_can_be_returned_as_list() { Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build(); List<Object> result = using(conf).parse("{\"bar\": {\"foo\": null}}").read("$..foo"); assertThat(result).hasSize(1); assertThat(result.get(0)).isNull(); assertThat((List)using(conf).parse("{\"bar\": {\"foo\": [1, 4, 8]}}").read("$..foo")).asList() .containsExactly(Arrays.asList(1, 4, 8)); }
@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 issue_143() { String json = "{ \"foo\": { \"bar\" : \"val\" }, \"moo\": { \"cow\" : \"val\" } }"; Configuration configuration = Configuration.builder().options( Option.AS_PATH_LIST ).build(); List<String> pathList = JsonPath.using(configuration).parse(json).read(JsonPath.compile("$.*.bar")); assertThat(pathList).containsExactly("$['foo']['bar']"); }
@Test public void a_path_evaluation_can_be_returned_as_PATH_LIST() { Configuration conf = Configuration.builder().options(AS_PATH_LIST).build(); List<String> pathList = using(conf).parse(JSON_DOCUMENT).read("$..author"); assertThat(pathList).containsExactly( "$['store']['book'][0]['author']", "$['store']['book'][1]['author']", "$['store']['book'][2]['author']", "$['store']['book'][3]['author']"); }
/** * 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(); }
@Test public void a_definite_path_can_be_returned_as_list() { Configuration conf = Configuration.builder().options(ALWAYS_RETURN_LIST).build(); assertThat((List)using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo")).isInstanceOf(List.class); assertThat((List)using(conf).parse("{\"foo\": null}").read("$.foo")).isInstanceOf(List.class); assertThat((List)using(conf).parse("{\"foo\": [1, 4, 8]}").read("$.foo")).asList() .containsExactly(Arrays.asList(1, 4, 8)); }
@Test public void a_leafs_can_be_defaulted_to_null() { Configuration conf = Configuration.builder().options(DEFAULT_PATH_LEAF_TO_NULL).build(); assertThat(using(conf).parse("{\"foo\" : \"bar\"}").read("$.baz", Object.class)).isNull(); }
@Test public void property_not_found() { //String result = JsonPath.read(SIMPLE_MAP, "$.not-found"); //assertThat(result).isNull(); Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; assertNull(JsonPath.parse(SIMPLE_MAP, configuration).read("$.not-found")); }
@Test public void last_token_defaults_to_null() { Configuration configuration = Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build(); assertNull(JsonPath.parse(DOCUMENT, configuration).read("$.children[2].age")); }
/** * 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(); }
@Test public void a_path_evaluation_can_be_returned_as_PATH_LIST() { Configuration conf = Configuration.builder().options(AS_PATH_LIST).build(); List<String> pathList = using(conf).parse("{\"foo\" : \"bar\"}").read("$.foo"); assertThat(pathList).containsOnly("$['foo']"); }
/** * 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(); }
@Test public void issue_suppress_exceptions_does_not_break_indefinite_evaluation() { Configuration conf = Configuration.builder().options(SUPPRESS_EXCEPTIONS).build(); assertThat((List)using(conf).parse("{\"foo2\": [5]}").read("$..foo2[0]")).asList().containsOnly(5); assertThat((List)using(conf).parse("{\"foo\" : {\"foo2\": [5]}}").read("$..foo2[0]")).asList().containsOnly(5); assertThat((List)using(conf).parse("[null, [{\"foo\" : {\"foo2\": [5]}}]]").read("$..foo2[0]")).asList().containsOnly(5); assertThat((List)using(conf).parse("[null, [{\"foo\" : {\"foo2\": [5]}}]]").read("$..foo.foo2[0]")).asList().containsOnly(5); assertThat((List)using(conf).parse("{\"aoo\" : {}, \"foo\" : {\"foo2\": [5]}, \"zoo\" : {}}").read("$[*].foo2[0]")).asList().containsOnly(5); }
/** * 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(); }
@Test public void issue_22c() throws Exception { //Configuration configuration = Configuration.builder().build(); Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build(); String json = "{\"a\":{\"b\":1,\"c\":2}}"; assertNull(JsonPath.parse(json, configuration).read("a.d")); }
/** * 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(); }
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(); }