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");
}
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);
}
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();
}
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);
}
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);
}
}
Aggregations