Search in sources :

Example 6 with GsonMappingProvider

use of com.jayway.jsonpath.spi.mapper.GsonMappingProvider in project JsonPath by json-path.

the class GsonJsonProviderTest method no_error_when_mapping_null.

@Test
public // https://github.com/json-path/JsonPath/issues/351
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();
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) Test(org.junit.Test)

Example 7 with GsonMappingProvider

use of com.jayway.jsonpath.spi.mapper.GsonMappingProvider in project JsonPath by jayway.

the class IssuesTest method issue_97.

@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");
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) Configuration(com.jayway.jsonpath.Configuration) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) DocumentContext(com.jayway.jsonpath.DocumentContext) BaseTest(com.jayway.jsonpath.BaseTest) Test(org.junit.Test)

Aggregations

GsonJsonProvider (com.jayway.jsonpath.spi.json.GsonJsonProvider)7 GsonMappingProvider (com.jayway.jsonpath.spi.mapper.GsonMappingProvider)7 Test (org.junit.Test)6 Configuration (com.jayway.jsonpath.Configuration)3 JsonArray (com.google.gson.JsonArray)2 BaseTest (com.jayway.jsonpath.BaseTest)2 DocumentContext (com.jayway.jsonpath.DocumentContext)2 Option (com.jayway.jsonpath.Option)1 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)1 JsonProvider (com.jayway.jsonpath.spi.json.JsonProvider)1 MappingProvider (com.jayway.jsonpath.spi.mapper.MappingProvider)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 EnumSet (java.util.EnumSet)1 Set (java.util.Set)1