Search in sources :

Example 1 with GsonMappingProvider

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

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)

Example 2 with GsonMappingProvider

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

the class ProviderInTest method testJsonPathQuotesGson.

@Test
public void testJsonPathQuotesGson() throws Exception {
    final Configuration gson = Configuration.builder().jsonProvider(new GsonJsonProvider()).mappingProvider(new GsonMappingProvider()).build();
    final DocumentContext ctx = JsonPath.using(gson).parse(JSON);
    final JsonArray doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER);
    assertEquals("bar", doubleQuoteEqualsResult.get(0).getAsString());
    final JsonArray singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER);
    assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult);
    final JsonArray doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER);
    assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult);
    final JsonArray singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER);
    assertEquals(doubleQuoteInResult, singleQuoteInResult);
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) JsonArray(com.google.gson.JsonArray) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) Test(org.junit.Test)

Example 3 with GsonMappingProvider

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

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 4 with GsonMappingProvider

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

the class ProviderInTest method testJsonPathQuotesGson.

@Test
public void testJsonPathQuotesGson() throws Exception {
    final Configuration gson = Configuration.builder().jsonProvider(new GsonJsonProvider()).mappingProvider(new GsonMappingProvider()).build();
    final DocumentContext ctx = JsonPath.using(gson).parse(JSON);
    final JsonArray doubleQuoteEqualsResult = ctx.read(DOUBLE_QUOTES_EQUALS_FILTER);
    assertEquals("bar", doubleQuoteEqualsResult.get(0).getAsString());
    final JsonArray singleQuoteEqualsResult = ctx.read(SINGLE_QUOTES_EQUALS_FILTER);
    assertEquals(doubleQuoteEqualsResult, singleQuoteEqualsResult);
    final JsonArray doubleQuoteInResult = ctx.read(DOUBLE_QUOTES_IN_FILTER);
    assertEquals(doubleQuoteInResult, doubleQuoteEqualsResult);
    final JsonArray singleQuoteInResult = ctx.read(SINGLE_QUOTES_IN_FILTER);
    assertEquals(doubleQuoteInResult, singleQuoteInResult);
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) JsonArray(com.google.gson.JsonArray) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) Test(org.junit.Test)

Example 5 with GsonMappingProvider

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

the class MtgjsonProvider method init.

public void init() {
    logger.info("init " + this);
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new GsonJsonProvider();

        private final MappingProvider mappingProvider = new GsonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
    Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
    try {
        logger.debug("loading file " + fileSetJson);
        if (!fileSetJson.exists() || fileSetJson.length() == 0) {
            logger.info("datafile does not exist. Downloading it");
            FileUtils.copyInputStreamToFile(getStreamFromUrl(new URL(getString("URL_SET_JSON_ZIP"))), fileSetJsonTemp);
            unZipIt();
            FileUtils.copyInputStreamToFile(getStreamFromUrl(new URL(getString("URL_VERSION"))), fversion);
        }
        if (hasNewVersion()) {
            FileUtils.copyInputStreamToFile(getStreamFromUrl(new URL(getString("URL_SET_JSON_ZIP"))), fileSetJsonTemp);
            unZipIt();
            FileUtils.copyInputStreamToFile(getStreamFromUrl(new URL(getString("URL_VERSION"))), fversion);
        }
        cachedCardEds = new HashMap<>();
        logger.info(this + " : parsing db file");
        long time1 = System.currentTimeMillis();
        ctx = JsonPath.parse(fileSetJson);
        long time2 = System.currentTimeMillis();
        logger.info(this + " : parsing OK (" + (time2 - time1) / 1000 + " s)");
    } catch (Exception e1) {
        logger.error(e1);
    }
}
Also used : GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) EnumSet(java.util.EnumSet) Set(java.util.Set) Configuration(com.jayway.jsonpath.Configuration) GsonMappingProvider(com.jayway.jsonpath.spi.mapper.GsonMappingProvider) MappingProvider(com.jayway.jsonpath.spi.mapper.MappingProvider) JsonProvider(com.jayway.jsonpath.spi.json.JsonProvider) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PathNotFoundException(com.jayway.jsonpath.PathNotFoundException) GsonJsonProvider(com.jayway.jsonpath.spi.json.GsonJsonProvider) Option(com.jayway.jsonpath.Option)

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