下面列出了com.fasterxml.jackson.databind.node.ContainerNode#getNodeType ( ) 实例代码,或者点击链接到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;
}
@Override
public JsonCache set(JsonPointer ptr, Object value) {
String property = ptr.last().getMatchingProperty();
ContainerNode<?> container = ensureContainerExists(ptr);
JsonNode node = nodeFor(value);
switch (container.getNodeType()) {
case ARRAY:
ArrayNode array = (ArrayNode) container;
int index = Integer.parseInt(property);
if (index < array.size()) {
array.set(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;
case OBJECT:
((ObjectNode) container).set(property, node);
break;
default:
throw new IllegalArgumentException(ptr + " does not identify a settable container");
}
setDirty();
return this;
}
protected void merge(ContainerNode<?> dest, ContainerNode<?> src) {
if (dest.getNodeType() == src.getNodeType()) {
if (dest.isArray()) {
ArrayNode destArray = (ArrayNode) dest;
ArrayNode srcArray = (ArrayNode) src;
outer:
for (int i = 0; i < srcArray.size(); i++) {
// Only add a source element if it is not already present in the destination array.
JsonNode srcElem = srcArray.get(i);
for (int j = 0; j < destArray.size(); j++) {
if (destArray.get(j).equals(srcElem))
continue outer;
}
destArray.add(srcElem);
}
} else if (dest.isObject()) {
ObjectNode destObject = (ObjectNode) dest;
ObjectNode srcObject = (ObjectNode) src;
Iterator<Entry<String, JsonNode>> fields = srcObject.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> field = fields.next();
String fieldName = field.getKey();
JsonNode srcChild = field.getValue();
if (destObject.has(fieldName)) {
JsonNode destChild = destObject.get(fieldName);
switch (mergePolicy) {
case OVERWRITE_EXISTING:
destObject.set(fieldName, srcChild);
// Mark the cache as dirty as we've added items from another file.
isDirty = true;
LOGGER.info("Existing root property '" + fieldName
+ "' has been overwritten by incoming data");
break;
case MERGE_RECURSIVE:
if (destChild.isContainerNode() && srcChild.isContainerNode())
merge((ContainerNode<?>) destChild, (ContainerNode<?>) srcChild);
break;
case KEEP_EXISTING:
LOGGER.info("Existing root property '" + fieldName
+ "' will not be overwritten by incoming data");
default:
// Nothing to do.
break;
}
} else {
destObject.set(fieldName, srcChild);
LOGGER.info("New property '" + fieldName + "' has been added from incoming data");
// Mark the cache as dirty as we've added items from another file.
isDirty = true;
}
}
}
} else {
LOGGER.warn("Cannot merge containers of differing types");
}
}