Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  
 import java.util.List;

Node class that represents Arrays mapped from Json content.
 
 public final class ArrayNode
     extends ContainerNode<ArrayNode>
 {
     private final List<JsonNode_children = new ArrayList<JsonNode>();
 
     public ArrayNode(JsonNodeFactory nc) { super(nc); }
 
     // note: co-variant to allow caller-side type safety
     @SuppressWarnings("unchecked")
     @Override
     public ArrayNode deepCopy()
     {
         ArrayNode ret = new ArrayNode();
 
         for (JsonNode element)
             ret._children.add(element.deepCopy());
 
         return ret;
     }
 
     /*
     /**********************************************************
     /* Implementation of core JsonNode API
     /**********************************************************
      */
 
     @Override
     public JsonNodeType getNodeType()
     {
         return .;
     }
 
     @Override public JsonToken asToken() { return .; }
 
     @Override
     public int size()
     {
         return .size();
     }
 
     @Override
     public Iterator<JsonNodeelements()
     {
         return .iterator();
     }
 
     @Override
     public JsonNode get(int index)
     {
         if (index >= 0 && index < .size()) {
             return .get(index);
         }
         return null;
     }
 
     @Override
     public JsonNode get(String fieldName) { return null; }
 
     @Override
     public JsonNode path(String fieldName) { return MissingNode.getInstance(); }
 
     @Override
     public JsonNode path(int index)
     {
         if (index >= 0 && index < .size()) {
             return .get(index);
         }
         return MissingNode.getInstance();
     }
     
     /*
     /**********************************************************
     /* Public API, serialization
     /**********************************************************
      */
 
     @Override
     public void serialize(JsonGenerator jgSerializerProvider provider)
         throws IOExceptionJsonProcessingException
     {
        jg.writeStartArray();
        for (JsonNode n : ) {
            /* 17-Feb-2009, tatu: Can we trust that all nodes will always
             *   extend BaseJsonNode? Or if not, at least implement
             *   JsonSerializable? Let's start with former, change if
             *   we must.
             */
            ((BaseJsonNode)n).serialize(jgprovider);
        }
        jg.writeEndArray();
    }
    @Override
    public void serializeWithType(JsonGenerator jgSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonProcessingException
    {
        typeSer.writeTypePrefixForArray(thisjg);
        for (JsonNode n : ) {
            ((BaseJsonNode)n).serialize(jgprovider);
        }
        typeSer.writeTypeSuffixForArray(thisjg);
    }
    
    /*
    /**********************************************************
    /* Public API, finding value nodes
    /**********************************************************
     */
    
    @Override
    public JsonNode findValue(String fieldName)
    {
        for (JsonNode node : ) {
            JsonNode value = node.findValue(fieldName);
            if (value != null) {
                return value;
            }
        }
        return null;
    }
    
    @Override
    public List<JsonNodefindValues(String fieldNameList<JsonNodefoundSoFar)
    {
        for (JsonNode node : ) {
            foundSoFar = node.findValues(fieldNamefoundSoFar);
        }
        return foundSoFar;
    }
    @Override
    public List<StringfindValuesAsText(String fieldNameList<StringfoundSoFar)
    {
        for (JsonNode node : ) {
            foundSoFar = node.findValuesAsText(fieldNamefoundSoFar);
        }
        return foundSoFar;
    }
    
    @Override
    public ObjectNode findParent(String fieldName)
    {
        for (JsonNode node : ) {
            JsonNode parent = node.findParent(fieldName);
            if (parent != null) {
                return (ObjectNodeparent;
            }
        }
        return null;
    }
    @Override
    public List<JsonNodefindParents(String fieldNameList<JsonNodefoundSoFar)
    {
        for (JsonNode node : ) {
            foundSoFar = node.findParents(fieldNamefoundSoFar);
        }
        return foundSoFar;
    }
    
    /*
    /**********************************************************
    /* Extended ObjectNode API, accessors
    /**********************************************************
     */

    
Method that will set specified field, replacing old value, if any.

Parameters:
value to set field to; if null, will be converted to a NullNode first (to remove field entry, call remove(int) instead)
Returns:
Old value of the field, if any; null if there was no old value.
    public JsonNode set(int indexJsonNode value)
    {
        if (value == null) { // let's not store 'raw' nulls but nodes
            value = nullNode();
        }
        if (index < 0 || index >= .size()) {
            throw new IndexOutOfBoundsException("Illegal index "index +", array size "+size());
        }
        return .set(indexvalue);
    }

    
Method for adding specified node at the end of this array.

Returns:
This node, to allow chaining
    public ArrayNode add(JsonNode value)
    {
        if (value == null) { // let's not store 'raw' nulls but nodes
            value = nullNode();
        }
        _add(value);
        return this;
    }

    
Method for adding all child nodes of given Array, appending to child nodes this array contains

Parameters:
other Array to add contents from
Returns:
This node (to allow chaining)
    public ArrayNode addAll(ArrayNode other)
    {
        .addAll(other._children);
        return this;
    }

    
Method for adding given nodes as child nodes of this array node.

Parameters:
nodes Nodes to add
Returns:
This node (to allow chaining)
    public ArrayNode addAll(Collection<JsonNodenodes)
    {
        .addAll(nodes);
        return this;
    }
    
    
Method for inserting specified child node as an element of this Array. If index is 0 or less, it will be inserted as the first element; if >= size(), appended at the end, and otherwise inserted before existing element in specified index. No exceptions are thrown for any index.

Returns:
This node (to allow chaining)
    public ArrayNode insert(int indexJsonNode value)
    {
        if (value == null) {
            value = nullNode();
        }
        _insert(indexvalue);
        return this;
    }

    
Method for removing an entry from this ArrayNode. Will return value of the entry at specified index, if entry existed; null if not.

Returns:
Node removed, if any; null if none
    public JsonNode remove(int index)
    {
        if (index >= 0 && index < .size()) {
            return .remove(index);
        }
        return null;
    }

    
Method for removing all elements of this array, leaving the array empty.

Returns:
This node (to allow chaining)
    @Override
    public ArrayNode removeAll()
    {
        .clear();
        return this;
    }
    
    /*
    /**********************************************************
    /* Extended ObjectNode API, mutators, generic; addXxx()/insertXxx()
    /**********************************************************
     */

    
Method that will construct an ArrayNode and add it as a field of this ObjectNode, replacing old value, if any.

Returns:
Newly constructed ArrayNode
    public ArrayNode addArray()
    {
        ArrayNode n  = arrayNode();
        _add(n);
        return n;
    }

    
Method that will construct an ObjectNode and add it at the end of this array node.

Returns:
Newly constructed ObjectNode
    public ObjectNode addObject()
    {
        ObjectNode n  = objectNode();
        _add(n);
        return n;
    }

    
Method that will construct a POJONode and add it at the end of this array node.

Returns:
This array node, to allow chaining
    public ArrayNode addPOJO(Object value)
    {
        if (value == null) {
            addNull();
        } else {
            _add(POJONode(value));
        }
        return this;
    }

    
Method that will add a null value at the end of this array node.

Returns:
This array node, to allow chaining
    public ArrayNode addNull()
    {
        _add(nullNode());
        return this;
    }

    
Method for adding specified number at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(int v) {
        _add(numberNode(v));
        return this;
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode add(Integer value) {
        if (value == null) {
            return addNull();
        }
        return _add(numberNode(value.intValue()));
    }
    
    
Method for adding specified number at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(long v) { return _add(numberNode(v)); }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode add(Long value) {
        if (value == null) {
            return addNull();
        }
        return _add(numberNode(value.longValue()));
    }
    
    
Method for adding specified number at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(float v) {
        return _add(numberNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode add(Float value) {
        if (value == null) {
            return addNull();
        }
        return _add(numberNode(value.floatValue()));
    }
    
    
Method for adding specified number at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(double v) {
        return _add(numberNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode add(Double value) {
        if (value == null) {
            return addNull();
        }
        return _add(numberNode(value.doubleValue()));
    }
    
    
Method for adding specified number at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(BigDecimal v) {
        if (v == null) {
            return addNull();
        }
        return _add(numberNode(v));
    }

    
Method for adding specified String value at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(String v) {
        if (v == null) {
            return addNull();
        }
        return _add(textNode(v));
    }

    
Method for adding specified boolean value at the end of this array.

Returns:
This array node, to allow chaining
    public ArrayNode add(boolean v) {
        return _add(booleanNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode add(Boolean value) {
        if (value == null) {
            return addNull();
        }
        return _add(booleanNode(value.booleanValue()));
    }
    
    
Method for adding specified binary value at the end of this array (note: when serializing as JSON, will be output Base64 encoded)

Returns:
This array node, to allow chaining
    public ArrayNode add(byte[] v) {
        if (v == null) {
            return addNull();
        }
        return _add(binaryNode(v));
    }

    
Method for creating an array node, inserting it at the specified point in the array, and returning the newly created array (note: NOT 'this' array)
    public ArrayNode insertArray(int index)
    {
        ArrayNode n  = arrayNode();
        _insert(indexn);
        return n;
    }

    
Method for creating an ObjectNode, appending it at the end of this array, and returning the newly created node (note: NOT 'this' array)

Returns:
Newly constructed ObjectNode
    public ObjectNode insertObject(int index)
    {
        ObjectNode n  = objectNode();
        _insert(indexn);
        return n;
    }

    
Method that will construct a POJONode and insert it at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insertPOJO(int indexObject value)
    {
        if (value == null) {
            return insertNull(index);
        }
        return _insert(indexPOJONode(value));
    }

    
Method that will insert a null value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insertNull(int index)
    {
        _insert(indexnullNode());
        return this;
    }

    
Method that will insert specified numeric value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexint v) {
        _insert(indexnumberNode(v));
        return this;
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexInteger value) {
        if (value == null) {
            insertNull(index);
        } else {
            _insert(indexnumberNode(value.intValue()));
        }
        return this;
    }
    
    
Method that will insert specified numeric value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexlong v) {
        return _insert(indexnumberNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexLong value) {
        if (value == null) {
            return insertNull(index);
        }
        return _insert(indexnumberNode(value.longValue()));
    }
    
    
Method that will insert specified numeric value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexfloat v) {
        return _insert(indexnumberNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexFloat value) {
        if (value == null) {
            return insertNull(index);
        }
        return _insert(indexnumberNode(value.floatValue()));
    }
    
    
Method that will insert specified numeric value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexdouble v) {
        return _insert(indexnumberNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexDouble value) {
        if (value == null) {
            return insertNull(index);
        }
        return _insert(indexnumberNode(value.doubleValue()));
    }

    
Method that will insert specified numeric value at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexBigDecimal v) {
        if (v == null) {
            return insertNull(index);
        }
        return _insert(indexnumberNode(v));
    }

    
Method that will insert specified String at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexString v) {
        if (v == null) {
            return insertNull(index);
        }
        return _insert(indextextNode(v));
    }

    
Method that will insert specified String at specified position in this array.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexboolean v) {
        return _insert(indexbooleanNode(v));
    }

    
Alternative method that we need to avoid bumping into NPE issues with auto-unboxing.

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexBoolean value) {
        if (value == null) {
            return insertNull(index);
        }
        return _insert(indexbooleanNode(value.booleanValue()));
    }
    
    
Method that will insert specified binary value at specified position in this array (note: when written as JSON, will be Base64 encoded)

Returns:
This array node, to allow chaining
    public ArrayNode insert(int indexbyte[] v) {
        if (v == null) {
            return insertNull(index);
        }
        return _insert(indexbinaryNode(v));
    }
    /*
    /**********************************************************
    /* Standard methods
    /**********************************************************
     */
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (getClass() != o.getClass()) {
            return false;
        }
        return .equals(((ArrayNodeo).);
    }
    @Override
    public int hashCode()
    {
        return .hashCode();
    }
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder(16 + (size() << 4));
        sb.append('[');
        for (int i = 0, len = .size(); i < len; ++i) {
            if (i > 0) {
                sb.append(',');
            }
            sb.append(.get(i).toString());
        }
        sb.append(']');
        return sb.toString();
    }
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    private ArrayNode _add(JsonNode node)
    {
        .add(node);
        return this;
    }
    private ArrayNode _insert(int indexJsonNode node)
    {
        if (index < 0) {
            .add(0, node);
        } else if (index >= .size()) {
            .add(node);
        } else {
            .add(indexnode);
        }
        return this;
    }
New to GrepCode? Check out our FAQ X