Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.std;
  
  import java.util.Iterator;
  
Fallback serializer for cases where Collection is not known to be of type for which more specializer serializer exists (such as index-accessible List). If so, we will just construct an java.util.Iterator to iterate over elements.
 
 public class CollectionSerializer
     extends AsArraySerializerBase<Collection<?>>
 {
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     public CollectionSerializer(JavaType elemTypeboolean staticTypingTypeSerializer vts,
             BeanProperty propertyJsonSerializer<ObjectvalueSerializer)
     {
         super(Collection.classelemTypestaticTypingvtspropertyvalueSerializer);
     }
 
             BeanProperty propertyTypeSerializer vtsJsonSerializer<?> valueSerializer)
     {
         super(srcpropertyvtsvalueSerializer);
     }
     
     @Override
     }
 
     @Override
     public CollectionSerializer withResolved(BeanProperty property,
             TypeSerializer vtsJsonSerializer<?> elementSerializer) {
         return new CollectionSerializer(thispropertyvtselementSerializer);
     }
 
     /*
     /**********************************************************
     /* Accessors
     /**********************************************************
      */
     
     @Override
     public boolean isEmpty(Collection<?> value) {
         return (value == null) || value.isEmpty();
     }
 
     @Override
     public boolean hasSingleElement(Collection<?> value) {
         Iterator<?> it = value.iterator();
         if (!it.hasNext()) {
             return false;
         }
         it.next();
         return !it.hasNext();
     }
     
     /*
     /**********************************************************
     /* Actual serialization
     /**********************************************************
      */
     
     @Override
     public void serializeContents(Collection<?> valueJsonGenerator jgenSerializerProvider provider)
         throws IOExceptionJsonGenerationException
     {
         if ( != null) {
             serializeContentsUsing(valuejgenprovider);
             return;
         }
         Iterator<?> it = value.iterator();
         if (!it.hasNext()) {
             return;
         }
         PropertySerializerMap serializers = ;
         final TypeSerializer typeSer = ;
 
         int i = 0;
         try {
            do {
                Object elem = it.next();
                if (elem == null) {
                    provider.defaultSerializeNull(jgen);
                } else {
                    Class<?> cc = elem.getClass();
                    JsonSerializer<Objectserializer = serializers.serializerFor(cc);
                    if (serializer == null) {
                        // To fix [JACKSON-508]
                        if (.hasGenericTypes()) {
                            serializer = _findAndAddDynamic(serializers,
                                    provider.constructSpecializedType(cc), provider);
                        } else {
                            serializer = _findAndAddDynamic(serializersccprovider);
                        }
                        serializers = ;
                    }
                    if (typeSer == null) {
                        serializer.serialize(elemjgenprovider);
                    } else {
                        serializer.serializeWithType(elemjgenprovidertypeSer);
                    }
                }
                ++i;
            } while (it.hasNext());
        } catch (Exception e) {
            // [JACKSON-55] Need to add reference information
            wrapAndThrow(providerevaluei);
        }
    }
    public void serializeContentsUsing(Collection<?> valueJsonGenerator jgenSerializerProvider provider,
            JsonSerializer<Objectser)
        throws IOExceptionJsonGenerationException
    {
        Iterator<?> it = value.iterator();
        if (it.hasNext()) {
            TypeSerializer typeSer = ;
            int i = 0;
            do {
                Object elem = it.next();
                try {
                    if (elem == null) {
                        provider.defaultSerializeNull(jgen);
                    } else {
                        if (typeSer == null) {
                            ser.serialize(elemjgenprovider);
                        } else {
                            ser.serializeWithType(elemjgenprovidertypeSer);
                        }
                    }
                    ++i;
                } catch (Exception e) {
                    // [JACKSON-55] Need to add reference information
                    wrapAndThrow(providerevaluei);
                }
            } while (it.hasNext());
        }
    }
New to GrepCode? Check out our FAQ X