Search in sources :

Example 76 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project buck by facebook.

the class TargetsCommandIntegrationTest method testJsonOutputWithShowCellPath.

@Test
public void testJsonOutputWithShowCellPath() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "output_path", tmp);
    workspace.setUp();
    ProcessResult result = workspace.runBuckCommand("targets", "--json", "--show-cell-path", "//:test");
    ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
    // Parse the observed JSON.
    JsonNode observed = objectMapper.readTree(objectMapper.getFactory().createParser(result.getStdout()));
    assertTrue(observed.isArray());
    JsonNode targetNode = observed.get(0);
    assertTrue(targetNode.isObject());
    JsonNode cellPath = targetNode.get("buck.cell_path");
    assertNotNull(cellPath);
    assertEquals(cellPath.asText(), MorePaths.pathWithPlatformSeparators(tmp.getRoot().toRealPath()));
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ProcessResult(com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 77 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project druid by druid-io.

the class JSONToLowerParser method parse.

@Override
public Map<String, Object> parse(String input) {
    try {
        Map<String, Object> map = new LinkedHashMap<>();
        JsonNode root = objectMapper.readTree(input);
        Iterator<String> keysIter = (fieldNames == null ? root.fieldNames() : fieldNames.iterator());
        while (keysIter.hasNext()) {
            String key = keysIter.next();
            if (exclude.contains(key.toLowerCase())) {
                continue;
            }
            JsonNode node = root.path(key);
            if (node.isArray()) {
                final List<Object> nodeValue = Lists.newArrayListWithExpectedSize(node.size());
                for (final JsonNode subnode : node) {
                    final Object subnodeValue = valueFunction.apply(subnode);
                    if (subnodeValue != null) {
                        nodeValue.add(subnodeValue);
                    }
                }
                // difference from JSONParser parse()
                map.put(key.toLowerCase(), nodeValue);
            } else {
                final Object nodeValue = valueFunction.apply(node);
                if (nodeValue != null) {
                    // difference from JSONParser parse()
                    map.put(key.toLowerCase(), nodeValue);
                }
            }
        }
        return map;
    } catch (Exception e) {
        throw new ParseException(e, "Unable to parse row [%s]", input);
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkedHashMap(java.util.LinkedHashMap)

Example 78 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project elasticsearch by elastic.

the class GeoIpCacheTests method testCachesAndEvictsResults.

public void testCachesAndEvictsResults() throws Exception {
    GeoIpCache cache = new GeoIpCache(1);
    final NodeCache.Loader loader = key -> new IntNode(key);
    JsonNode jsonNode1 = cache.get(1, loader);
    assertSame(jsonNode1, cache.get(1, loader));
    // evict old key by adding another value
    cache.get(2, loader);
    assertNotSame(jsonNode1, cache.get(1, loader));
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) NodeCache(com.maxmind.db.NodeCache) IntNode(com.fasterxml.jackson.databind.node.IntNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ESTestCase(org.elasticsearch.test.ESTestCase) IntNode(com.fasterxml.jackson.databind.node.IntNode) NodeCache(com.maxmind.db.NodeCache) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 79 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project zeppelin by apache.

the class SparkRInterpreter method interpret.

@Override
public InterpreterResult interpret(String lines, InterpreterContext interpreterContext) {
    SparkInterpreter sparkInterpreter = getSparkInterpreter();
    sparkInterpreter.populateSparkWebUrl(interpreterContext);
    String jobGroup = Utils.buildJobGroupId(interpreterContext);
    sparkInterpreter.getSparkContext().setJobGroup(jobGroup, "Zeppelin", false);
    String imageWidth = getProperty("zeppelin.R.image.width");
    String[] sl = lines.split("\n");
    if (sl[0].contains("{") && sl[0].contains("}")) {
        String jsonConfig = sl[0].substring(sl[0].indexOf("{"), sl[0].indexOf("}") + 1);
        ObjectMapper m = new ObjectMapper();
        try {
            JsonNode rootNode = m.readTree(jsonConfig);
            JsonNode imageWidthNode = rootNode.path("imageWidth");
            if (!imageWidthNode.isMissingNode())
                imageWidth = imageWidthNode.textValue();
        } catch (Exception e) {
            logger.warn("Can not parse json config: " + jsonConfig, e);
        } finally {
            lines = lines.replace(jsonConfig, "");
        }
    }
    String setJobGroup = "";
    // assign setJobGroup to dummy__, otherwise it would print NULL for this statement
    if (Utils.isSpark2()) {
        setJobGroup = "dummy__ <- setJobGroup(\"" + jobGroup + "\", \"zeppelin sparkR job group description\", TRUE)";
    } else if (getSparkInterpreter().getSparkVersion().newerThanEquals(SparkVersion.SPARK_1_5_0)) {
        setJobGroup = "dummy__ <- setJobGroup(sc, \"" + jobGroup + "\", \"zeppelin sparkR job group description\", TRUE)";
    }
    logger.debug("set JobGroup:" + setJobGroup);
    lines = setJobGroup + "\n" + lines;
    try {
        // render output with knitr
        if (useKnitr()) {
            zeppelinR.setInterpreterOutput(null);
            zeppelinR.set(".zcmd", "\n```{r " + renderOptions + "}\n" + lines + "\n```");
            zeppelinR.eval(".zres <- knit2html(text=.zcmd)");
            String html = zeppelinR.getS0(".zres");
            RDisplay rDisplay = render(html, imageWidth);
            return new InterpreterResult(rDisplay.code(), rDisplay.type(), rDisplay.content());
        } else {
            // alternatively, stream the output (without knitr)
            zeppelinR.setInterpreterOutput(interpreterContext.out);
            zeppelinR.eval(lines);
            return new InterpreterResult(InterpreterResult.Code.SUCCESS, "");
        }
    } catch (Exception e) {
        logger.error("Exception while connecting to R", e);
        return new InterpreterResult(InterpreterResult.Code.ERROR, e.getMessage());
    } finally {
        try {
        } catch (Exception e) {
        // Do nothing...
        }
    }
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException)

Example 80 with JsonNode

use of com.fasterxml.jackson.databind.JsonNode in project hive by apache.

the class TestCodahaleMetrics method testFileReporting.

@Test
public void testFileReporting() throws Exception {
    int runs = 5;
    for (int i = 0; i < runs; i++) {
        MetricsFactory.getInstance().incrementCounter("count2");
    }
    byte[] jsonData = MetricsTestUtils.getFileData(jsonReportFile.getAbsolutePath(), 2000, 3);
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode rootNode = objectMapper.readTree(jsonData);
    JsonNode countersNode = rootNode.path("counters");
    JsonNode methodCounterNode = countersNode.path("count2");
    JsonNode countNode = methodCounterNode.path("count");
    Assert.assertEquals(countNode.asInt(), 5);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1055 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)275 Test (org.junit.Test)267 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)165 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)125 IOException (java.io.IOException)124 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)97 HashMap (java.util.HashMap)78 ArrayList (java.util.ArrayList)75 HttpGet (org.apache.http.client.methods.HttpGet)69 JsonException (jmri.server.json.JsonException)66 Deployment (org.activiti.engine.test.Deployment)66 InputStream (java.io.InputStream)64 StringEntity (org.apache.http.entity.StringEntity)54 ByteArrayInputStream (java.io.ByteArrayInputStream)53 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)49 Map (java.util.Map)45 Task (org.activiti.engine.task.Task)41 HttpPost (org.apache.http.client.methods.HttpPost)39 Tree (org.apache.jackrabbit.oak.api.Tree)30