Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  import java.util.*;
  
  
Node that maps to JSON Object structures in JSON content.
 
 public class ObjectNode
     extends ContainerNode<ObjectNode>
 {
     // note: until 2.1, was explicitly `LinkedHashMap`
     protected Map<StringJsonNode_children = null;
 
     public ObjectNode(JsonNodeFactory nc) { super(nc); }
 
     protected ObjectNode(JsonNodeFactory ncMap<StringJsonNodechildren) {
         super(nc);
          = children;
     }
     
     /* Question: should this delegate to `JsonNodeFactory`? It does not absolutely
      * have to, as long as sub-types override the method but...
      */
     // note: co-variant for type safety
     @SuppressWarnings("unchecked")
     @Override
     public ObjectNode deepCopy()
     {
         /* 28-Sep-2012, tatu: Sub-classes really should override this method to
          *   produce compliant copies.
          */
         if (getClass() != ObjectNode.class) {
             throw new IllegalStateException("ObjectNode subtype ("+getClass().getName()+" does not override deepCopy(), needs to");
         }
         return _defaultDeepCopy();
     }
 
    
Default implementation for 'deepCopy()': can be delegated to by sub-classes if necessary; but usually isn't.
 
     protected ObjectNode _defaultDeepCopy()
     {
         if ( == null) {
             return new ObjectNode();
         }
         final int len = .size();
         Map<StringJsonNodenewKids = _createMap(Math.max(4, len));
         for (Map.Entry<StringJsonNodeentry : .entrySet()) {
             newKids.put(entry.getKey(), entry.getValue().deepCopy());
         }
         return new ObjectNode(newKids);
     }
     
     /*
     /**********************************************************
     /* Implementation of core JsonNode API
     /**********************************************************
      */
 
     @Override public JsonToken asToken() { return .; }
 
     @Override
     public boolean isObject() { return true; }
 
     @Override
     public int size() {
         return ( == null) ? 0 : .size();
     }
 
     @Override
     public Iterator<JsonNodeelements()
     {
         return ( == null) ? NoNodesIterator.instance() : .values().iterator();
     }
 
     @Override
     public JsonNode get(int index) { return null; }
 
     @Override
     public JsonNode get(String fieldName)
     {
         if ( != null) {
             return .get(fieldName);
         }
         return null;
     }
 
     @Override
     public Iterator<StringfieldNames() {
         return ( == null) ? NoStringsIterator.instance() : .keySet().iterator();
    }
    @Override
    public JsonNode path(int index)
    {
        return MissingNode.getInstance();
    }
    @Override
    public JsonNode path(String fieldName)
    {
        if ( != null) {
            JsonNode n = .get(fieldName);
            if (n != null) {
                return n;
            }
        }
        return MissingNode.getInstance();
    }

    
Method to use for accessing all fields (with both names and values) of this JSON Object.
    @Override
    public Iterator<Map.Entry<StringJsonNode>> fields()
    {
        if ( == null) {
            return .;
        }
        return .entrySet().iterator();
    }
    @Override
    public ObjectNode with(String propertyName)
    {
        if ( == null) {
             = _createMap();
        } else {
            JsonNode n = .get(propertyName);
            if (n != null) {
                if (n instanceof ObjectNode) {
                    return (ObjectNoden;
                }
                throw new UnsupportedOperationException("Property '"+propertyName
                        +"' has value that is not of type ObjectNode (but "
                        +n.getClass().getName()+")");
            }
        }
        ObjectNode result = objectNode();
        .put(propertyNameresult);
        return result;
    }
    @Override
    public ArrayNode withArray(String propertyName)
    {
        if ( == null) {
             = _createMap();
        } else {
            JsonNode n = .get(propertyName);
            if (n != null) {
                if (n instanceof ArrayNode) {
                    return (ArrayNoden;
                }
                throw new UnsupportedOperationException("Property '"+propertyName
                        +"' has value that is not of type ArrayNode (but "
                        +n.getClass().getName()+")");
            }
        }
        ArrayNode result = arrayNode();
        .put(propertyNameresult);
        return result;
    }
    
    /*
    /**********************************************************
    /* Public API, finding value nodes
    /**********************************************************
     */
    
    @Override
    public JsonNode findValue(String fieldName)
    {
        if ( != null) {
            for (Map.Entry<StringJsonNodeentry : .entrySet()) {
                if (fieldName.equals(entry.getKey())) {
                    return entry.getValue();
                }
                JsonNode value = entry.getValue().findValue(fieldName);
                if (value != null) {
                    return value;
                }
            }
        }
        return null;
    }
    
    @Override
    public List<JsonNodefindValues(String fieldNameList<JsonNodefoundSoFar)
    {
        if ( != null) {
            for (Map.Entry<StringJsonNodeentry : .entrySet()) {
                if (fieldName.equals(entry.getKey())) {
                    if (foundSoFar == null) {
                        foundSoFar = new ArrayList<JsonNode>();
                    }
                    foundSoFar.add(entry.getValue());
                } else { // only add children if parent not added
                    foundSoFar = entry.getValue().findValues(fieldNamefoundSoFar);
                }
            }
        }
        return foundSoFar;
    }
    @Override
    public List<StringfindValuesAsText(String fieldNameList<StringfoundSoFar)
    {
        if ( != null) {
            for (Map.Entry<StringJsonNodeentry : .entrySet()) {
                if (fieldName.equals(entry.getKey())) {
                    if (foundSoFar == null) {
                        foundSoFar = new ArrayList<String>();
                    }
                    foundSoFar.add(entry.getValue().asText());
                } else { // only add children if parent not added
                    foundSoFar = entry.getValue().findValuesAsText(fieldNamefoundSoFar);
                }
            }
        }
        return foundSoFar;
    }
    
    @Override
    public ObjectNode findParent(String fieldName)
    {
        if ( != null) {
            for (Map.Entry<StringJsonNodeentry : .entrySet()) {
                if (fieldName.equals(entry.getKey())) {
                    return this;
                }
                JsonNode value = entry.getValue().findParent(fieldName);
                if (value != null) {
                    return (ObjectNodevalue;
                }
            }
        }
        return null;
    }
    @Override
    public List<JsonNodefindParents(String fieldNameList<JsonNodefoundSoFar)
    {
        if ( != null) {
            for (Map.Entry<StringJsonNodeentry : .entrySet()) {
                if (fieldName.equals(entry.getKey())) {
                    if (foundSoFar == null) {
                        foundSoFar = new ArrayList<JsonNode>();
                    }
                    foundSoFar.add(this);
                } else { // only add children if parent not added
                    foundSoFar = entry.getValue().findParents(fieldNamefoundSoFar);
                }
            }
        }
        return foundSoFar;
    }
    
    /*
    /**********************************************************
    /* Public API, serialization
    /**********************************************************
     */

    
Method that can be called to serialize this node and all of its descendants using specified JSON generator.
    @Override
    public final void serialize(JsonGenerator jgSerializerProvider provider)
        throws IOExceptionJsonProcessingException
    {
        jg.writeStartObject();
        if ( != null) {
            for (Map.Entry<StringJsonNodeen : .entrySet()) {
                jg.writeFieldName(en.getKey());
                /* 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.
                 */
                ((BaseJsonNodeen.getValue()).serialize(jgprovider);
            }
        }
        jg.writeEndObject();
    }
    @Override
    public void serializeWithType(JsonGenerator jgSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonProcessingException
    {
        typeSer.writeTypePrefixForObject(thisjg);
        if ( != null) {
            for (Map.Entry<StringJsonNodeen : .entrySet()) {
                jg.writeFieldName(en.getKey());
                ((BaseJsonNodeen.getValue()).serialize(jgprovider);
            }
        }
        typeSer.writeTypeSuffixForObject(thisjg);
    }
    /*
    /**********************************************************
    /* Extended ObjectNode API, mutators, since 2.1
    /**********************************************************
     */

    
Method that will set specified field, replacing old value, if any. Note that this is identical to replace(java.lang.String,com.fasterxml.jackson.databind.JsonNode), except for return value.

NOTE: added to replace those uses of put(java.lang.String,com.fasterxml.jackson.databind.JsonNode) where chaining with 'this' is desired.

Parameters:
value to set field to; if null, will be converted to a NullNode first (to remove field entry, call remove(java.lang.String) instead)
Returns:
This node after adding/replacing property value (to allow chaining)
Since:
2.1
    public JsonNode set(String fieldNameJsonNode value)
    {
        if (value == null) {
            value = nullNode();
        }
        _put(fieldNamevalue);
        return this;
    }

    
Method for adding given properties to this object node, overriding any existing values for those properties.

Parameters:
properties Properties to add
Returns:
This node after adding/replacing property values (to allow chaining)
Since:
2.1
    public JsonNode setAll(Map<String,JsonNodeproperties)
    {
        if ( == null) {
             = _createMap();
        } else {
            for (Map.Entry<StringJsonNodeen : properties.entrySet()) {
                JsonNode n = en.getValue();
                if (n == null) {
                    n = nullNode();
                }
                .put(en.getKey(), n);
            }
        }
        return this;
    }

    
Method for adding all properties of the given Object, overriding any existing values for those properties.

Parameters:
other Object of which properties to add to this object
Returns:
This node after addition (to allow chaining)
Since:
2.1
    public JsonNode setAll(ObjectNode other)
    {
        int len = other.size();
        if (len > 0) {
            if ( == null) {
                 = _createMap(len);
            }
            other.putContentsTo();
        }
        return this;
    }
    
    
Method for replacing value of specific property with passed value, and returning value (or null if none).

Parameters:
fieldName Property of which value to replace
value Value to set property to, replacing old value if any
Returns:
Old value of the property; null if there was no such property with value
Since:
2.1
    public JsonNode replace(String fieldNameJsonNode value)
    {
        if (value == null) { // let's not store 'raw' nulls but nodes
            value = nullNode();
        }
        return _put(fieldNamevalue);
    }

    
Method for removing field entry from this ObjectNode, and returning instance after removal.

Returns:
This node after removing entry (if any)
Since:
2.1
    public JsonNode without(String fieldName)
    {
        if ( != null) {
            .remove(fieldName);
        }
        return this;
    }

    
Method for removing specified field properties out of this ObjectNode.

Parameters:
fieldNames Names of fields to remove
Returns:
This node after removing entries
Since:
2.1
    public ObjectNode without(Collection<StringfieldNames)
    {
        if ( != null) {
            for (String fieldName : fieldNames) {
                .remove(fieldName);
            }
        }
        return this;
    }
    
    /*
    /**********************************************************
    /* Extended ObjectNode API, mutators, generic
    /**********************************************************
     */
    
    
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(java.lang.String) instead)

NOTE: this method will be deprecated in 2.2; and should be replace with either set(java.lang.String,com.fasterxml.jackson.databind.JsonNode) or replace(java.lang.String,com.fasterxml.jackson.databind.JsonNode), depending on which return value is desired for possible chaining.

Returns:
Old value of the field, if any; null if there was no old value.
    public JsonNode put(String fieldNameJsonNode value)
    {
        if (value == null) { // let's not store 'raw' nulls but nodes
            value = nullNode();
        }
        return _put(fieldNamevalue);
    }
    
    
Method for removing field entry from this ObjectNode. Will return value of the field, if such field existed; null if not.

Returns:
Value of specified field, if it existed; null if not
    public JsonNode remove(String fieldName)
    {
        if ( != null) {
            return .remove(fieldName);
        }
        return null;
    }

    
Method for removing specified field properties out of this ObjectNode.

Parameters:
fieldNames Names of fields to remove
Returns:
This node after removing entries
    public ObjectNode remove(Collection<StringfieldNames)
    {
        if ( != null) {
            for (String fieldName : fieldNames) {
                .remove(fieldName);
            }
        }
        return this;
    }
    
    
Method for removing all field properties, such that this ObjectNode will contain no properties after call.

Returns:
This node after removing all entries
    @Override
    public ObjectNode removeAll()
    {
         = null;
        return this;
    }

    
Method for adding given properties to this object node, overriding any existing values for those properties.

NOTE: this method will be deprecated in 2.2; and should be replace with setAll(java.util.Map).

Parameters:
properties Properties to add
Returns:
This node after adding/replacing property values (to allow chaining)
    public JsonNode putAll(Map<String,JsonNodeproperties) {
        return setAll(properties);
    }

    
Method for adding all properties of the given Object, overriding any existing values for those properties.

NOTE: this method will be deprecated in 2.2; and should be replace with setAll(com.fasterxml.jackson.databind.node.ObjectNode).

Parameters:
other Object of which properties to add to this object
Returns:
This node (to allow chaining)
    public JsonNode putAll(ObjectNode other) {
        return setAll(other);
    }

    
Method for removing all field properties out of this ObjectNode except for ones specified in argument.

Parameters:
fieldNames Fields to retain in this ObjectNode
Returns:
This node (to allow call chaining)
    public ObjectNode retain(Collection<StringfieldNames)
    {
        if ( != null) {
            Iterator<Map.Entry<String,JsonNode>> entries = .entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<StringJsonNodeentry = entries.next();
                if (!fieldNames.contains(entry.getKey())) {
                    entries.remove();
                }
            }
        }
        return this;
    }

    
Method for removing all field properties out of this ObjectNode except for ones specified in argument.

Parameters:
fieldNames Fields to retain in this ObjectNode
Returns:
This node (to allow call chaining)
    public ObjectNode retain(String... fieldNames) {
        return retain(Arrays.asList(fieldNames));
    }
    
    /*
    /**********************************************************
    /* Extended ObjectNode API, mutators, typed
    /**********************************************************
     */

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

NOTE: Unlike all put(...) methods, return value is NOT this ObjectNode, but the newly created ArrayNode instance.

Returns:
Newly constructed ArrayNode (NOT the old value, which could be of any type)
    public ArrayNode putArray(String fieldName)
    {
        ArrayNode n  = arrayNode();
        _put(fieldNamen);
        return n;
    }

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

NOTE: Unlike all put(...) methods, return value is NOT this ObjectNode, but the newly created ObjectNode instance.

Returns:
Newly constructed ObjectNode (NOT the old value, which could be of any type)
    public ObjectNode putObject(String fieldName)
    {
        ObjectNode n  = objectNode();
        _put(fieldNamen);
        return n;
    }

    

Returns:
This node (to allow chaining)
    public ObjectNode putPOJO(String fieldNameObject pojo) {
        _put(fieldNamePOJONode(pojo));
        return this;
    }

    

Returns:
This node (to allow chaining)
    public ObjectNode putNull(String fieldName)
    {
        _put(fieldNamenullNode());
        return this;
    }

    
Method for setting value of a field to specified numeric value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameint v) {
        _put(fieldNamenumberNode(v));
        return this;
    }

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

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameInteger value) {
        if (value == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamenumberNode(value.intValue()));
        }
        return this;
    }
    
    
Method for setting value of a field to specified numeric value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNamelong v) {
        _put(fieldNamenumberNode(v));
        return this;
    }

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

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameLong value) {
        if (value == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamenumberNode(value.longValue()));
        }
        return this;
    }
    
    
Method for setting value of a field to specified numeric value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNamefloat v) {
        _put(fieldNamenumberNode(v));
        return this;
    }

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

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameFloat value) {
        if (value == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamenumberNode(value.floatValue()));
        }
        return this;
    }
    
    
Method for setting value of a field to specified numeric value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNamedouble v) {
        _put(fieldNamenumberNode(v));
        return this;
    }

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

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameDouble value) {
        if (value == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamenumberNode(value.doubleValue()));
        }
        return this;
    }
    
    
Method for setting value of a field to specified numeric value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameBigDecimal v) {
        if (v == null) {
            putNull(fieldName);
        } else {
            _put(fieldNamenumberNode(v));
        }
        return this;
    }

    
Method for setting value of a field to specified String value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameString v) {
        if (v == null) {
            putNull(fieldName);
        } else {
            _put(fieldNametextNode(v));
        }
        return this;
    }

    
Method for setting value of a field to specified String value.

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameboolean v) {
        _put(fieldNamebooleanNode(v));
        return this;
    }

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

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNameBoolean value) {
        if (value == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamebooleanNode(value.booleanValue()));
        }
        return this;
    }
    
    
Method for setting value of a field to specified binary value

Returns:
This node (to allow chaining)
    public ObjectNode put(String fieldNamebyte[] v) {
        if (v == null) {
            _put(fieldNamenullNode());
        } else {
            _put(fieldNamebinaryNode(v));
        }
        return this;
    }
    /*
    /**********************************************************
    /* Overridable methods
    /**********************************************************
     */

    
Internal factory method for creating java.util.Map used for storing child nodes. Overridable by sub-classes, used when caller does not know what optimal size would, used for example when constructing a Map when adding the first one.

Since:
2.1
    protected Map<StringJsonNode_createMap() {
        return new LinkedHashMap<StringJsonNode>();
    }
    
    
Internal factory method for creating java.util.Map used for storing child nodes. Overridable by sub-classes, used when caller has an idea of what optimal size should be: used when copying contents of an existing node.

Since:
2.1
    protected Map<StringJsonNode_createMap(int defaultSize) {
        return new LinkedHashMap<StringJsonNode>(defaultSize);
    }
    
    /*
    /**********************************************************
    /* Package methods (for other node classes to use)
    /**********************************************************
     */
    protected void putContentsTo(Map<String,JsonNodedst)
    {
        if ( != null) {
            for (Map.Entry<String,JsonNodeen : .entrySet()) {
                dst.put(en.getKey(), en.getValue());
            }
        }
    }
    /*
    /**********************************************************
    /* Standard methods
    /**********************************************************
     */
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) {
            return false;
        }
        ObjectNode other = (ObjectNodeo;
        if (other.size() != size()) {
            return false;
        }
        if ( != null) {
            for (Map.Entry<StringJsonNodeen : .entrySet()) {
                String key = en.getKey();
                JsonNode value = en.getValue();
                JsonNode otherValue = other.get(key);
                if (otherValue == null || !otherValue.equals(value)) {
                    return false;
                }
            }
        }
        return true;
    }
    @Override
    public int hashCode()
    {
        return ( == null) ? -1 : .hashCode();
    }
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder(32 + (size() << 4));
        sb.append("{");
        if ( != null) {
            int count = 0;
            for (Map.Entry<StringJsonNodeen : .entrySet()) {
                if (count > 0) {
                    sb.append(",");
                }
                ++count;
                TextNode.appendQuoted(sben.getKey());
                sb.append(':');
                sb.append(en.getValue().toString());
            }
        }
        sb.append("}");
        return sb.toString();
    }
    /*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
    private final JsonNode _put(String fieldNameJsonNode value)
    {
        if ( == null) {
             = _createMap();
        }
        return .put(fieldNamevalue);
    }
    /*
    /**********************************************************
    /* Helper classes
    /**********************************************************
     */

    
For efficiency, let's share the "no fields" iterator...
    protected static class NoFieldsIterator
        implements Iterator<Map.Entry<StringJsonNode>>
    {
        final static NoFieldsIterator instance = new NoFieldsIterator();
        private NoFieldsIterator() { }
//      @Override
        public boolean hasNext() { return false; }
//      @Override
        public Map.Entry<String,JsonNodenext() { throw new NoSuchElementException(); }
//      @Override
        public void remove() { // or IllegalOperationException?
            throw new IllegalStateException();
        }
    }
New to GrepCode? Check out our FAQ X