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

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

Introduction

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

Prototype

public Iterator<JsonNode> elements() 

Source Link

Usage

From source file:org.jmxtrans.embedded.config.ConfigurationParser.java

private void mergeEmbeddedJmxTransConfiguration(@Nonnull JsonNode configurationRootNode,
        @Nonnull EmbeddedJmxTrans embeddedJmxTrans) {
    for (JsonNode queryNode : configurationRootNode.path("queries")) {

        String objectName = queryNode.path("objectName").asText();
        Query query = new Query(objectName);
        embeddedJmxTrans.addQuery(query);
        JsonNode resultAliasNode = queryNode.path("resultAlias");
        if (resultAliasNode.isMissingNode()) {
        } else if (resultAliasNode.isValueNode()) {
            query.setResultAlias(resultAliasNode.asText());
        } else {//from  w w w.  j av a2s. co m
            logger.warn("Ignore invalid node {}", resultAliasNode);
        }

        JsonNode attributesNode = queryNode.path("attributes");
        if (attributesNode.isMissingNode()) {
        } else if (attributesNode.isArray()) {
            Iterator<JsonNode> itAttributeNode = attributesNode.elements();
            while (itAttributeNode.hasNext()) {
                JsonNode attributeNode = itAttributeNode.next();
                parseQueryAttributeNode(query, attributeNode);
            }
        } else {
            logger.warn("Ignore invalid node {}", resultAliasNode);
        }

        JsonNode attributeNode = queryNode.path("attribute");
        parseQueryAttributeNode(query, attributeNode);
        List<OutputWriter> outputWriters = parseOutputWritersNode(queryNode);
        query.getOutputWriters().addAll(outputWriters);
        logger.trace("Add {}", query);
    }

    List<OutputWriter> outputWriters = parseOutputWritersNode(configurationRootNode);
    embeddedJmxTrans.getOutputWriters().addAll(outputWriters);
    logger.trace("Add global output writers: {}", outputWriters);

    JsonNode queryIntervalInSecondsNode = configurationRootNode.path("queryIntervalInSeconds");
    if (!queryIntervalInSecondsNode.isMissingNode()) {
        embeddedJmxTrans.setQueryIntervalInSeconds(queryIntervalInSecondsNode.asInt());
    }

    JsonNode exportBatchSizeNode = configurationRootNode.path("exportBatchSize");
    if (!exportBatchSizeNode.isMissingNode()) {
        embeddedJmxTrans.setExportBatchSize(exportBatchSizeNode.asInt());
    }

    JsonNode numQueryThreadsNode = configurationRootNode.path("numQueryThreads");
    if (!numQueryThreadsNode.isMissingNode()) {
        embeddedJmxTrans.setNumQueryThreads(numQueryThreadsNode.asInt());
    }

    JsonNode exportIntervalInSecondsNode = configurationRootNode.path("exportIntervalInSeconds");
    if (!exportIntervalInSecondsNode.isMissingNode()) {
        embeddedJmxTrans.setExportIntervalInSeconds(exportIntervalInSecondsNode.asInt());
    }

    JsonNode numExportThreadsNode = configurationRootNode.path("numExportThreads");
    if (!numExportThreadsNode.isMissingNode()) {
        embeddedJmxTrans.setNumExportThreads(numExportThreadsNode.asInt());
    }

    logger.info("Loaded {}", embeddedJmxTrans);
}

From source file:com.bna.ezrxlookup.service.OpenFdaService.java

/**
 * Retrieve set of <code>DrugLabel</code> from JSON response
 * @param response//from  ww  w.j av  a  2s.  c o m
 * @return TreeSet with DrugLabel objects
 */
public Set<DrugLabel> getDrugLabelResults(String jsonResponse) throws Exception {
    Set<DrugLabel> labelList = new TreeSet<DrugLabel>();

    if (StringUtils.isEmpty(jsonResponse))
        return labelList;

    try {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(jsonResponse);
        JsonNode resultsNodes = rootNode.path(RESULTS_TAG);
        LOG.debug("Results size : " + resultsNodes.size());

        Iterator<JsonNode> elements = resultsNodes.elements();

        while (elements.hasNext()) {
            JsonNode aResultsNode = elements.next(); // results node
            JsonNode openFdaNode = aResultsNode.get(OPENFDA_TAG);

            DrugLabel label = new DrugLabel();
            label.setId(aResultsNode.path(ID_TAG).asText());
            label.setVersion(aResultsNode.path(VERSION_TAG).asText());
            label.setBrandName(openFdaNode.get(BRAND_NAME_TAG).elements().next().asText());
            label.setManufactureName(openFdaNode.get(MFR_NAME_TAG).elements().next().asText());

            labelList.add(label);
        }
    } catch (IOException e) {
        LOG.error(e);
        throw e;
    }

    LOG.debug(labelList);
    return labelList;
}

From source file:lumbermill.api.JsonEvent.java

@Override
public String valueAsString(String field) {
    if (this.jsonNode.has(field)) {

        JsonNode node = jsonNode.get(field);

        // TODO: This should find another home
        // Support for boolean expressions of arrays
        if (node instanceof ArrayNode) {
            Iterator<JsonNode> elements = node.elements();
            StringBuffer sb = new StringBuffer("[");
            while (elements.hasNext()) {
                sb.append("'").append(elements.next().asText()).append("'");
                if (elements.hasNext()) {
                    sb.append(",");
                }/*from ww  w  .j  ava 2 s. co  m*/
            }
            return sb.append("]").toString();
        }

        if (node == null) {
            return null;
        }

        return node.asText();
    }
    return super.valueAsString(field);
}

From source file:kz.nurlan.kaspandr.KaspandrWindow.java

private HashMap<String, Integer> getCountedLessonsByGroup(String lessons) {
    HashMap<String, Integer> groupMap = new HashMap<String, Integer>();
    try {//from w  w  w .  ja  v a  2  s .co m
        ObjectNode rootNode1 = mapper.readValue("{\"lessons\":[" + lessons + "]}", ObjectNode.class);
        JsonNode lessonsNode1 = rootNode1.get("lessons");

        groupMap.put("all", 0);

        if (lessonsNode1 != null && lessonsNode1.isArray()) {
            Iterator<JsonNode> it = lessonsNode1.elements();

            while (it.hasNext()) {
                JsonNode lesson = it.next();

                if (lesson.get("id") != null && !lesson.get("status").textValue().equalsIgnoreCase("deleted")
                        && lesson.get("group") != null && !lesson.get("group").textValue().isEmpty()) {
                    if (groupMap.containsKey(lesson.get("group").textValue()))
                        groupMap.put(lesson.get("group").textValue(),
                                groupMap.get(lesson.get("group").textValue()) + 1);
                    else
                        groupMap.put(lesson.get("group").textValue(), 1);
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return groupMap;
}

From source file:de.tudarmstadt.ukp.clarin.webanno.crowdflower.CrowdJob.java

/**
 * Create job arguments that are used by CrowdClient to build a POST request for a new job on Crowdflower
 *///ww  w .  ja  v a  2 s  . com
void createArgumentMaps() {
    argumentMap = new LinkedMultiValueMap<String, String>();
    includedCountries = new Vector<String>();
    excludedCountries = new Vector<String>();

    Iterator<Map.Entry<String, JsonNode>> jsonRootIt = template.fields();

    for (Map.Entry<String, JsonNode> elt; jsonRootIt.hasNext();) {
        elt = jsonRootIt.next();

        JsonNode currentNode = elt.getValue();
        String currentKey = elt.getKey();

        if (currentNode.isContainerNode()) {
            //special processing for these arrays:
            if (currentKey.equals(includedCountriesKey) || currentKey.equals(excludedCountriesKey)) {
                Iterator<JsonNode> jsonSubNodeIt = currentNode.elements();
                for (JsonNode subElt; jsonSubNodeIt.hasNext();) {
                    subElt = jsonSubNodeIt.next();
                    (currentKey.equals(includedCountriesKey) ? includedCountries : excludedCountries)
                            .addElement(subElt.path(countryCodeKey).asText());
                }

            }
        } else if (!currentNode.isNull() && argumentFilterSet.contains(currentKey)) {
            argumentMap.add(jobKey + "[" + currentKey + "]", currentNode.asText());
        }
        if (currentKey == idKey) {
            this.id = currentNode.asText();
        }
    }
}

From source file:org.carrot2.core.ProcessingResultTest.java

private void checkJsonClusters(final ProcessingResult result, final JsonNode root) {
    final JsonNode clusters = root.get("clusters");
    Assertions.assertThat(clusters).isNotNull();
    final ArrayList<JsonNode> clusterNodes = Lists.newArrayList(clusters.elements());
    Assertions.assertThat(clusterNodes).hasSize(result.getClusters().size());
}

From source file:org.carrot2.core.ProcessingResultTest.java

private void checkJsonDocuments(final ProcessingResult result, final JsonNode root) {
    final JsonNode documents = root.get("documents");
    Assertions.assertThat(documents).isNotNull();
    final ArrayList<JsonNode> documentNodes = Lists.newArrayList(documents.elements());
    Assertions.assertThat(documentNodes).hasSize(result.getDocuments().size());
}

From source file:org.jmxtrans.embedded.config.ConfigurationParser.java

protected void parseQueryAttributeNode(@Nonnull Query query, @Nonnull JsonNode attributeNode) {
    if (attributeNode.isMissingNode()) {
    } else if (attributeNode.isValueNode()) {
        query.addAttribute(attributeNode.asText());
    } else if (attributeNode.isObject()) {
        List<String> keys = null;

        JsonNode keysNode = attributeNode.path("keys");
        if (keysNode.isMissingNode()) {
        } else if (keysNode.isArray()) {
            if (keys == null) {
                keys = new ArrayList<String>();
            }/*from   w  w  w.  j a v a2  s .co m*/
            Iterator<JsonNode> itAttributeNode = keysNode.elements();
            while (itAttributeNode.hasNext()) {
                JsonNode keyNode = itAttributeNode.next();
                if (keyNode.isValueNode()) {
                    keys.add(keyNode.asText());
                } else {
                    logger.warn("Ignore invalid node {}", keyNode);
                }
            }
        } else {
            logger.warn("Ignore invalid node {}", keysNode);
        }

        JsonNode keyNode = attributeNode.path("key");
        if (keyNode.isMissingNode()) {
        } else if (keyNode.isValueNode()) {
            if (keys == null) {
                keys = new ArrayList<String>();
            }
            keys.add(keyNode.asText());
        } else {
            logger.warn("Ignore invalid node {}", keyNode);
        }

        String name = attributeNode.path("name").asText();
        JsonNode resultAliasNode = attributeNode.path("resultAlias");
        String resultAlias = resultAliasNode.isMissingNode() ? null : resultAliasNode.asText();
        JsonNode typeNode = attributeNode.path("type");
        String type = typeNode.isMissingNode() ? null : typeNode.asText();
        if (keys == null) {
            query.addAttribute(new QueryAttribute(name, type, resultAlias));
        } else {
            query.addAttribute(new QueryAttribute(name, type, resultAlias, keys));
        }
    } else {
        logger.warn("Ignore invalid node {}", attributeNode);
    }
}

From source file:com.sitewhere.wso2.identity.scim.Wso2ScimAssetModule.java

/**
 * Parse role information.//from   w  ww .  j a  v a  2  s.  co m
 * 
 * @param resource
 * @param asset
 */
protected void parseRoles(JsonNode resource, PersonAsset asset) {
    JsonNode groups = resource.get(IScimFields.GROUPS);
    if (groups != null) {
        Iterator<JsonNode> it = groups.elements();
        while (it.hasNext()) {
            JsonNode fields = it.next();
            JsonNode role = fields.get(IScimFields.DISPLAY);
            if (role != null) {
                asset.getRoles().add(role.textValue());
            }
        }
    }
}

From source file:com.redhat.lightblue.mongo.config.MongoConfiguration.java

@Override
public void initializeFromJson(JsonNode node) {
    if (node != null) {
        JsonNode x = node.get("connectionsPerHost");
        if (x != null) {
            connectionsPerHost = x.asInt();
        }/*from w ww  .  jav  a2  s .c  o m*/
        x = node.get("ssl");
        if (x != null) {
            ssl = x.asBoolean();
        }
        x = node.get("noCertValidation");
        if (x != null) {
            noCertValidation = x.asBoolean();
        }
        credentials = credentialsFromJson(node.get("credentials"));
        x = node.get("metadataDataStoreParser");
        try {
            if (x != null) {
                metadataDataStoreParser = Class.forName(x.asText());
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException(node.toString() + ":" + e);
        }
        x = node.get("database");
        if (x != null) {
            database = x.asText();
        }
        JsonNode jsonNodeServers = node.get("servers");
        if (jsonNodeServers != null && jsonNodeServers.isArray()) {
            Iterator<JsonNode> elements = jsonNodeServers.elements();
            while (elements.hasNext()) {
                JsonNode next = elements.next();
                try {
                    String host;
                    x = next.get("host");
                    if (x != null) {
                        host = x.asText();
                    } else {
                        host = null;
                    }

                    x = next.get("port");
                    if (x != null) {
                        addServerAddress(host, x.asInt());
                    } else {
                        addServerAddress(host);
                    }
                } catch (UnknownHostException e) {
                    throw new IllegalStateException(e);
                }
            }

        } else {
            JsonNode server = node.get("server");
            if (server != null) {
                try {
                    x = server.get("host");
                    if (x != null) {
                        String host = x.asText();
                        x = server.get("port");
                        if (x != null) {
                            setServer(host, x.asInt());
                        } else {
                            setServer(host);
                        }
                    } else {
                        throw new IllegalStateException("host is required in server");
                    }
                } catch (IllegalStateException | UnknownHostException e) {
                    throw new IllegalStateException(e);
                }
            }
        }
    }
}