下面列出了com.fasterxml.jackson.databind.node.ArrayNode#set ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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 setValue(ELContext context, ArrayNode node, Object property, Object value) {
if (readOnly) {
throw new PropertyNotWritableException("resolver is read-only");
}
int index = toIndex(property);
JsonNode jsonNode = createNode(node, value);
node.set(index, jsonNode);
context.setPropertyResolved(true);
}
private void processValueString() throws IOException {
Level child;
Level parent;
String value = null;
if(stack.isEmpty()) {
generator.writeString( parser.getText() );
return;
}
parent = stack.peek();
if( parent.isArray() ) {
ArrayNode array = (ArrayNode)parent.node;
array.add( parser.getText() );
if( bufferingLevel == null ) {
value = filterStreamValue( parent );
array.set( array.size()-1, new TextNode( value ) );
} else {
array.removeAll();
}
} else {
child = stack.pop();
parent = stack.peek();
((ObjectNode)parent.node ).put( child.field, parser.getText() );
if( bufferingLevel == null ) {
child.node = parent.node; // Populate the JsonNode of the child for filtering.
value = filterStreamValue( child );
}
}
if( bufferingLevel == null ) {
if( parent.node.isArray() ) {
((ArrayNode)parent.node).removeAll();
} else {
((ObjectNode)parent.node).removeAll();
}
generator.writeString( value );
}
}
private static void setArrayItem(ArrayNode list, int index, JsonNode valNode) {
if (index == -1) {
// append to end of array
list.add(valNode);
return;
}
// make sure items up to index exist
for (int i = list.size(); i < index+1; i++) {
list.add(NullNode.instance);
}
list.set(index, valNode);
}