下面列出了com.fasterxml.jackson.databind.node.ArrayNode#insert ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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() + ')');
}
}
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;
}
@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);
}
}
}
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());
}
}
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);
}
}