You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.9 KiB
90 lines
2.9 KiB
10 years ago
|
package com.jayway.jsonpath;
|
||
|
|
||
|
import com.google.gson.JsonArray;
|
||
|
import com.google.gson.JsonElement;
|
||
|
import com.google.gson.JsonObject;
|
||
|
import org.junit.Test;
|
||
|
|
||
|
import static com.jayway.jsonpath.JsonPath.using;
|
||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||
|
|
||
|
public class GsonJsonProviderTest extends BaseTest {
|
||
|
|
||
|
@Test
|
||
|
public void json_can_be_parsed() {
|
||
|
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);
|
||
|
|
||
|
assertThat(unwrapped).isEqualTo("string-value");
|
||
|
assertThat(unwrapped).isEqualTo(node.getAsString());
|
||
|
}
|
||
|
|
||
|
@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", Integer.class);
|
||
|
|
||
|
assertThat(unwrapped).isEqualTo(Integer.MAX_VALUE);
|
||
|
assertThat(unwrapped).isEqualTo(node.getAsInt());
|
||
|
}
|
||
|
|
||
|
@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);
|
||
|
|
||
|
assertThat(val).isEqualTo(Long.MAX_VALUE);
|
||
|
assertThat(val).isEqualTo(node.getAsLong());
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void int_to_long_mapping() {
|
||
|
assertThat(using(GSON_CONFIGURATION).parse("{\"val\": 1}").read("val", Long.class)).isEqualTo(1L);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void an_Integer_can_be_converted_to_a_Double() {
|
||
|
assertThat(using(GSON_CONFIGURATION).parse("{\"val\": 1}").read("val", Double.class)).isEqualTo(1D);
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void list_of_numbers() {
|
||
|
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);
|
||
|
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
public void an_object_can_be_mapped_to_pojo() {
|
||
|
|
||
|
String json = "{\n" +
|
||
|
" \"foo\" : \"foo\",\n" +
|
||
|
" \"bar\" : 10,\n" +
|
||
|
" \"baz\" : true\n" +
|
||
|
"}";
|
||
|
|
||
|
|
||
|
FooBarBaz fooBarBaz = JsonPath.using(GSON_CONFIGURATION).parse(json).read("$", FooBarBaz.class);
|
||
|
|
||
|
assertThat(fooBarBaz.foo).isEqualTo("foo");
|
||
|
assertThat(fooBarBaz.bar).isEqualTo(10L);
|
||
|
assertThat(fooBarBaz.baz).isEqualTo(true);
|
||
|
|
||
|
}
|
||
|
|
||
|
public static class FooBarBaz {
|
||
|
public String foo;
|
||
|
public Long bar;
|
||
|
public boolean baz;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|