Example usage for com.fasterxml.jackson.databind JsonNode get

List of usage examples for com.fasterxml.jackson.databind JsonNode get

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode get.

Prototype

public JsonNode get(String paramString) 

Source Link

Usage

From source file:com.gsma.mobileconnect.utils.JsonUtils.java

/**
 * Return the integer value of an optional child node.
 * <p>/*from w  w w  . ja  v  a 2s. co  m*/
 * Check the parent node for the named child, if found return the integer contents of the child node, return null otherwise.
 *
 * @param parentNode The node to check
 * @param name The name of the optional child.
 * @return Integer value of the child node if present, null otherwise.
 */
static private Integer getOptionalIntegerValue(JsonNode parentNode, String name) {
    JsonNode childNode = parentNode.get(name);
    if (null == childNode) {
        return null;
    } else {
        return childNode.asInt();
    }
}

From source file:com.gsma.mobileconnect.utils.JsonUtils.java

/**
 * Return the long value of an optional child node.
 * <p>//from  w  w  w. j a va 2 s  .  c  o  m
 * Check the parent node for the named child, if found return the long contents of the child node, return null otherwise.
 *
 * @param parentNode The node to check
 * @param name The name of the optional child.
 * @return Long value of the child node if present, null otherwise.
 */
static private Long getOptionalLongValue(JsonNode parentNode, String name) {
    JsonNode childNode = parentNode.get(name);
    if (null == childNode) {
        return null;
    } else {
        return childNode.asLong();
    }
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClient.java

private static String extractIdFromJsonNode(JsonNode jsonNode) {
    return jsonNode.get("id").asText();
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClient.java

private static String extractTitleFromJsonNode(JsonNode jsonNode) {
    return jsonNode.get("title").asText();
}

From source file:jp.opencollector.guacamole.auth.delegated.DelegatedAuthenticationProvider.java

private static Optional<GuacamoleConfiguration> buildConfigurationFromRequest(HttpServletRequest req)
        throws GuacamoleException {
    try {// w  ww . ja va 2 s. co  m
        if (req.getClass().getName().equals("org.glyptodon.guacamole.net.basic.rest.APIRequest")) {
            final GuacamoleConfiguration config = new GuacamoleConfiguration();
            final String protocol = req.getParameter("protocol");
            if (protocol == null)
                throw new GuacamoleException("required parameter \"protocol\" is missing");
            config.setProtocol(protocol);
            for (Map.Entry<String, String[]> param : req.getParameterMap().entrySet()) {
                String[] values = param.getValue();
                if (values.length > 0)
                    config.setParameter(param.getKey(), values[0]);
            }
            return Optional.of(config);
        } else {
            final ServletInputStream is = req.getInputStream();
            if (!is.isReady()) {
                MediaType contentType = MediaType.parse(req.getContentType());
                boolean invalidContentType = true;
                if (contentType.type().equals("application")) {
                    if (contentType.subtype().equals("json")) {
                        invalidContentType = false;
                    } else if (contentType.subtype().equals("x-www-form-urlencoded")
                            && req.getParameter("token") != null) {
                        return Optional.<GuacamoleConfiguration>absent();
                    }
                }
                if (invalidContentType)
                    throw new GuacamoleException(String.format("expecting application/json, got %s",
                            contentType.withoutParameters()));
                final GuacamoleConfiguration config = new GuacamoleConfiguration();
                try {
                    final ObjectMapper mapper = new ObjectMapper();
                    JsonNode root = (JsonNode) mapper.readTree(
                            createJsonParser(req.getInputStream(), contentType.charset().or(UTF_8), mapper));
                    {
                        final JsonNode protocol = root.get("protocol");
                        if (protocol == null)
                            throw new GuacamoleException("required parameter \"protocol\" is missing");
                        final JsonNode parameters = root.get("parameters");
                        if (parameters == null)
                            throw new GuacamoleException("required parameter \"parameters\" is missing");
                        config.setProtocol(protocol.asText());
                        {
                            for (Iterator<Entry<String, JsonNode>> i = parameters.fields(); i.hasNext();) {
                                Entry<String, JsonNode> member = i.next();
                                config.setParameter(member.getKey(), member.getValue().asText());
                            }
                        }
                    }
                } catch (ClassCastException e) {
                    throw new GuacamoleException("error occurred during parsing configuration", e);
                }
                return Optional.of(config);
            } else {
                return Optional.<GuacamoleConfiguration>absent();
            }
        }
    } catch (IOException e) {
        throw new GuacamoleException("error occurred during retrieving configuration from the request body", e);
    }
}

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static Message JSONtoUserDataMessage(JsonNode result, UserData userData) {
    Message ret = new Message();
    Bundle data = new Bundle();
    if (result != null) {
        data.putBoolean("success", result.get("success").asBoolean());
        data.putString("info", result.get("info").asText());
        JsonNode dataNode = result.get("data");
        if (dataNode != null) {
            data.putInt("id", dataNode.get("id").asInt());
            data.putString("name", dataNode.get("name").asText());
            data.putString("email", dataNode.get("email").asText());
            if (dataNode.has("image_url"))
                data.putString("image_url", dataNode.get("image_url").asText());
        }//from w ww . j  a  va 2  s  . c  om
        ret.setData(data);
    }
    return ret;
}

From source file:com.ning.metrics.serialization.event.SmileEnvelopeEvent.java

@SuppressWarnings("deprecation")
public static Granularity getGranularityFromJson(final JsonNode node) {
    final JsonNode granularityNode = node.get(SMILE_EVENT_GRANULARITY_TOKEN_NAME);

    if (granularityNode == null) {
        return Granularity.HOURLY;
    }//  w w w .  ja  v  a 2s  .com
    try {
        return Granularity.valueOf(granularityNode.asText());
    } catch (IllegalArgumentException e) {
        // hmmmh. Returning null seems dangerous; but that's what we had...
        return null;
    }
}

From source file:controllers.TestApplication.java

public static WebSocket<JsonNode> testMeasurements() {
    return new WebSocket<JsonNode>() {
        public void onReady(final In<JsonNode> in, final Out<JsonNode> out) {
            final ActorRef measureActor = Akka.system().actorOf(Props.create(TestMeasureConnection.class, out));

            in.onMessage(new F.Callback<JsonNode>() {
                @Override/*from w w  w . j  a v a2 s  . co m*/
                public void invoke(JsonNode jsonNode) throws Throwable {
                    if (jsonNode.has("action") && "retrieve".equals(jsonNode.get("action").textValue())) {
                        final ActorRef measureActor = Akka.system()
                                .actorOf(Props.create(TestMeasureConnection.class, out));
                        Inbox inbox = Inbox.create(Akka.system());
                        for (Measure m = TestRunner.getMeasureFromQueue(); m != null; m = TestRunner
                                .getMeasureFromQueue()) {
                            inbox.send(measureActor, m);
                        }
                    }
                }
            });

            in.onClose(new F.Callback0() {
                @Override
                public void invoke() throws Throwable {
                    Akka.system().stop(measureActor);
                }
            });

        }
    };
}

From source file:org.jetbrains.webdemo.examples.ExamplesLoader.java

private static Example loadExample(String path, String parentUrl, boolean loadTestVersion,
        List<ObjectNode> commonFilesManifests) throws IOException {
    File manifestFile = new File(path + File.separator + "manifest.json");
    try (BufferedInputStream reader = new BufferedInputStream(new FileInputStream(manifestFile))) {
        ObjectNode manifest = (ObjectNode) JsonUtils.getObjectMapper().readTree(reader);

        String name = new File(path).getName();
        String id = (parentUrl + name).replaceAll(" ", "%20");
        String args = manifest.has("args") ? manifest.get("args").asText() : "";
        String runConfiguration = manifest.get("confType").asText();
        boolean searchForMain = manifest.has("searchForMain") ? manifest.get("searchForMain").asBoolean()
                : true;//w  ww.  j  av a 2s. c  o m
        String expectedOutput;
        List<String> readOnlyFileNames = new ArrayList<>();
        List<ProjectFile> files = new ArrayList<>();
        List<ProjectFile> hiddenFiles = new ArrayList<>();
        if (manifest.has("expectedOutput")) {
            expectedOutput = manifest.get("expectedOutput").asText();
        } else if (manifest.has("expectedOutputFile")) {
            Path expectedOutputFilePath = Paths
                    .get(path + File.separator + manifest.get("expectedOutputFile").asText());
            expectedOutput = new String(Files.readAllBytes(expectedOutputFilePath));
        } else {
            expectedOutput = null;
        }
        String help = null;
        File helpFile = new File(path + File.separator + "task.md");
        if (helpFile.exists()) {
            PegDownProcessor processor = new PegDownProcessor(org.pegdown.Extensions.FENCED_CODE_BLOCKS);
            String helpInMarkdown = new String(Files.readAllBytes(helpFile.toPath()));
            help = new GFMNodeSerializer().toHtml(processor.parseMarkdown(helpInMarkdown.toCharArray()));
        }

        List<JsonNode> fileManifests = new ArrayList<JsonNode>();
        if (manifest.has("files")) {
            for (JsonNode fileManifest : manifest.get("files")) {
                fileManifests.add(fileManifest);
            }
        }
        fileManifests.addAll(commonFilesManifests);
        for (JsonNode fileDescriptor : fileManifests) {

            if (loadTestVersion && fileDescriptor.has("skipInTestVersion")
                    && fileDescriptor.get("skipInTestVersion").asBoolean()) {
                continue;
            }

            String filePath = fileDescriptor.has("path") ? fileDescriptor.get("path").asText()
                    : path + File.separator + fileDescriptor.get("filename").textValue();
            ExampleFile file = loadExampleFile(filePath, id, fileDescriptor);
            if (!loadTestVersion && file.getType().equals(ProjectFile.Type.SOLUTION_FILE)) {
                continue;
            }
            if (!file.isModifiable()) {
                readOnlyFileNames.add(file.getName());
            }

            if (file.isHidden()) {
                hiddenFiles.add(file);
            } else {
                files.add(file);
            }
        }
        loadDefaultFiles(path, id, files, loadTestVersion);

        return new Example(id, name, args, runConfiguration, id, expectedOutput, searchForMain, files,
                hiddenFiles, readOnlyFileNames, help);
    } catch (IOException e) {
        System.err.println("Can't load project: " + e.toString());
        return null;
    }
}

From source file:com.ibm.iotf.connector.Connector.java

public static <T> T parseEnv(String serviceName, Class<T> classType) {
    try {/*  w ww. jav  a2 s. co m*/
        String vcap = System.getenv("VCAP_SERVICES");

        if (vcap == null) {
            logger.log(Level.FATAL, "VCAP_SERVICES env var not defined");
            return null;
        }

        ObjectMapper mapper = new ObjectMapper();
        JsonNode vcapServicesJson = mapper.readValue(vcap, JsonNode.class);

        // only attempt to parse the config if the env has an entry for the requested service
        if (vcapServicesJson.has(serviceName)) {
            T env = mapper.readValue(vcapServicesJson.get(serviceName).get(0).toString(), classType);
            return env;
        } else {
            logger.log(Level.FATAL, "Error parsing VCAP_SERVICES:  Could not find a service instance for '"
                    + serviceName + "'.");
        }
    } catch (Exception e) {
        logger.log(Level.FATAL, "Error parsing service configuraton from VCAP_SERVICES for service '"
                + serviceName + "': " + e.getMessage(), e);
    }

    return null;
}