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.turn.shapeshifter.NamedSchemaSerializer.java

/**
 * Serializes a repeated field./*from  w w w  .  j av a 2s  . c o  m*/
 *
 * @param message the message being serialized
 * @param registry a registry of schemas, for enclosed object types
 * @param field the descriptor of the repeated field to serialize
 * @param count the count of repeated items in the field
 * @return the JSON representation of the serialized
 * @throws SerializationException
 */
private ArrayNode serializeRepeatedField(Message message, ReadableSchemaRegistry registry,
        FieldDescriptor field, int count) throws SerializationException {
    ArrayNode array = new ArrayNode(JsonNodeFactory.instance);
    for (int i = 0; i < count; i++) {
        Object value = message.getRepeatedField(field, i);
        JsonNode valueNode = serializeValue(value, field, registry);
        if (!valueNode.isNull()) {
            array.add(valueNode);
        }
    }
    return array;
}

From source file:net.mostlyharmless.jghservice.connector.jira.UpdateVersionsOnIssue.java

@Override
public String getJson() throws JsonProcessingException {
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ObjectNode root = factory.objectNode();
    ObjectNode update = factory.objectNode();
    root.put("update", update);

    if (!fixVersionsAdded.isEmpty() || !fixVersionsRemoved.isEmpty()) {
        ArrayNode fixVersions = factory.arrayNode();
        for (String version : fixVersionsAdded) {
            ObjectNode add = factory.objectNode();
            ObjectNode addVersion = factory.objectNode();
            addVersion.put("name", version);
            add.put("add", addVersion);
            fixVersions.add(add);
        }/*from ww w .  j a  va 2 s . co m*/

        for (String version : fixVersionsRemoved) {
            ObjectNode remove = factory.objectNode();
            ObjectNode removeVersion = factory.objectNode();
            removeVersion.put("name", version);
            remove.put("remove", removeVersion);
            fixVersions.add(remove);
        }

        update.put("fixVersions", fixVersions);
    }

    if (!affectsVersionsAdded.isEmpty() || !affectsVersionsRemoved.isEmpty()) {
        ArrayNode affectsVersions = factory.arrayNode();
        for (String version : affectsVersionsAdded) {
            ObjectNode add = factory.objectNode();
            ObjectNode addVersion = factory.objectNode();
            addVersion.put("name", version);
            add.put("add", addVersion);
            affectsVersions.add(add);
        }

        for (String version : affectsVersionsRemoved) {
            ObjectNode remove = factory.objectNode();
            ObjectNode removeVersion = factory.objectNode();
            removeVersion.put("name", version);
            remove.put("remove", removeVersion);
            affectsVersions.add(remove);
        }

        update.put("versions", affectsVersions);
    }

    return new ObjectMapper().writeValueAsString(root);

}

From source file:io.fabric8.kubernetes.api.KubernetesHelper.java

protected static void addObjectsToItemArray(ArrayNode itemArray, Object object) throws IOException {
    JsonNode node = toJsonNode(object);//w  ww .ja v a  2 s . com
    JsonNode items = node.get("items");
    if (items != null && items.isArray()) {
        Iterator<JsonNode> iter = items.iterator();
        for (JsonNode item : items) {
            itemArray.add(item);
        }
    } else {
        itemArray.add(node);
    }
}

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

private ObjectNode getMatchesObjectNode(Matches matches) {
    if (matches == null) {
        return null;
    }/*from w w w.  j  a  v a 2  s . c  o  m*/

    ObjectNode matchesNode = mapper.createObjectNode();

    if (matches.getInputInterface() != null) {
        matchesNode.put(_INPUT_INTERFACE, matches.getInputInterface());
    }

    if (matches.getAceType() != null) {
        String aceType = matches.getAceType().getImplementedInterface().getSimpleName();

        switch (aceType) {
        case ACE_IP:
            AceIp aceIp = (AceIp) matches.getAceType();
            if (aceIp.getDscp() != null) {
                matchesNode.put(_DSCP, aceIp.getDscp().getValue());
            }
            matchesNode.put(_PROTOCOL, aceIp.getProtocol());
            matchesNode.put(_SOURCE_PORT_RANGE, this.getSourcePortRangeObjectNode(aceIp));
            matchesNode.put(_DESTINATION_PORT_RANGE, this.getDestinationPortRangeObjectNode(aceIp));
            matchesNode = this.getAceIpVersionObjectNode(aceIp, matchesNode);
            break;
        case ACE_ETH:
            AceEth aceEth = (AceEth) matches.getAceType();
            if (aceEth.getDestinationMacAddress() != null) {
                matchesNode.put(_DESTINATION_MAC_ADDRESS, aceEth.getDestinationMacAddress().getValue());
            }
            if (aceEth.getDestinationMacAddressMask() != null) {
                matchesNode.put(_DESTINATION_MAC_ADDRESS_MASK,
                        aceEth.getDestinationMacAddressMask().getValue());
            }
            if (aceEth.getSourceMacAddress() != null) {
                matchesNode.put(_SOURCE_MAC_ADDRESS, aceEth.getSourceMacAddress().getValue());
            }
            if (aceEth.getSourceMacAddressMask() != null) {
                matchesNode.put(_SOURCE_MAC_ADDRESS_MASK, aceEth.getSourceMacAddressMask().getValue());
            }
            break;
        }
    }

    Matches1 matches1 = matches.getAugmentation(Matches1.class);
    if (matches1 != null) {
        List<String> appIds = matches1.getApplicationId();

        if (appIds != null) {
            ArrayNode an = matchesNode.putArray(_ACE_APPLICATIONIDS);
            for (String appId : appIds) {
                an.add(appId);
            }
        }
    }

    return matchesNode;
}

From source file:io.github.microcks.util.postman.PostmanTestStepsRunner.java

private ArrayNode buildHeaders(Set<Header> headers) {
    ArrayNode jsonHS = mapper.createArrayNode();
    for (Header header : headers) {
        JsonNode jsonH = mapper.createObjectNode();
        ((ObjectNode) jsonH).put("key", header.getName());
        ((ObjectNode) jsonH).put("value", buildValue(header.getValues()));
        jsonHS.add(jsonH);
    }/*from   ww  w. j av a  2 s  .  com*/
    return jsonHS;
}

From source file:org.camunda.spin.impl.json.jackson.JacksonJsonNode.java

public SpinJsonNode append(Object property) {
    ensureNotNull("property", property);
    if (jsonNode.isArray()) {
        ArrayNode node = (ArrayNode) jsonNode;

        node.add(dataFormat.createJsonNode(property));

        return this;
    } else {/*from  ww  w .  ja va2s . c  o m*/
        throw LOG.unableToModifyNode(jsonNode.getNodeType().name());
    }
}

From source file:com.yahoo.elide.extensions.JsonApiPatch.java

private boolean processAction(ArrayNode errorList, boolean failed, PatchAction action) {
    if (action.cause != null) {
        // this is the failed operation
        errorList.add(toErrorNode(action.cause.getMessage(), action.cause.getStatus()));
        failed = true;/*from   w  ww .j a va 2s  . com*/
    } else if (!failed) {
        // this operation succeeded
        errorList.add(ERR_NODE_ERR_IN_SUBSEQUENT_OPERATION);
    } else {
        // this operation never ran
        errorList.add(ERR_NODE_OPERATION_NOT_RUN);
    }
    return failed;
}

From source file:com.attribyte.essem.DefaultResponseGenerator.java

protected void parseGraph(JsonNode sourceParent, List<String> fields, RateUnit rateUnit, ObjectNode targetMeta,
        ArrayNode targetGraph) {/*from  w ww . j  a va2  s  .  c  o m*/

    DateTimeFormatter parser = ISODateTimeFormat.basicDateTime();

    Map<MetricKey, ArrayNode> outputGraphs = Maps.newHashMapWithExpectedSize(4);

    JsonNode hitsObj = sourceParent.get("hits");
    if (hitsObj != null) {
        JsonNode hitsArr = hitsObj.get("hits");
        if (hitsArr != null) {

            for (JsonNode hitObj : hitsArr) {
                JsonNode fieldsObj = hitObj.get("fields");
                if (fieldsObj != null) {
                    MetricKey key = new MetricKey(getStringField(fieldsObj, "name"),
                            getStringField(fieldsObj, "application"), getStringField(fieldsObj, "host"),
                            getStringField(fieldsObj, "instance"));

                    ArrayNode samplesArr = outputGraphs.get(key);
                    if (samplesArr == null) {
                        ObjectNode graphObj = targetGraph.addObject();
                        addMeta(key, graphObj, targetMeta);
                        samplesArr = graphObj.putArray("samples");
                        outputGraphs.put(key, samplesArr);
                    }

                    ArrayNode sampleArr = samplesArr.addArray();

                    DateTime timestamp = parser.parseDateTime(getStringField(fieldsObj, "ts"));
                    sampleArr.add(timestamp.getMillis());
                    sampleArr.add(1); //Samples..

                    for (String field : fields) {
                        if (!graphIgnoreProperties.contains(field)) {
                            JsonNode fieldNode = getFieldNode(fieldsObj, field);
                            if (rateUnit == RAW_RATE_UNIT || fieldNode == null || !rateFields.contains(field)) {
                                if (fieldNode != null) {
                                    sampleArr.add(fieldNode);
                                } else {
                                    sampleArr.addNull();
                                }
                            } else {
                                sampleArr.add(fieldNode.doubleValue() * rateUnit.mult);
                            }
                        }
                    }
                }
            }
        }
    }
}

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

@Override
public String exportJson(DataObject dataObject) {
    String ret = null;//w ww. j  av  a2  s .co m

    if (dataObject instanceof ServiceFunctionSchedulerType) {
        ServiceFunctionSchedulerType sfst = (ServiceFunctionSchedulerType) dataObject;

        ArrayNode sfstArray = mapper.createArrayNode();
        ObjectNode sfstNode = mapper.createObjectNode();
        sfstNode.put(_NAME, sfst.getName());
        sfstNode.put(_ENABLED, sfst.isEnabled());
        if (sfst.getType() != null) {
            sfstNode.put(_TYPE,
                    SERVICE_FUNCTION_SCHEDULE_TYPE_PREFIX + sfst.getType().getSimpleName().toLowerCase());
        }

        sfstArray.add(sfstNode);
        try {
            Object sfstObject = mapper.treeToValue(sfstArray, Object.class);
            ret = mapper.writeValueAsString(sfstObject);
            ret = "{\"" + _SERVICE_FUNCTION_SCHEDULE_TYPE + "\":" + ret + "}";
            LOG.debug("Created Service Function Schedule Type JSON: {}", ret);
        } catch (JsonProcessingException e) {
            LOG.error("Error during creation of JSON for Service Function Schedule Type {}", sfst.getName());
        }
    } else {
        throw new IllegalArgumentException("Argument is not an instance of ServiceFunctionSchedulerType");
    }

    return ret;
}

From source file:com.rusticisoftware.tincan.ActivityDefinition.java

@Override
public ObjectNode toJSONNode(TCAPIVersion version) {
    ObjectMapper mapper = Mapper.getInstance();
    ObjectNode node = mapper.createObjectNode();
    if (this.name != null) {
        node.put("name", this.getName().toJSONNode(version));
    }//from   w w w  .  j  a  v a2s.  c o  m
    if (this.description != null) {
        node.put("description", this.getDescription().toJSONNode(version));
    }
    if (this.type != null) {
        node.put("type", this.getType().toString());
    }
    if (this.moreInfo != null) {
        node.put("moreInfo", this.getMoreInfo().toString());
    }
    if (this.extensions != null) {
        node.put("extensions", this.getExtensions().toJSONNode(version));
    }
    if (this.interactionType != null) {
        node.put("interactionType", this.getInteractionType().toString());

        switch (this.interactionType) {
        case CHOICE:
        case SEQUENCING:
            if (this.choices != null && this.choices.size() > 0) {
                ArrayNode choices = mapper.createArrayNode();
                node.put("choices", choices);

                for (InteractionComponent ic : this.getChoices()) {
                    choices.add(ic.toJSONNode(version));
                }
            }
            break;

        case LIKERT:
            if (this.scale != null && this.scale.size() > 0) {
                ArrayNode scale = mapper.createArrayNode();
                node.put("scale", scale);

                for (InteractionComponent ic : this.getScale()) {
                    scale.add(ic.toJSONNode(version));
                }
            }
            break;

        case MATCHING:
            if (this.source != null && this.source.size() > 0) {
                ArrayNode source = mapper.createArrayNode();
                node.put("source", source);

                for (InteractionComponent ic : this.getSource()) {
                    source.add(ic.toJSONNode(version));
                }
            }
            if (this.target != null && this.target.size() > 0) {
                ArrayNode target = mapper.createArrayNode();
                node.put("target", target);

                for (InteractionComponent ic : this.getTarget()) {
                    target.add(ic.toJSONNode(version));
                }
            }
            break;

        case PERFORMANCE:
            if (this.steps != null && this.steps.size() > 0) {
                ArrayNode steps = mapper.createArrayNode();
                node.put("steps", steps);

                for (InteractionComponent ic : this.getSteps()) {
                    steps.add(ic.toJSONNode(version));
                }
            }
            break;

        case TRUE_FALSE:
        case FILL_IN:
        case NUMERIC:
        case OTHER:
            break;
        }
    }
    if (this.correctResponsesPattern != null && this.correctResponsesPattern.size() > 0) {
        ArrayNode responses = mapper.createArrayNode();
        node.put("correctResponsesPattern", responses);

        for (String resp : this.getCorrectResponsesPattern()) {
            responses.add(resp);
        }
    }
    return node;
}