Java Code Examples for com.fasterxml.jackson.databind.JsonNode#deepCopy()

The following examples show how to use com.fasterxml.jackson.databind.JsonNode#deepCopy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyOperation.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
JsonNode apply(final JsonNode node) {
    JsonNode source = node.at(from);
    if (source.isMissingNode()) {
        throw new JsonPatchException("non-existent source path: " + from);
    }

    if (path.toString().isEmpty()) {
        return source;
    }

    final JsonNode targetParent = ensureTargetParent(node, path);
    source = source.deepCopy();
    return targetParent.isArray() ? AddOperation.addToArray(path, node, source)
                                  : AddOperation.addToObject(path, node, source);
}
 
Example 2
Source File: Jackson.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
public static JsonNode mergeTree(Iterable<JsonNode> jsonNodes) {
    requireNonNull(jsonNodes, "jsonNodes");
    final int size = Iterables.size(jsonNodes);
    checkArgument(size > 0, "jsonNodes is empty.");
    final Iterator<JsonNode> it = jsonNodes.iterator();
    final JsonNode first = it.next();
    JsonNode merged = first.deepCopy();

    final StringBuilder fieldNameAppender = new StringBuilder("/");
    while (it.hasNext()) {
        final JsonNode addition = it.next();
        merged = traverse(merged, addition, fieldNameAppender, true, true);
    }

    if (size > 2) {
        // Traverse once more to find the mismatched value between the first and the merged node.
        traverse(first, merged, fieldNameAppender, false, true);
    }
    return merged;
}
 
Example 3
Source File: VariantReferenceResolver.java    From commercetools-sync-java with Apache License 2.0 6 votes vote down vote up
@Nonnull
private CompletionStage<AttributeDraft> resolveAttributeReference(@Nonnull final AttributeDraft attributeDraft) {

    final JsonNode attributeDraftValue = attributeDraft.getValue();

    if (attributeDraftValue == null) {
        return CompletableFuture.completedFuture(attributeDraft);
    }

    final JsonNode attributeDraftValueClone = attributeDraftValue.deepCopy();

    final List<JsonNode> allAttributeReferences = attributeDraftValueClone.findParents(REFERENCE_TYPE_ID_FIELD);

    if (!allAttributeReferences.isEmpty()) {
        return mapValuesToFutureOfCompletedValues(allAttributeReferences, this::resolveReference, toList())
            .thenApply(ignoredResult -> AttributeDraft.of(attributeDraft.getName(), attributeDraftValueClone));
    }

    return CompletableFuture.completedFuture(attributeDraft);
}
 
Example 4
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
private String stringify(JsonNode items) {
    ObjectNode itemsClone =  (ObjectNode) items.deepCopy();
    String jsonString = items.get("json_blob").toString();
    itemsClone.remove("json_blob");
    itemsClone.put("json_blob", jsonString);
    return itemsClone.toString();
}
 
Example 5
Source File: JsonPatch.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
/**
 * Applies this patch to a JSON value.
 *
 * @param node the value to apply the patch to
 * @return the patched JSON value
 * @throws JsonPatchException failed to apply patch
 * @throws NullPointerException input is null
 */
public JsonNode apply(final JsonNode node) {
    requireNonNull(node, "node");
    JsonNode ret = node.deepCopy();
    for (final JsonPatchOperation operation : operations) {
        ret = operation.apply(ret);
    }

    return ret;
}
 
Example 6
Source File: SafeReplaceOperation.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@JsonCreator
SafeReplaceOperation(@JsonProperty("path") final JsonPointer path,
                     @JsonProperty("oldValue") JsonNode oldValue,
                     @JsonProperty("value") JsonNode newValue) {
    super("safeReplace", path);
    this.oldValue = oldValue.deepCopy();
    this.newValue = newValue.deepCopy();
}
 
Example 7
Source File: Nonce.java    From dxWDL with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a copy of an input object with an additional nonce field.
 *
 * @param input a JsonNode object
 *
 * @return a Copy of the given JsonNode containing a nonce.
 */
public static JsonNode updateNonce(JsonNode input) {
    ObjectNode inputJson = (ObjectNode)(input.deepCopy());
    if (!inputJson.has("nonce")) {
        inputJson.put("nonce", nonce());
    }
    return inputJson;
}
 
Example 8
Source File: DynamoDocumentStoreTemplate.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public JsonNode merge(final JsonNode newNode, final JsonNode oldNode) {
    final Set<String> allFieldNames = new HashSet<>();
    final Iterator<String> newNodeFieldNames = oldNode.fieldNames();
    while (newNodeFieldNames.hasNext()) {
        allFieldNames.add(newNodeFieldNames.next());
    }
    final Iterator<String> oldNodeFieldNames = oldNode.fieldNames();
    while (oldNodeFieldNames.hasNext()) {
        allFieldNames.add(oldNodeFieldNames.next());
    }
    final JsonNode mergedNode = newNode.deepCopy();
    for (final String fieldName : allFieldNames) {
        final JsonNode newNodeValue = newNode.get(fieldName);
        final JsonNode oldNodeValue = oldNode.get(fieldName);
        if (newNodeValue == null && oldNodeValue == null) {
            logger.trace("Skipping (both null): " + fieldName);
        } else if (newNodeValue == null && oldNodeValue != null) {
            logger.trace("Using old (new is null): " + fieldName);
            ((ObjectNode) mergedNode).set(fieldName, oldNodeValue);
        } else if (newNodeValue != null && oldNodeValue == null) {
            logger.trace("Using new (old is null): " + fieldName);
            ((ObjectNode) mergedNode).set(fieldName, newNodeValue);
        } else {
            logger.trace("Using new: " + fieldName);
            if (oldNodeValue.isObject()) {
                ((ObjectNode) mergedNode).set(fieldName, merge(newNodeValue, oldNodeValue));
            } else {
                ((ObjectNode) mergedNode).set(fieldName, newNodeValue);
            }
        }
    }
    return mergedNode;
}
 
Example 9
Source File: DeploymentDescriptorIT.java    From syndesis with Apache License 2.0 4 votes vote down vote up
private static ObjectNode propertyNodeForComparisson(final JsonNode propertyDefinition) {
    final ObjectNode propertyDefinitionForComparisson = propertyDefinition.deepCopy();
    propertyDefinitionForComparisson.remove(Arrays.asList("tags", "componentProperty"));
    return propertyDefinitionForComparisson;
}
 
Example 10
Source File: JaxRsEnumRule.java    From apicurio-studio with Apache License 2.0 4 votes vote down vote up
/**
 * Applies this schema rule to take the required code generation steps.
 * <p>
 * A Java {@link Enum} is created, with constants for each of the enum
 * values present in the schema. The enum name is derived from the nodeName,
 * and the enum type itself is created as an inner class of the owning type.
 * In the rare case that no owning type exists (the enum is the root of the
 * schema), then the enum becomes a public class in its own right.
 * <p>
 * The actual JSON value for each enum constant is held in a property called
 * "value" in the generated type. A static factory method
 * <code>fromValue(String)</code> is added to the generated enum, and the
 * methods are annotated to allow Jackson to marshal/unmarshal values
 * correctly.
 *
 * @param nodeName
 *            the name of the property which is an "enum"
 * @param node
 *            the enum node
 * @param container
 *            the class container (class or package) to which this enum
 *            should be added
 * @return the newly generated Java type that was created to represent the
 *         given enum
 * @see org.jsonschema2pojo.rules.Rule#apply(java.lang.String, com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.JsonNode, java.lang.Object, org.jsonschema2pojo.Schema)
 */
@Override
public JType apply(String nodeName, JsonNode node, JsonNode parent, JClassContainer container,
        Schema schema) {

    JDefinedClass _enum;
    try {
        _enum = createEnum(node, nodeName, container);
    } catch (ClassAlreadyExistsException e) {
        return e.getExistingClass();
    }

    schema.setJavaTypeIfEmpty(_enum);

    if (node.has("javaInterfaces")) {
        addInterfaces(_enum, node.get("javaInterfaces"));
    }
    
    // copy our node; remove the javaType as it will throw off the TypeRule for our case
    ObjectNode typeNode = (ObjectNode)node.deepCopy();
    typeNode.remove("javaType");

    // If type is specified on the enum, get a type rule for it.  Otherwise, we're a string.
    // (This is different from the default of Object, which is why we don't do this for every case.)
    JType backingType = node.has("type") ? 
            ruleFactory.getTypeRule().apply(nodeName, typeNode, parent, container, schema) :
            container.owner().ref(String.class);
    
    JFieldVar valueField = addValueField(_enum, backingType);
    
    // override toString only if we have a sensible string to return
    if(isString(backingType)){
        addToString(_enum, valueField);
    }
    
    addValueMethod(_enum, valueField);
    
    addEnumConstants(node.path("enum"), _enum, node.path("javaEnumNames"), backingType);
    addFactoryMethod(_enum, backingType);

    return _enum;
}
 
Example 11
Source File: InPlaceApplyProcessor.java    From zjsonpatch with Apache License 2.0 4 votes vote down vote up
@Override
public void copy(JsonPointer fromPath, JsonPointer toPath) throws JsonPointerEvaluationException {
    JsonNode valueNode = fromPath.evaluate(target);
    JsonNode valueToCopy = valueNode != null ? valueNode.deepCopy() : null;
    set(toPath, valueToCopy, Operation.COPY);
}
 
Example 12
Source File: CopyingApplyProcessor.java    From zjsonpatch with Apache License 2.0 4 votes vote down vote up
CopyingApplyProcessor(JsonNode target, EnumSet<CompatibilityFlags> flags) {
    super(target.deepCopy(), flags);
}
 
Example 13
Source File: PathValueOperation.java    From centraldogma with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param op operation name
 * @param path affected path
 * @param value JSON value
 */
PathValueOperation(final String op, final JsonPointer path, final JsonNode value) {
    super(op, path);
    this.value = value.deepCopy();
}
 
Example 14
Source File: DXEnvironment.java    From dxWDL with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the security context to use to authenticate to the Platform.
 *
 * @param json security context JSON
 *
 * @return the same Builder object
 *
 * @deprecated Use {@link #setBearerToken(String)} instead.
 */
@Deprecated
public Builder setSecurityContext(JsonNode json) {
    securityContext = json.deepCopy();
    return this;
}