use of com.jayway.jsonpath.DocumentContext in project pom-manipulation-ext by release-engineering.
the class JSONIOTest method countMatches.
@Test
public void countMatches() throws ManipulationException, IOException {
String pluginsPath = "$..plugins";
String reposURLPath = "$.repository.url";
try {
DocumentContext doc = jsonIO.parseJSON(pluginFile);
List o = doc.read(pluginsPath);
assertTrue(o.size() == 1);
o = doc.read(reposURLPath);
assertTrue(o.size() == 1);
} catch (JsonPathException e) {
throw new ManipulationException("Caught JsonPath", e);
}
}
use of com.jayway.jsonpath.DocumentContext in project pom-manipulation-ext by release-engineering.
the class JSONIOTest method modifyPartialFile.
@Test
public void modifyPartialFile() throws ManipulationException, IOException {
String deletePath = "$.dependencies.agent-base..resolved";
DocumentContext doc = jsonIO.parseJSON(npmFile);
doc.delete(deletePath);
File target = tf.newFile();
jsonIO.writeJSON(target, doc.jsonString());
assertFalse(doc.jsonString().contains("https://registry.npmjs.org/agent-base/-/agent-base-2.0.1.tgz"));
assertTrue(doc.jsonString().contains("resolved"));
}
use of com.jayway.jsonpath.DocumentContext in project pom-manipulation-ext by release-engineering.
the class JSONManipulator method internalApplyChanges.
// Package accessible so tests can use it.
void internalApplyChanges(Project project, JSONState.JSONOperation operation) throws ManipulationException {
File target = new File(project.getPom().getParentFile(), operation.getFile());
logger.info("Attempting to start JSON update to file {} with xpath {} and replacement '{}' ", target, operation.getXPath(), operation.getUpdate());
DocumentContext dc = null;
try {
if (!target.exists()) {
logger.error("Unable to locate JSON file {} ", target);
throw new ManipulationException("Unable to locate JSON file " + target);
}
dc = jsonIO.parseJSON(target);
List o = dc.read(operation.getXPath());
if (o.size() == 0) {
if (project.isIncrementalPME()) {
logger.warn("Did not locate JSON using XPath " + operation.getXPath());
return;
} else {
logger.error("XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile());
throw new ManipulationException("XPath did not resolve to a valid value");
}
}
if (isEmpty(operation.getUpdate())) {
// Delete
logger.info("Deleting {} on {}", operation.getXPath(), dc.toString());
dc.delete(operation.getXPath());
} else {
// Update
logger.info("Updating {} on {}", operation.getXPath(), dc.toString());
dc.set(operation.getXPath(), operation.getUpdate());
}
jsonIO.writeJSON(target, dc.jsonString());
} catch (JsonPathException e) {
logger.error("Caught JSON exception processing file {}, document context {} ", target, dc, e);
throw new ManipulationException("Caught JsonPath", e);
}
}
use of com.jayway.jsonpath.DocumentContext in project bender by Nextdoor.
the class GenericJsonEvent method setField.
@Override
public void setField(String fieldName, Object value) throws IllegalArgumentException {
if (this.payload == null) {
throw new IllegalArgumentException("payload is null");
}
int lastDot = fieldName.lastIndexOf('.');
if (lastDot == -1) {
throw new IllegalArgumentException("Unable to find '.' in field name denoting path");
}
if (!fieldName.startsWith("$")) {
throw new IllegalArgumentException("Field name must be a path and must start with '$'");
}
DocumentContext json = JsonPathProvider.parse(this.payload.getAsJsonObject());
String path = fieldName.substring(0, lastDot);
String field = fieldName.substring(lastDot + 1);
json.put(path, field, value);
}
use of com.jayway.jsonpath.DocumentContext in project incubator-heron by apache.
the class TrackerMetricsProvider method parse.
@SuppressWarnings("unchecked")
private Map<String, InstanceMetrics> parse(String response, String component, String metric) {
Map<String, InstanceMetrics> metricsData = new HashMap<>();
if (response == null || response.isEmpty()) {
return metricsData;
}
DocumentContext result = JsonPath.parse(response);
JSONArray jsonArray = result.read("$.." + metric);
if (jsonArray.size() != 1) {
LOG.info(String.format("Did not get any metrics from tracker for %s:%s ", component, metric));
return metricsData;
}
Map<String, Object> metricsMap = (Map<String, Object>) jsonArray.get(0);
if (metricsMap == null || metricsMap.isEmpty()) {
LOG.info(String.format("Did not get any metrics from tracker for %s:%s ", component, metric));
return metricsData;
}
for (String instanceName : metricsMap.keySet()) {
Map<String, String> tmpValues = (Map<String, String>) metricsMap.get(instanceName);
Map<Instant, Double> values = new HashMap<>();
for (String timeStamp : tmpValues.keySet()) {
values.put(Instant.ofEpochSecond(Long.parseLong(timeStamp)), Double.parseDouble(tmpValues.get(timeStamp)));
}
InstanceMetrics instanceMetrics = new InstanceMetrics(instanceName);
instanceMetrics.addMetric(metric, values);
metricsData.put(instanceName, instanceMetrics);
}
return metricsData;
}
Aggregations