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:org.onosproject.tvue.TopologyResource.java

private ArrayNode json(ObjectMapper mapper, Path path) {
    ArrayNode pathNode = mapper.createArrayNode();
    for (Link link : path.links()) {
        ObjectNode linkNode = mapper.createObjectNode().put("src", id(link.src())).put("dst", id(link.dst()));
        pathNode.add(linkNode);
    }// w w  w  .j a va  2  s . co m
    return pathNode;
}

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

@Override
public String exportJsonNameOnly(DataObject dataObject) {

    String ret = null;/*from w  w  w .  j  a va  2  s  .c o  m*/
    if (dataObject instanceof Acl) {
        Acl acl = (Acl) dataObject;

        ObjectNode aclNode = mapper.createObjectNode();
        aclNode.put(_ACL_NAME, acl.getAclName());
        ArrayNode aclArray = mapper.createArrayNode();
        aclArray.add(aclNode);

        ret = "{\"" + _ACL + "\":" + aclArray.toString() + "}";
        LOG.debug("Created Access List JSON: {}", ret);

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

    return ret;
}

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

@Override
public String exportJsonNameOnly(DataObject dataObject) {
    String ret = null;/*from  w w w .  ja va2 s . co m*/

    if (dataObject instanceof ServiceFunctionState) {
        ServiceFunctionState obj = (ServiceFunctionState) dataObject;

        ObjectNode node = mapper.createObjectNode();
        if (obj.getName() != null) {
            node.put(_NAME, obj.getName().getValue());
        }
        ArrayNode sfstateArray = mapper.createArrayNode();
        sfstateArray.add(node);
        ret = "{\"" + _SERVICE_FUNCTION_STATE + "\":" + sfstateArray.toString() + "}";
    } else {
        throw new IllegalArgumentException("Argument is not an instance of ServiceFunctionState");
    }

    return ret;
}

From source file:com.github.reinert.jjschema.v1.EnumSchemaWrapper.java

private <T> void processEnum(Class<T> type) {
    ArrayNode enumArray = getNode().putArray("enum");
    for (T constant : type.getEnumConstants()) {
        String value = constant.toString();
        // Check if value is numeric
        try {/*from   w ww .  ja  v  a 2s .  co m*/
            // First verifies if it is an integer
            Long integer = Long.parseLong(value);
            enumArray.add(integer);
            setType("integer");
        }
        // If not then verifies if it is an floating point number
        catch (NumberFormatException e) {
            try {
                BigDecimal number = new BigDecimal(value);
                enumArray.add(number);
                setType("number");
            }
            // Otherwise add as String
            catch (NumberFormatException e1) {
                enumArray.add(value);
                setType("string");
            }
        }
    }
}

From source file:com.unboundid.scim2.server.utils.ResourceTrimmer.java

/**
 * Trim attributes of the values in the array node to return.
 *
 * @param arrayNode The array node to return.
 * @param parentPath  The parent path of attributes in the array.
 * @return The trimmed object node ready to return to the client.
 *//*from  ww w.  ja  va  2s  . c  om*/
protected ArrayNode trimArrayNode(final ArrayNode arrayNode, final Path parentPath) {
    ArrayNode arrayToReturn = JsonUtils.getJsonNodeFactory().arrayNode();
    for (JsonNode value : arrayNode) {
        if (value.isArray()) {
            ArrayNode trimmedNode = trimArrayNode((ArrayNode) value, parentPath);
            if (trimmedNode.size() > 0) {
                arrayToReturn.add(trimmedNode);
            }
        } else if (value.isObject()) {
            ObjectNode trimmedNode = trimObjectNode((ObjectNode) value, parentPath);
            if (trimmedNode.size() > 0) {
                arrayToReturn.add(trimmedNode);
            }
        } else {
            arrayToReturn.add(value);
        }
    }
    return arrayToReturn;
}

From source file:com.redhat.lightblue.crud.ldap.translator.ResultTranslator.java

private JsonNode toJson(ArrayField field, Attribute attr, FieldCursor fieldCursor) {
    if (!fieldCursor.firstChild()) {
        //TODO: Should an exception be thrown here?
        return null;
    }//from ww  w .  ja v  a2 s. co  m

    FieldTreeNode node = fieldCursor.getCurrentNode();

    ArrayElement arrayElement = field.getElement();
    ArrayNode valueNode = factory.arrayNode();

    if (arrayElement instanceof SimpleArrayElement) {
        String[] values = attr.getValues();
        for (String value : values) {
            valueNode.add(node.getType().toJson(factory, value));
        }
    } else if (arrayElement instanceof ObjectArrayElement) {
        throw new UnsupportedOperationException("Object ArrayField type is not currently supported.");
    } else {
        throw new UnsupportedOperationException(
                "ArrayElement type is not supported: " + node.getClass().getName());
    }

    fieldCursor.parent();
    return valueNode;
}

From source file:org.activiti.rest.service.api.legacy.management.TablesResource.java

@Get
public ObjectNode getTables() {
    if (authenticate(SecuredResource.ADMIN) == false)
        return null;

    Map<String, Long> tableCounts = ActivitiUtil.getManagementService().getTableCount();
    ArrayList<String> tableNames = new ArrayList<String>(tableCounts.keySet());
    Collections.sort(tableNames);

    ObjectNode responseJSON = new ObjectMapper().createObjectNode();

    ArrayNode tablesJSON = new ObjectMapper().createArrayNode();
    responseJSON.put("data", tablesJSON);

    for (String tableName : tableNames) {
        ObjectNode tableJSON = new ObjectMapper().createObjectNode();
        tableJSON.put("tableName", tableName);
        tableJSON.put("total", tableCounts.get(tableName));
        tablesJSON.add(tableJSON);
    }/*from   w  ww  . j av a2s.  c  o  m*/

    return responseJSON;
}

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

@Override
public String exportJson(DataObject dataObject) {

    String ret = null;/*from  w w  w.j  a  v  a 2  s  .co m*/
    if (dataObject instanceof ServiceFunction) {
        ServiceFunction sf = (ServiceFunction) dataObject;

        ArrayNode sfArray = mapper.createArrayNode();
        ObjectNode sfNode = mapper.createObjectNode();
        if (sf.getName() != null && sf.getName().getValue() != null) {
            sfNode.put(_NAME, sf.getName().getValue());
        }
        sfNode.put(_IP_MGMT_ADDRESS, ExporterUtil.convertIpAddress(sf.getIpMgmtAddress()));
        if (sf.getRestUri() != null) {
            sfNode.put(_REST_URI, sf.getRestUri().getValue());
        }
        if (sf.getType() != null) {
            sfNode.put(_TYPE, SERVICE_FUNCTION_TYPE_PREFIX + sf.getType().getValue().toLowerCase());
        }
        sfNode.put(_NSH_AWARE, sf.isNshAware());
        sfNode.put(_REQUEST_RECLASSIFICATION, sf.isRequestReclassification());

        if (sf.getSfDataPlaneLocator() != null) {
            ArrayNode locatorArray = mapper.createArrayNode();
            for (SfDataPlaneLocator sfDataPlaneLocator : sf.getSfDataPlaneLocator()) {
                ObjectNode sfLocatorNode = this.getSfDataPlaneLocatorObjectNode(sfDataPlaneLocator);
                locatorArray.add(sfLocatorNode);
            }
            sfNode.putArray(_SF_DATA_PLANE_LOCATOR).addAll(locatorArray);
        }

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

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

    return ret;
}

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

@Override
public String exportJson(DataObject dataObject) {

    String ret = null;//from   w  ww  . j  a va  2s  .com
    if (dataObject instanceof ServiceFunctionForwarder) {
        ServiceFunctionForwarder sff = (ServiceFunctionForwarder) dataObject;

        ArrayNode sffArray = mapper.createArrayNode();

        ObjectNode sffNode = mapper.createObjectNode();
        if (sff.getName() != null) {
            sffNode.put(_NAME, sff.getName().getValue());
        }
        if (sff.getIpMgmtAddress() != null) {
            sffNode.put(_IP_MGMT_ADDRESS, ExporterUtil.convertIpAddress(sff.getIpMgmtAddress()));
        }
        if (sff.getRestUri() != null) {
            sffNode.put(_REST_URI, sff.getRestUri().getValue());
        }
        if (sff.getServiceNode() != null) {
            sffNode.put(_SERVICE_NODE, sff.getServiceNode().getValue());
        }
        if (sff.getSffDataPlaneLocator() != null) {
            ArrayNode locatorArray = mapper.createArrayNode();
            for (SffDataPlaneLocator sffDataPlaneLocator : sff.getSffDataPlaneLocator()) {
                locatorArray.add(this.getSffDataPlaneLocatorObjectNode(sffDataPlaneLocator));
            }

            sffNode.putArray(_SFF_DATA_PLANE_LOCATOR).addAll(locatorArray);
        }

        if (sff.getServiceFunctionDictionary() != null) {
            ArrayNode dictionaryArray = mapper.createArrayNode();
            for (ServiceFunctionDictionary serviceFunctionDictionary : sff.getServiceFunctionDictionary()) {
                dictionaryArray.add(this.getSfDictionaryObjectNode(serviceFunctionDictionary));
            }

            sffNode.putArray(_SERVICE_FUNCTION_DICTIONARY).addAll(dictionaryArray);
        }

        sffArray.add(sffNode);
        try {
            Object sffObject = mapper.treeToValue(sffArray, Object.class);
            ret = mapper.writeValueAsString(sffObject);
            ret = "{\"" + _SERVICE_FUNCTION_FORWARDER + "\":" + ret + "}";
            LOG.debug("Created Service Function Forwarder JSON: {}", ret);
        } catch (JsonProcessingException e) {
            LOG.error("Error during creation of JSON for Service Function Forwarder {}", sff.getName());
        }
    } else {
        throw new IllegalArgumentException("Argument is not an instance of ServiceFunctionForwarder");
    }

    return ret;
}

From source file:scott.barleyrs.rest.QueryResultMessageBodyWriter.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) {
        if (((RefNode) node).getEntityKey() == NotLoaded.VALUE) {
            return;
        }/*from w  w  w.j  a va  2s  . c  o m*/
        Entity reffedEntity = ((RefNode) node).getReference(false);
        if (reffedEntity != null) {
            if (reffedEntity.isLoadedOrNew()) {
                /*
                 * we have the entities properties so we convert it to a json object
                 */
                JsonNode je = toJson(mapper, reffedEntity, started);
                if (je != null) {
                    jsonEntity.set(node.getName(), je);
                }
            } 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);
        }
    }
}