Search in sources :

Example 41 with DocumentContext

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);
    }
}
Also used : JsonPathException(com.jayway.jsonpath.JsonPathException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 42 with DocumentContext

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"));
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 43 with DocumentContext

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);
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) JsonPathException(com.jayway.jsonpath.JsonPathException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) File(java.io.File)

Example 44 with DocumentContext

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);
}
Also used : DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 45 with DocumentContext

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;
}
Also used : InstanceMetrics(com.microsoft.dhalion.metrics.InstanceMetrics) HashMap(java.util.HashMap) Instant(java.time.Instant) JSONArray(net.minidev.json.JSONArray) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DocumentContext (com.jayway.jsonpath.DocumentContext)150 Test (org.junit.Test)106 HashMap (java.util.HashMap)14 BaseTest (com.jayway.jsonpath.BaseTest)12 Map (java.util.Map)12 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)9 JsonPath (com.jayway.jsonpath.JsonPath)7 File (java.io.File)7 List (java.util.List)7 JsonPathAssert (com.revinate.assertj.json.JsonPathAssert)6 Query (org.graylog.plugins.views.search.Query)6 MessageList (org.graylog.plugins.views.search.searchtypes.MessageList)6 Time (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Time)6 Values (org.graylog.plugins.views.search.searchtypes.pivot.buckets.Values)6 DateTime (org.joda.time.DateTime)6 Configuration (com.jayway.jsonpath.Configuration)5 ArrayList (java.util.ArrayList)5 Test (org.junit.jupiter.api.Test)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 IOException (java.io.IOException)4