Java Code Examples for com.fasterxml.jackson.databind.node.ArrayNode#insert()

The following examples show how to use com.fasterxml.jackson.databind.node.ArrayNode#insert() . 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: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected JsonCache add(JsonPointer ptr, JsonNode node) {
    // If ptr ends with an array index, this implies inserting at the specified index.
    // If ptr does not end with an array index, this implies appending to the end of the array.
    // In both cases the array in question and its ancestors must be created if they do not already exist.
    String lastProperty = ptr.last().getMatchingProperty();
    boolean isIndexed = isInteger(lastProperty);
    ContainerNode<?> container = ensureContainerExists(ptr, !isIndexed);
    switch (container.getNodeType()) {
        case ARRAY:
            ArrayNode array = (ArrayNode) container;
            int index = isIndexed ? Integer.parseInt(lastProperty) : array.size();
            if (index < array.size()) {
                array.insert(index, node);
            } else {
                // Fill any gap between current size and index with nulls (Jackson doesn't support sparse arrays).
                for (int i = array.size(); i < index; i++)
                    array.add(array.nullNode());
                array.add(node);
            }
            break;
        default:
            throw new IllegalArgumentException(ptr + " does not identify an array");
    }
    setDirty();
    return this;
}
 
Example 2
Source File: JsonCacheImpl.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
protected void insertNumber(ArrayNode array, int index, Number value) {
    if (value instanceof Short) {
        array.insert(index, (Short) value);
    } else if (value instanceof Integer) {
        array.insert(index, (Integer) value);
    } else if (value instanceof Long) {
        array.insert(index, (Long) value);
    } else if (value instanceof Float) {
        array.insert(index, (Float) value);
    } else if (value instanceof Double) {
        array.insert(index, (Double) value);
    } else if (value instanceof BigInteger) {
        array.insert(index, (BigInteger) value);
    } else if (value instanceof BigDecimal) {
        array.insert(index, (BigDecimal) value);
    } else {
        throw new IllegalArgumentException(
                "unsupported numeric value: " + value + " (" + value.getClass().getSimpleName() + ')');
    }
}
 
Example 3
Source File: AddOperation.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
static JsonNode addToArray(final JsonPointer path, final JsonNode node, final JsonNode value) {
    final ArrayNode target = (ArrayNode) node.at(path.head());
    final String rawToken = path.last().getMatchingProperty();

    if (rawToken.equals(LAST_ARRAY_ELEMENT)) {
        target.add(value);
        return node;
    }

    final int size = target.size();
    final int index;
    try {
        index = Integer.parseInt(rawToken);
    } catch (NumberFormatException ignored) {
        throw new JsonPatchException("not an index: " + rawToken + " (expected: a non-negative integer)");
    }

    if (index < 0 || index > size) {
        throw new JsonPatchException("index out of bounds: " + index +
                                     " (expected: >= 0 && <= " + size + ')');
    }

    target.insert(index, value);
    return node;
}
 
Example 4
Source File: SolutionPerformanceHistoryComputer.java    From AILibs with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void processEvent(final IAlgorithmEvent e, final Map<String, Object> currentResults) {
	if (e instanceof IScoredSolutionCandidateFoundEvent) {
		@SuppressWarnings("rawtypes")
		double score = (double) ((IScoredSolutionCandidateFoundEvent) e).getScore();
		ArrayNode observation = new ObjectMapper().createArrayNode();
		observation.insert(0, System.currentTimeMillis() - this.start); // relative time
		observation.insert(1, MathExt.round(score, 5)); // score
		this.observations.add(observation);
		if (this.observations.size() % this.saveRate == 0) {
			currentResults.put("history", this.observations);
		}
	}
}
 
Example 5
Source File: JacksonJsonNode.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
public SpinJsonNode insertAt(int index, Object property) {
  ensureNotNull("property", property);

  if(jsonNode.isArray()) {
    index = getCorrectIndex(index);
    ArrayNode node = (ArrayNode) jsonNode;

    node.insert(index, dataFormat.createJsonNode(property));

    return this;
  } else {
    throw LOG.unableToModifyNode(jsonNode.getNodeType().name());
  }
}
 
Example 6
Source File: InPlaceApplyProcessor.java    From zjsonpatch with Apache License 2.0 5 votes vote down vote up
private void addToArray(JsonPointer path, JsonNode value, JsonNode parentNode) {
    final ArrayNode target = (ArrayNode) parentNode;
    int idx = path.last().getIndex();

    if (idx == JsonPointer.LAST_INDEX) {
        // see http://tools.ietf.org/html/rfc6902#section-4.1
        target.add(value);
    } else {
        if (idx > target.size())
            throw new JsonPatchApplicationException(
                    "Array index " + idx + " out of bounds", Operation.ADD, path.getParent());
        target.insert(idx, value);
    }
}