Example usage for com.fasterxml.jackson.databind.node ArrayNode add

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode add

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node ArrayNode add.

Prototype

public ArrayNode add(JsonNode paramJsonNode) 

Source Link

Usage

From source file:com.ibm.watson.catalyst.corpus.tfidf.sentences.Frequencies.java

public JsonNode toJsonNode(int min) {
    ObjectNode result = MAPPER.createObjectNode();

    Iterator<String> strings = sortedIterator(min);

    ArrayNode stringsArrayNode = MAPPER.createArrayNode();
    while (strings.hasNext()) {
        ObjectNode aStringNode = MAPPER.createObjectNode();
        String s = strings.next();
        aStringNode.put("string", s);
        aStringNode.put("frequency", get(s));
        stringsArrayNode.add(aStringNode);
    }/* w w w . j  a va  2  s .  c o m*/
    result.set("strings", stringsArrayNode);

    return result;
}

From source file:com.redhat.lightblue.ResponseTest.java

@Test
public void testWithDataErrors() {
    ObjectNode objNode = JsonObject.getFactory().objectNode();
    ArrayNode arr = JsonObject.getFactory().arrayNode();

    for (DataError err : getPopulatedDataErrors(3)) {
        arr.add(err.toJson());
    }/*from   ww w.ja v a2s.  co  m*/

    objNode.set("dataErrors", arr);
    builder.withDataErrors(arr);

    for (int i = 0; i < builder.buildResponse().getDataErrors().size(); i++) {
        DataError de = builder.buildResponse().getDataErrors().get(i);

        for (int j = 0; j < de.getErrors().size(); j++) {
            Error error = de.getErrors().get(j);
            assertTrue(error.getErrorCode().equals(getPopulatedErrors(3).get(j).getErrorCode()));
        }
    }

}

From source file:scott.barleyrs.rest.EntityResultMessageBodyWriter.java

private void putNode(ObjectMapper mapper, ObjectNode jsonEntity, Node node, Set<Entity> started) {
    if (node instanceof ValueNode) {
        setValue(jsonEntity, node.getName(), (ValueNode) node);
    } else if (node instanceof RefNode) {
        Entity reffedEntity = ((RefNode) node).getReference(false);
        if (reffedEntity != null) {
            if (reffedEntity.isLoadedOrNew()) {
                JsonNode je = toJson(mapper, reffedEntity, started);
                if (je != null) {
                    jsonEntity.set(node.getName(), je);
                }//  w w  w .ja v a 2 s .c  o  m
            } else if (reffedEntity.isFetchRequired()) {
                /*
                 * a fetch is required, we just output the ID
                 */
                jsonEntity.put(node.getName(), reffedEntity.getKey().getValue().toString());
            }

        } else {
            jsonEntity.putNull(node.getName());
        }
    } else if (node instanceof ToManyNode) {
        ToManyNode tm = (ToManyNode) node;
        if (!tm.getList().isEmpty()) {
            ArrayNode array = jsonEntity.arrayNode();
            for (Entity e : tm.getList()) {
                JsonNode je = toJson(mapper, e, started);
                if (je != null) {
                    array.add(je);
                }
            }
            jsonEntity.set(tm.getName(), array);
        }
    }
}

From source file:com.hengyi.japp.print.server.rest.JappExceptionMapper.java

private void getError(ArrayNode arrayErrors, Throwable exception) {
    if (exception == null) {
        return;//from w  w w .j a v a 2 s  . c om
    }

    if (exception instanceof AppException) {
        AppException appException = (AppException) exception;
        ObjectNode error = JsonNodeFactory.instance.objectNode().put("errorCode", appException.getErrorCode());
        arrayErrors.add(error);
    } else if (exception instanceof AppMultiException) {
        AppMultiException appMultiException = (AppMultiException) exception;
        appMultiException.getAppExceptions().forEach(
                o -> arrayErrors.add(JsonNodeFactory.instance.objectNode().put("errorCode", o.getErrorCode())));
    } else if (StringUtils.isNotBlank(exception.getMessage())) {
        arrayErrors.add(JsonNodeFactory.instance.objectNode().put("errorMsg", exception.getMessage()));
    }

    getError(arrayErrors, exception.getCause());
}

From source file:org.opendaylight.sfc.sbrest.json.SfgExporterFactory.java

@Override
public String exportJson(DataObject dataObject) {

    String ret = null;//from www .j ava  2  s . c  o m
    if (dataObject instanceof ServiceFunctionGroup) {
        ServiceFunctionGroup sfg = (ServiceFunctionGroup) dataObject;

        ArrayNode sfgArray = mapper.createArrayNode();
        ObjectNode sfgNode = mapper.createObjectNode();
        sfgNode.put(_NAME, sfg.getName());
        sfgNode.put(_IP_MGMT_ADDRESS, ExporterUtil.convertIpAddress(sfg.getIpMgmtAddress()));
        sfgNode.put(_ALGORITHM, sfg.getAlgorithm());
        if (sfg.getRestUri() != null) {
            sfgNode.put(_REST_URI, sfg.getRestUri().getValue());
        }
        if (sfg.getType() != null) {
            sfgNode.put(_TYPE, SERVICE_FUNCTION_TYPE_PREFIX + sfg.getType().getValue().toLowerCase());
        }

        // this should be revamped
        if (sfg.getSfcServiceFunction() != null) {
            ArrayNode sfArray = mapper.createArrayNode();
            for (SfcServiceFunction entry : sfg.getSfcServiceFunction()) {
                ObjectNode o = mapper.createObjectNode();
                o.put(_NAME, entry.getName().getValue());
                sfArray.add(o);
            }
            sfgNode.putArray(_SERVICE_FUNCTION).addAll(sfArray);
        }

        sfgArray.add(sfgNode);
        try {
            Object sfObject = mapper.treeToValue(sfgArray, Object.class);
            ret = mapper.writeValueAsString(sfObject);
            ret = "{\"" + _SERVICE_FUNCTION_GROUP + "\":" + ret + "}";
            LOG.debug("Created Service Function Group JSON: {}", ret);
        } catch (JsonProcessingException e) {
            LOG.error("Error during creation of JSON for Service Function {}", sfg.getName());
        }

    } else {
        throw new IllegalArgumentException("Argument is not an instance of ServiceFunction");
    }

    return ret;
}

From source file:org.eclipse.winery.repository.Utils.java

public static String getAllXSDefinitionsForTypeAheadSelection(short type) {
    SortedSet<XSDImportId> allImports = Repository.INSTANCE.getAllTOSCAComponentIds(XSDImportId.class);

    Map<Namespace, Collection<String>> data = new HashMap<>();

    for (XSDImportId id : allImports) {
        XSDImportResource resource = new XSDImportResource(id);
        Collection<String> allLocalNames = resource.getAllDefinedLocalNames(type);

        Collection<String> list;
        if ((list = data.get(id.getNamespace())) == null) {
            // list does not yet exist
            list = new ArrayList<>();
            data.put(id.getNamespace(), list);
        }//from  ww w. ja v a  2s  . c o m
        list.addAll(allLocalNames);
    }

    ArrayNode rootNode = Utils.mapper.createArrayNode();

    // ensure ordering in JSON object
    Collection<Namespace> allns = new TreeSet<>();
    allns.addAll(data.keySet());

    for (Namespace ns : allns) {
        Collection<String> localNames = data.get(ns);
        if (!localNames.isEmpty()) {
            ObjectNode groupEntry = Utils.mapper.createObjectNode();
            rootNode.add(groupEntry);
            groupEntry.put("text", ns.getDecoded());
            ArrayNode children = Utils.mapper.createArrayNode();
            groupEntry.put("children", children);
            Collection<String> sortedLocalNames = new TreeSet<>();
            sortedLocalNames.addAll(localNames);
            for (String localName : sortedLocalNames) {
                String value = "{" + ns.getDecoded() + "}" + localName;
                //noinspection UnnecessaryLocalVariable
                String text = localName;
                ObjectNode o = Utils.mapper.createObjectNode();
                o.put("text", text);
                o.put("value", value);
                children.add(o);
            }
        }
    }

    try {
        return Utils.mapper.writeValueAsString(rootNode);
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Could not create JSON", e);
    }
}

From source file:org.envirocar.server.rest.util.GeoJSON.java

protected ArrayNode encodeCoordinates(CoordinateSequence coordinates) throws GeometryConverterException {
    ArrayNode list = getJsonFactory().arrayNode();
    for (int i = 0; i < coordinates.size(); ++i) {
        ArrayNode coordinate = getJsonFactory().arrayNode();
        coordinate.add(coordinates.getX(i));
        coordinate.add(coordinates.getY(i));
        list.add(coordinate);//from   w  ww  .j  av  a  2  s .c  o  m
    }
    return list;
}

From source file:com.sqs.tq.fdc.FileVariantAnalyser.java

private ObjectNode convertToJson(List<GroupData> hashSortedByCount) {
    ObjectNode result = JsonNodeFactory.instance.objectNode();
    ArrayNode groupsNode = result.arrayNode();

    result.put("name", "???");
    result.set("groups", groupsNode);

    for (GroupData gd : hashSortedByCount) {
        ObjectNode jgd = groupsNode.objectNode();
        ArrayNode filesNode = groupsNode.arrayNode();

        groupsNode.add(jgd);
        jgd.put("hash", gd.hash);
        jgd.set("files", filesNode);

        for (FileData fd : gd.data) {
            filesNode.add(fd.fqn);/*from www.  j a  va2 s . c  o  m*/
        }
    }
    return result;
}

From source file:org.walkmod.conf.providers.yml.RemoveModulesYMLAction.java

@Override
public void doAction(JsonNode node) throws Exception {
    if (node.has("modules")) {
        JsonNode aux = node.get("modules");
        ObjectMapper mapper = provider.getObjectMapper();
        if (aux.isArray()) {
            ArrayNode modulesList = (ArrayNode) node.get("modules");
            Iterator<JsonNode> it = modulesList.iterator();
            ArrayNode newModulesList = new ArrayNode(mapper.getNodeFactory());
            while (it.hasNext()) {
                JsonNode next = it.next();
                if (next.isTextual()) {
                    String text = next.asText();
                    if (!modules.contains(text)) {
                        newModulesList.add(text);
                    }//from   w w  w  .j  ava 2s  . c  o  m
                }
            }
            ObjectNode oNode = (ObjectNode) node;
            if (newModulesList.size() > 0) {
                oNode.set("modules", newModulesList);
            } else {
                oNode.remove("modules");
            }
            provider.write(node);
        }
    }
}

From source file:com.almende.eve.algorithms.test.TestGraph.java

/**
 * Write visGraph./* ww  w .  ja va  2s.  co m*/
 *
 * @param agents
 *            the agents
 * @return the string
 */
private String writeVisGraph(List<NodeAgent> agents) {
    final ObjectNode result = JOM.createObjectNode();
    final ArrayNode nodes = JOM.createArrayNode();
    final ArrayNode edges = JOM.createArrayNode();

    for (NodeAgent agent : agents) {
        final ObjectNode node = JOM.createObjectNode();
        node.put("id", agent.getId());
        node.put("label", agent.getId());
        nodes.add(node);
        for (Edge edge : agent.getGraph().getEdges()) {
            final ObjectNode edgeNode = JOM.createObjectNode();
            edgeNode.put("from", agent.getId());
            edgeNode.put("to", edge.getAddress().toASCIIString().replace("local:", ""));
            edges.add(edgeNode);
        }
    }

    result.set("nodes", nodes);
    result.set("edges", edges);
    return result.toString();
}