Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.node;
  
  import java.util.*;
  
  
Helper class used by TreeTraversingParser to keep track of current location within traversed JSON tree.
 
 abstract class NodeCursor
     extends JsonStreamContext
 {
    
Parent cursor of this cursor, if any; null for root cursors.
 
     protected final NodeCursor _parent;

    
Current field name
 
     protected String _currentName;
     
     public NodeCursor(int contextTypeNodeCursor p)
     {
         super();
          = contextType;
          = -1;
          = p;
     }
 
     /*
     /**********************************************************
     /* JsonStreamContext impl
     /**********************************************************
      */
 
     // note: co-variant return type
     @Override
     public final NodeCursor getParent() { return ; }
 
     @Override
     public final String getCurrentName() {
         return ;
     }

    

Since:
2.0
 
     public void overrideCurrentName(String name) {
          = name;
     }
     
     /*
     /**********************************************************
     /* Extended API
     /**********************************************************
      */
 
     public abstract JsonToken nextToken();
     public abstract JsonToken nextValue();
     public abstract JsonToken endToken();
 
     public abstract JsonNode currentNode();
     public abstract boolean currentHasChildren();
    
    
Method called to create a new context for iterating all contents of the current structured value (JSON array or object)
 
     public final NodeCursor iterateChildren() {
         JsonNode n = currentNode();
         if (n == nullthrow new IllegalStateException("No current node");
         if (n.isArray()) { // false since we have already returned START_ARRAY
             return new Array(nthis);
         }
         if (n.isObject()) {
             return new Object(nthis);
         }
         throw new IllegalStateException("Current node of type "+n.getClass().getName());
     }
 
     /*
     /**********************************************************
     /* Concrete implementations
     /**********************************************************
      */

    
Context matching root-level value nodes (i.e. anything other than JSON Object and Array). Note that context is NOT created for leaf values.
 
     protected final static class RootValue
         extends NodeCursor
     {
        protected JsonNode _node;
        protected boolean _done = false;
        public RootValue(JsonNode nNodeCursor p) {
            super(.p);
             = n;
        }
        @Override
        public void overrideCurrentName(String name) {
            
        }
        
        @Override
        public JsonToken nextToken() {
            if (!) {
                 = true;
                return .asToken();
            }
             = null;
            return null;
        }
        
        @Override
        public JsonToken nextValue() { return nextToken(); }
        @Override
        public JsonToken endToken() { return null; }
        @Override
        public JsonNode currentNode() { return ; }
        @Override
        public boolean currentHasChildren() { return false; }
    }

    
Cursor used for traversing non-empty JSON Array nodes
    protected final static class Array
        extends NodeCursor
    {
        protected Iterator<JsonNode_contents;
        protected JsonNode _currentNode;
        public Array(JsonNode nNodeCursor p) {
            super(.p);
             = n.elements();
        }
        @Override
        public JsonToken nextToken()
        {
            if (!.hasNext()) {
                 = null;
                return null;
            }
             = .next();
            return .asToken();
        }
        @Override
        public JsonToken nextValue() { return nextToken(); }
        @Override
        public JsonToken endToken() { return .; }
        @Override
        public JsonNode currentNode() { return ; }
        @Override
        public boolean currentHasChildren() {
            // note: ONLY to be called for container nodes
            return ((ContainerNode<?>) currentNode()).size() > 0;
        }
    }

    
Cursor used for traversing non-empty JSON Object nodes
    protected final static class Object
        extends NodeCursor
    {
        protected Iterator<Map.Entry<StringJsonNode>> _contents;
        protected Map.Entry<StringJsonNode_current;
        protected boolean _needEntry;
        
        public Object(JsonNode nNodeCursor p)
        {
            super(.p);
             = ((ObjectNoden).fields();
             = true;
        }
        @Override
        public JsonToken nextToken()
        {
            // Need a new entry?
            if () {
                if (!.hasNext()) {
                     = null;
                     = null;
                    return null;
                }
                 = false;
                 = .next();
                 = ( == null) ? null : .getKey();
                return .;
            }
             = true;
            return .getValue().asToken();
        }
        @Override
        public JsonToken nextValue()
        {
            JsonToken t = nextToken();
            if (t == .) {
                t = nextToken();
            }
            return t;
        }
        @Override
        public JsonToken endToken() { return .; }
        @Override
        public JsonNode currentNode() {
            return ( == null) ? null : .getValue();
        }
        @Override
        public boolean currentHasChildren() {
            // note: ONLY to be called for container nodes
            return ((ContainerNode<?>) currentNode()).size() > 0;
        }
    }
New to GrepCode? Check out our FAQ X