Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind;
  
  import java.io.Closeable;
  import java.util.*;
  
Iterator exposed by ObjectMapper when binding sequence of objects. Extension is done to allow more convenient exposing of java.io.IOException (which basic java.util.Iterator does not expose)
 
 public class MappingIterator<T> implements Iterator<T>, Closeable
 {
     protected final static MappingIterator<?> EMPTY_ITERATOR =
         new MappingIterator<Object>(nullnullnullnullfalsenull);
     
     protected final JavaType _type;
 
     protected final DeserializationContext _context;
     
     protected final JsonDeserializer<T> _deserializer;
 
     protected JsonParser _parser;
    
    
Flag that indicates whether input com.fasterxml.jackson.core.JsonParser should be closed when we are done or not; generally only called when caller did not pass JsonParser.
 
     protected final boolean _closeParser;

    
Flag that is set when we have determined what hasNextValue() should value; reset when nextValue() is called
 
     protected boolean _hasNextChecked;
    
    
If not null, "value to update" instead of creating a new instance for each call.
 
     protected final T _updatedValue;

    

Deprecated:
Since 2.1, to be removed
 
     @Deprecated
     protected MappingIterator(JavaType typeJsonParser jpDeserializationContext ctxt,
             JsonDeserializer<?> deser)
     {
         this(typejpctxtdesertruenull);
     }

    

Parameters:
managedParser Whether we "own" the com.fasterxml.jackson.core.JsonParser passed or not: if true, it was created by ObjectReader and code here needs to close it; if false, it was passed by calling code and should not be closed by iterator.
 
     @SuppressWarnings("unchecked")
     protected MappingIterator(JavaType typeJsonParser jpDeserializationContext ctxt,
             JsonDeserializer<?> deser,
             boolean managedParserObject valueToUpdate)
     {
          = type;
          = jp;
          = ctxt;
          = (JsonDeserializer<T>) deser;
          = managedParser;
         if (valueToUpdate == null) {
              = null;
         } else {
              = (T) valueToUpdate;
         }
 
         /* Ok: one more thing; we may have to skip START_ARRAY, assuming
          * "wrapped" sequence; but this is ONLY done for 'managed' parsers
          * and never if JsonParser was directly passed by caller (if it
          * was, caller must have either positioned it over first token of
          * the first element, or cleared the START_ARRAY token explicitly).
          * Note, however, that we do not try to guess whether this could be
          * an unwrapped sequence of arrays/Lists: we just assume it is wrapped;
          * and if not, caller needs to hand us JsonParser instead, pointing to
          * the first token of the first element.
          */
         if (managedParser && jp != null && jp.getCurrentToken() == .) {
             jp.clearCurrentToken();
         }
     }
 
     @SuppressWarnings("unchecked")
     protected static <T> MappingIterator<T> emptyIterator() {
         return (MappingIterator<T>) ;
     }
     
     /*
     /**********************************************************
    /* Basic iterator impl
    /**********************************************************
     */
//  @Override
    public boolean hasNext()
    {
        try {
            return hasNextValue();
        } catch (JsonMappingException e) {
            throw new RuntimeJsonMappingException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
//  @Override
    public T next()
    {
        try {
            return nextValue();
        } catch (JsonMappingException e) {
            throw new RuntimeJsonMappingException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
//  @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }
    
    public void close() throws IOException{
        if( != null) {
            .close();
        }
    }
    /*
    /**********************************************************
    /* Extended API
    /**********************************************************
     */

    
Equivalent of next() but one that may throw checked exceptions from Jackson due to invalid input.
    public boolean hasNextValue() throws IOException
    {
        if ( == null) {
            return false;
        }
        if (!) {
            JsonToken t = .getCurrentToken();
             = true;
            if (t == null) { // un-initialized or cleared; find next
                t = .nextToken();
                // If EOF, no more, or if we hit END_ARRAY (although we don't clear the token).
                if (t == null || t == .) {
                    JsonParser jp = ;
                     = null;
                    if () {
                        jp.close();
                    }
                    return false;
                }
            }
        }
        return true;
    }
    
    public T nextValue() throws IOException
    {
        // caller should always call 'hasNext[Value]' first; but let's ensure:
        if (!) {
            if (!hasNextValue()) {
                throw new NoSuchElementException();
            }
        }
        if ( == null) {
            throw new NoSuchElementException();
        }
         = false;
        T result;
        
        if ( == null) {
            result = .deserialize();
        } else{
            result = ;
        }
        // Need to consume the token too
        .clearCurrentToken();
        return result;
    }
New to GrepCode? Check out our FAQ X