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

The following examples show how to use com.fasterxml.jackson.databind.node.ObjectNode#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: AdditionalPropertiesDeserializer.java    From botbuilder-java with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectNode root = mapper.readTree(jp);
    ObjectNode copy = root.deepCopy();

    // compare top level fields and keep only missing fields
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            JsonProperty property = field.getAnnotation(JsonProperty.class);
            String key = property.value().split("((?<!\\\\))\\.")[0];
            if (!key.isEmpty() && copy.has(key)) {
                copy.remove(key);
            }
        }
    }

    // put into additional properties
    root.put("additionalProperties", copy);

    JsonParser parser = new JsonFactory().createParser(root.toString());
    parser.nextToken();
    return defaultDeserializer.deserialize(parser, ctxt);
}
 
Example 2
Source File: ColumnToRowExpander.java    From samantha with MIT License 6 votes vote down vote up
public List<ObjectNode> expand(List<ObjectNode> initialResult,
                               RequestContext requestContext) {
    List<ObjectNode> expanded = new ArrayList<>();
    for (ObjectNode entity : initialResult) {
        List<ObjectNode> oneExpanded = new ArrayList<>();
        for (String colName : colNames) {
            if (entity.has(colName)) {
                ObjectNode newEntity = entity.deepCopy();
                newEntity.put(nameAttr, colName);
                newEntity.set(valueAttr, entity.get(colName));
                oneExpanded.add(newEntity);
            } else {
                logger.warn("The column {} is not present: {}", colName, entity.toString());
            }
        }
        if (oneExpanded.size() > 0) {
            expanded.addAll(oneExpanded);
        } else {
            expanded.add(entity);
        }
    }
    return expanded;
}
 
Example 3
Source File: JsonStringOrNodeExpander.java    From samantha with MIT License 6 votes vote down vote up
public List<ObjectNode> expand(List<ObjectNode> initialResult,
                               RequestContext requestContext) {
    List<ObjectNode> expanded = new ArrayList<>();
    for (ObjectNode entity : initialResult) {
        JsonNode json = entity.get(jsonAttr);
        if (json.isTextual()) {
            json = Json.parse(json.asText());
        }
        if (json.isArray()) {
            for (JsonNode one : json) {
                ObjectNode newEntity = entity.deepCopy();
                IOUtilities.parseEntityFromJsonNode(one, newEntity);
                expanded.add(newEntity);
            }
        } else {
            IOUtilities.parseEntityFromJsonNode(json, entity);
            expanded.add(entity);
        }
    }
    return expanded;
}
 
Example 4
Source File: PropertyUtil.java    From streams with Apache License 2.0 6 votes vote down vote up
/**
 * merge parent and child properties maps.
 * @param content ObjectNode
 * @param parent ObjectNode
 * @return merged ObjectNode
 */
public static ObjectNode mergeProperties(ObjectNode content, ObjectNode parent) {

  ObjectNode merged = parent.deepCopy();
  Iterator<Map.Entry<String, JsonNode>> fields = content.fields();
  for ( ; fields.hasNext(); ) {
    Map.Entry<String, JsonNode> field = fields.next();
    String fieldId = field.getKey();
    if( merged.get(fieldId) != null ) {
      if( merged.get(fieldId).getNodeType().equals(JsonNodeType.OBJECT)) {
        merged.put(fieldId, mergeProperties(field.getValue().deepCopy(), (ObjectNode)merged.get(fieldId)));
      } else if ( merged.get(fieldId).getNodeType().equals(JsonNodeType.ARRAY)) {
        merged.put(fieldId, mergeArrays(((ArrayNode)field.getValue()), ((ArrayNode)merged.get(fieldId))));
      } else {
        merged.put(fieldId, content.get(fieldId));
      }
    } else {
      merged.put(fieldId, content.get(fieldId));
    }
  }
  return merged;
}
 
Example 5
Source File: PostProcessBenchmarkResults.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
private ObjectNode subtractBenchmarkResults(ObjectNode benchmark, ObjectNode benchmarkBaseline) {
    final ObjectNode result = benchmark.deepCopy();
    result.put("benchmark", result.get("benchmark").textValue() + ".delta");
    subtract((ObjectNode) result.get("primaryMetric"), (ObjectNode) benchmarkBaseline.get("primaryMetric"));
    subtract((ObjectNode) result.get("secondaryMetrics"), (ObjectNode) benchmarkBaseline.get("secondaryMetrics"));
    return result;
}
 
Example 6
Source File: AdditionalPropertiesDeserializer.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    ObjectNode root = mapper.readTree(jp);
    ObjectNode copy = root.deepCopy();

    // compare top level fields and keep only missing fields
    final Class<?> tClass = this.defaultDeserializer.handledType();
    for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            JsonProperty property = field.getAnnotation(JsonProperty.class);
            if (property != null) {
                String key = property.value().split("((?<!\\\\))\\.")[0];
                if (!key.isEmpty()) {
                    if (copy.has(key)) {
                        copy.remove(key);
                    }
                }
            }
        }
    }

    // put into additional properties
    root.put("additionalProperties", copy);

    JsonParser parser = new JsonFactory().createParser(root.toString());
    parser.nextToken();
    return defaultDeserializer.deserialize(parser, ctxt);
}
 
Example 7
Source File: ApiTest.java    From zjsonpatch with Apache License 2.0 5 votes vote down vote up
@Test
public void applyInPlaceMutatesSource() throws Exception {
    JsonNode patch = readTree("[{ \"op\": \"add\", \"path\": \"/b\", \"value\": \"b-value\" }]");
    ObjectNode source = newObjectNode();
    ObjectNode beforeApplication = source.deepCopy();
    JsonPatch.apply(patch, source);
    assertThat(source, is(beforeApplication));
}
 
Example 8
Source File: ApiTest.java    From zjsonpatch with Apache License 2.0 5 votes vote down vote up
@Test
public void applyDoesNotMutateSource2() throws Exception {
    JsonNode patch = readTree("[{ \"op\": \"add\", \"path\": \"/b\", \"value\": \"b-value\" }]");
    ObjectNode source = newObjectNode();
    ObjectNode beforeApplication = source.deepCopy();
    JsonPatch.apply(patch, source);
    assertThat(source, is(beforeApplication));
}
 
Example 9
Source File: SequenceToStepExpander.java    From samantha with MIT License 4 votes vote down vote up
public List<ObjectNode> expand(List<ObjectNode> initialResult,
                               RequestContext requestContext) {
    List<ObjectNode> expanded = new ArrayList<>();
    for (ObjectNode entity : initialResult) {
        String attrStr = entity.get(nameAttrs.get(0)).asText();
        if ("".equals(attrStr)) {
            continue;
        }
        List<String[]> values = new ArrayList<>();
        int size = 0;
        for (String nameAttr : nameAttrs) {
            String[] splitted = entity.get(nameAttr).asText().split(separator, -1);
            size = splitted.length;
            values.add(splitted);
        }
        int start = 0;
        int end = size;
        if (tstampAttr != null) {
            String[] fstamp = entity.get(tstampAttr).asText().split(separator, -1);
            int i;
            for (i=0; i<size; i++) {
                String tstamp = fstamp[i];
                int istamp = Integer.parseInt(tstamp);
                if (istamp >= splitTstamp) {
                    break;
                }
            }
            if (backward) {
                end = i;
            } else {
                start = i;
            }
            size = end - start;
        }
        if (maxStepNum != null && maxStepNum < size) {
            if (backward) {
                start = end - maxStepNum;
            } else {
                end = start + maxStepNum;
            }
        }
        for (int i=start; i<end; i++) {
            ObjectNode newEntity;
            if (otherAttrs != null) {
                newEntity = Json.newObject();
                for (String attr : otherAttrs) {
                    newEntity.set(attr, entity.get(attr));
                }
            } else {
                newEntity = entity.deepCopy();
            }
            for (int j=0; j<values.size(); j++) {
                newEntity.put(valueAttrs.get(j), values.get(j)[i]);
                if (historyAttrs != null && historyAttrs.size() > j) {
                    newEntity.put(historyAttrs.get(j), StringUtils.join(
                            ArrayUtils.subarray(values.get(j), 0, i), joiner));
                }
            }
            expanded.add(newEntity);
        }
    }
    return expanded;
}
 
Example 10
Source File: Display2ActionExpander.java    From samantha with MIT License 4 votes vote down vote up
public List<ObjectNode> expand(List<ObjectNode> initialResult,
                               RequestContext requestContext) {
    List<ObjectNode> expanded = new ArrayList<>();
    for (ObjectNode entity : initialResult) {
        List<String[]> values = new ArrayList<>();
        List<List<String>> valueStrs = new ArrayList<>();
        for (String nameAttr : nameAttrs) {
            values.add(entity.get(nameAttr).asText().split(separator, -1));
            valueStrs.add(new ArrayList<>());
        }
        String[] actions = entity.get(actionName).asText().split(separator, -1);
        int end = actions.length;
        if (tstampAttr != null) {
            String tstampStr = entity.get(tstampAttr).asText();
            if (!"".equals(tstampStr)) {
                String[] fstamp = tstampStr.split(separator, -1);
                int i;
                for (i=0; i<fstamp.length; i++) {
                    String tstamp = fstamp[i];
                    int istamp = Integer.parseInt(tstamp);
                    if (istamp >= splitTstamp) {
                        break;
                    }
                }
                String[] indices = entity.get(inGrpRank).asText().split(separator, -1);
                end = i + FeatureExtractorUtilities.getForwardEnd(
                        ArrayUtils.subarray(indices, i, end), maxGrpNum, grpSize);
            }
        }
        boolean include = false;
        for (int i=0; i<end; i++) {
            String act = actions[i];
            if (!"".equals(act) && Double.parseDouble(act) > 0.0) {
                for (int j=0; j<valueStrs.size(); j++) {
                    valueStrs.get(j).add(values.get(j)[i]);
                }
                include = true;
            }
        }
        if (include || alwaysInclude) {
            ObjectNode newEntity = entity.deepCopy();
            for (int j=0; j<valueAttrs.size(); j++) {
                newEntity.put(valueAttrs.get(j), StringUtils.join(valueStrs.get(j), joiner));
            }
            expanded.add(newEntity);
        }
    }
    return expanded;
}
 
Example 11
Source File: ConfigElement.java    From digdag with Apache License 2.0 4 votes vote down vote up
@JsonCreator
public static ConfigElement of(ObjectNode node)
{
    return new ConfigElement(node.deepCopy());
}
 
Example 12
Source File: ConfigElement.java    From digdag with Apache License 2.0 4 votes vote down vote up
private ConfigElement(ObjectNode node)
{
    this.object = node.deepCopy();
}