Browse Source

Renamed JacksonTreeJsonProvider.

pull/61/head
Kalle Stenflo 10 years ago
parent
commit
28388af0f3
  1. 3
      README.md
  2. 6
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonNodeJsonProvider.java
  3. 6
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  4. 22
      json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java
  5. 10
      json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java

3
README.md

@ -185,7 +185,7 @@ When evaluating a path you need to understand the concept of when a path is `def
* `?(<expression>)` - an expression
* `[<number>, <number> (, <number>)]` - multiple array indexes
`Indefinite` paths always returns a list.
`Indefinite` paths always returns a list (as represented by current JsonProvider).
By default a simple object mapper is provided by the MappingProvider SPI. This allows you to specify the return type you want and the MappingProvider will
try to perform the mapping. In the example below mapping between `Long` and `Date` is demonstrated.
@ -338,6 +338,7 @@ JsonPath is shipped with three different JsonProviders:
* [JsonSmartJsonProvider](https://code.google.com/p/json-smart/) (default)
* [JacksonJsonProvider](https://github.com/FasterXML/jackson)
* [JacksonJsonNodeJsonProvider](https://github.com/FasterXML/jackson)
* [GsonJsonProvider](https://code.google.com/p/google-gson/) (experimental)
Changing the configuration defaults as demonstrated should only be done when your application is being initialized. Changes during runtime is strongly discouraged, especially in multi threaded applications.

6
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonTreeJsonProvider.java → json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonNodeJsonProvider.java

@ -19,7 +19,7 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.List;
public class JacksonTreeJsonProvider extends AbstractJsonProvider {
public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
private static final Logger logger = LoggerFactory.getLogger(JacksonJsonProvider.class);
@ -34,7 +34,7 @@ public class JacksonTreeJsonProvider extends AbstractJsonProvider {
/**
* Initialize the JacksonTreeJsonProvider with the default ObjectMapper and ObjectReader
*/
public JacksonTreeJsonProvider() {
public JacksonJsonNodeJsonProvider() {
this(defaultObjectMapper);
}
@ -43,7 +43,7 @@ public class JacksonTreeJsonProvider extends AbstractJsonProvider {
*
* @param objectMapper the ObjectMapper to use
*/
public JacksonTreeJsonProvider(ObjectMapper objectMapper) {
public JacksonJsonNodeJsonProvider(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

6
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -2,8 +2,8 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.internal.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.internal.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.internal.spi.json.JacksonTreeJsonProvider;
import com.jayway.jsonpath.internal.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.internal.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.internal.token.PredicateContextImpl;
@ -24,10 +24,10 @@ public class BaseTest {
.jsonProvider(new JacksonJsonProvider())
.build();
public static final Configuration JACKSON_TREE_CONFIGURATION = Configuration
public static final Configuration JACKSON_JSON_NODE_CONFIGURATION = Configuration
.builder()
.mappingProvider(new JacksonMappingProvider())
.jsonProvider(new JacksonTreeJsonProvider())
.jsonProvider(new JacksonJsonNodeJsonProvider())
.build();
public static final Configuration JSON_SMART_CONFIGURATION = Configuration.defaultConfiguration();

22
json-path/src/test/java/com/jayway/jsonpath/JacksonTreeJsonProviderTest.java → json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

@ -12,7 +12,7 @@ import java.util.List;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
public class JacksonTreeJsonProviderTest extends BaseTest {
public class JacksonJsonNodeJsonProviderTest extends BaseTest {
private static final String JSON =
"[" +
@ -38,14 +38,14 @@ public class JacksonTreeJsonProviderTest extends BaseTest {
@Test
public void json_can_be_parsed() {
ObjectNode node = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$");
ObjectNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$");
assertThat(node.get("string-property").asText()).isEqualTo("string-value");
}
@Test
public void strings_are_unwrapped() {
JsonNode node = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property");
String unwrapped = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class);
JsonNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property");
String unwrapped = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class);
assertThat(unwrapped).isEqualTo("string-value");
assertThat(unwrapped).isEqualTo(node.asText());
@ -53,16 +53,16 @@ public class JacksonTreeJsonProviderTest extends BaseTest {
@Test
public void ints_are_unwrapped() {
JsonNode node = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property");
int unwrapped = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class);
JsonNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property");
int unwrapped = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class);
assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE);
assertThat(unwrapped).isEqualTo(node.asInt());
}
@Test
public void longs_are_unwrapped() {
JsonNode node = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property");
long unwrapped = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", long.class);
JsonNode node = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property");
long unwrapped = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", long.class);
assertThat(unwrapped).isEqualTo(Long.MAX_VALUE);
assertThat(unwrapped).isEqualTo(node.asLong());
@ -71,7 +71,7 @@ public class JacksonTreeJsonProviderTest extends BaseTest {
@Test
public void list_of_numbers() {
ArrayNode objs = using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price");
ArrayNode objs = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price");
System.out.println(objs.toString());
}
@ -79,7 +79,7 @@ public class JacksonTreeJsonProviderTest extends BaseTest {
public void test_type_ref() throws IOException {
TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() {};
List<FooBarBaz<Gen>> list = using(JACKSON_TREE_CONFIGURATION).parse(JSON).read("$", typeRef);
List<FooBarBaz<Gen>> list = using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
assertThat(list.get(0).gen.eric).isEqualTo("yepp");
}
@ -88,7 +88,7 @@ public class JacksonTreeJsonProviderTest extends BaseTest {
public void test_type_ref_fail() throws IOException {
TypeRef<List<FooBarBaz<Integer>>> typeRef = new TypeRef<List<FooBarBaz<Integer>>>() {};
using(JACKSON_TREE_CONFIGURATION).parse(JSON).read("$", typeRef);
using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef);
}
public static class FooBarBaz<T> {

10
json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java

@ -35,7 +35,7 @@ public class JsonProviderTest extends BaseTest {
@Test
public void strings_are_unwrapped() {
assertThat(using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class)).isEqualTo("string-value");
assertThat(using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class)).isEqualTo("string-value");
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class)).isEqualTo("string-value");
assertThat(using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class)).isEqualTo("string-value");
assertThat(using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class)).isEqualTo("string-value");
}
@ -43,7 +43,7 @@ public class JsonProviderTest extends BaseTest {
@Test
public void integers_are_unwrapped() {
assertThat(using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", Integer.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", Integer.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", Integer.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", Integer.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", Integer.class)).isEqualTo(Integer.MAX_VALUE);
}
@ -51,7 +51,7 @@ public class JsonProviderTest extends BaseTest {
@Test
public void ints_are_unwrapped() {
assertThat(using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(JSON_SMART_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class)).isEqualTo(Integer.MAX_VALUE);
assertThat(using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class)).isEqualTo(Integer.MAX_VALUE);
}
@ -61,7 +61,7 @@ public class JsonProviderTest extends BaseTest {
TypeRef<List<Double>> typeRef = new TypeRef<List<Double>>() {};
assertThat(using(JACKSON_TREE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price", typeRef)).containsExactly(8.95D, 12.99D, 8.99D, 22.99D);
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price", typeRef)).containsExactly(8.95D, 12.99D, 8.99D, 22.99D);
assertThat(using(JACKSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price", typeRef)).containsExactly(8.95D, 12.99D, 8.99D, 22.99D);
assertThat(using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price", typeRef)).containsExactly(8.95D, 12.99D, 8.99D, 22.99D);
}
@ -71,7 +71,7 @@ public class JsonProviderTest extends BaseTest {
TypeRef<List<FooBarBaz<Sub>>> typeRef = new TypeRef<List<FooBarBaz<Sub>>>() {};
assertThat(using(JACKSON_CONFIGURATION).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2");
assertThat(using(JACKSON_TREE_CONFIGURATION).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2");
assertThat(using(JACKSON_JSON_NODE_CONFIGURATION).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2");
assertThat(using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef)).extracting("foo").containsExactly("foo0", "foo1", "foo2");
}

Loading…
Cancel
Save