|
|
|
@ -10,6 +10,7 @@ import java.io.IOException;
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
|
import java.math.BigInteger; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import static com.jayway.jsonpath.JsonPath.using; |
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
@ -18,36 +19,36 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
private static final String JSON = |
|
|
|
|
"[" + |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo0\",\n" + |
|
|
|
|
" \"bar\" : 0,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}," + |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo1\",\n" + |
|
|
|
|
" \"bar\" : 1,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}," + |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo2\",\n" + |
|
|
|
|
" \"bar\" : 2,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}" + |
|
|
|
|
"]"; |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo0\",\n" + |
|
|
|
|
" \"bar\" : 0,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}," + |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo1\",\n" + |
|
|
|
|
" \"bar\" : 1,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}," + |
|
|
|
|
"{\n" + |
|
|
|
|
" \"foo\" : \"foo2\",\n" + |
|
|
|
|
" \"bar\" : 2,\n" + |
|
|
|
|
" \"baz\" : true,\n" + |
|
|
|
|
" \"gen\" : {\"eric\" : \"yepp\"}" + |
|
|
|
|
"}" + |
|
|
|
|
"]"; |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void json_can_be_parsed() { |
|
|
|
|
JsonObject node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$"); |
|
|
|
|
JsonObject node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$"); |
|
|
|
|
assertThat(node.get("string-property").getAsString()).isEqualTo("string-value"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void strings_are_unwrapped() { |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property"); |
|
|
|
|
String unwrapped = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class); |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property"); |
|
|
|
|
String unwrapped = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.string-property", String.class); |
|
|
|
|
|
|
|
|
|
assertThat(unwrapped).isEqualTo("string-value"); |
|
|
|
|
assertThat(unwrapped).isEqualTo(node.getAsString()); |
|
|
|
@ -55,8 +56,8 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void ints_are_unwrapped() { |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property"); |
|
|
|
|
int unwrapped = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class); |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property"); |
|
|
|
|
int unwrapped = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.int-max-property", int.class); |
|
|
|
|
|
|
|
|
|
assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE); |
|
|
|
|
assertThat(unwrapped).isEqualTo(node.getAsInt()); |
|
|
|
@ -64,8 +65,8 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void longs_are_unwrapped() { |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property"); |
|
|
|
|
long val = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", Long.class); |
|
|
|
|
JsonElement node = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property"); |
|
|
|
|
long val = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.long-max-property", Long.class); |
|
|
|
|
|
|
|
|
|
assertThat(val).isEqualTo(Long.MAX_VALUE); |
|
|
|
|
assertThat(val).isEqualTo(node.getAsLong()); |
|
|
|
@ -140,9 +141,20 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
assertThat(using(GSON_CONFIGURATION).parse("{\"val\": 1}").read("val", Double.class)).isEqualTo(1D); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test(expected = ClassCastException.class) |
|
|
|
|
public void jackson_can_what_gson_cannot() { |
|
|
|
|
final Map<String, Object> jacksonMap = using(JACKSON_CONFIGURATION).parse(JSON_AWS_LAMBDA_CONTEXT).read("$.queryStringParameters"); |
|
|
|
|
assertThat(jacksonMap.get("lat")).isEqualTo(50D); |
|
|
|
|
|
|
|
|
|
// thorws class ClassCastException
|
|
|
|
|
final Map<String, Object> gsonMap = using(GSON_CONFIGURATION).parse(JSON_AWS_LAMBDA_CONTEXT).read("$.queryStringParameters"); |
|
|
|
|
assertThat(gsonMap.get("lat")).isEqualTo(50D); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void list_of_numbers() { |
|
|
|
|
JsonArray objs = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price"); |
|
|
|
|
JsonArray objs = using(GSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[*].display-price"); |
|
|
|
|
|
|
|
|
|
assertThat(objs.iterator()).extracting("asDouble").containsExactly(8.95D, 12.99D, 8.99D, 22.99D); |
|
|
|
|
|
|
|
|
@ -168,7 +180,8 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void test_type_ref() throws IOException { |
|
|
|
|
TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() {}; |
|
|
|
|
TypeRef<List<FooBarBaz<Gen>>> typeRef = new TypeRef<List<FooBarBaz<Gen>>>() { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
List<FooBarBaz<Gen>> list = JsonPath.using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef); |
|
|
|
|
|
|
|
|
@ -177,7 +190,8 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
|
|
|
|
|
@Test(expected = MappingException.class) |
|
|
|
|
public void test_type_ref_fail() throws IOException { |
|
|
|
|
TypeRef<List<FooBarBaz<Integer>>> typeRef = new TypeRef<List<FooBarBaz<Integer>>>() {}; |
|
|
|
|
TypeRef<List<FooBarBaz<Integer>>> typeRef = new TypeRef<List<FooBarBaz<Integer>>>() { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
using(GSON_CONFIGURATION).parse(JSON).read("$", typeRef); |
|
|
|
|
} |
|
|
|
@ -201,6 +215,4 @@ public class GsonJsonProviderTest extends BaseTest {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|