Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.databind.ser.impl;
  
  import java.util.*;
  
  
Efficient implement for serializing java.util.Lists that contains Strings and are random-accessible. The only complexity is due to possibility that serializer for java.lang.String may be overridde; because of this, logic is needed to ensure that the default serializer is in use to use fastest mode, or if not, to defer to custom String serializer.
 
 public final class IndexedStringListSerializer
     implements ContextualSerializer
 {
     public final static IndexedStringListSerializer instance = new IndexedStringListSerializer();
     
     protected final JsonSerializer<String_serializer;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
     
     protected IndexedStringListSerializer() {
         this(null);
     }
 
     @SuppressWarnings("unchecked")
     public IndexedStringListSerializer(JsonSerializer<?> ser) {
         super(List.class);
          = (JsonSerializer<String>) ser;
         
     }
 
     @Override protected JsonNode contentSchema() {
         return createSchemaNode("string"true);
     }
 
     @Override
     protected void acceptContentVisitor(JsonArrayFormatVisitor visitor)
         throws JsonMappingException
     {
     }
 
     /*
     /**********************************************************
     /* Post-processing
     /**********************************************************
      */
 
     @Override
     public JsonSerializer<?> createContextual(SerializerProvider provider,
             BeanProperty property)
         throws JsonMappingException
     {
         /* 29-Sep-2012, tatu: Actually, we need to do much more contextual
          *    checking here since we finally know for sure the property,
          *    and it may have overrides
          */
         JsonSerializer<?> ser = null;
         // First: if we have a property, may have property-annotation overrides
         if (property != null) {
             AnnotatedMember m = property.getMember();
             if (m != null) {
                 Object serDef = provider.getAnnotationIntrospector().findContentSerializer(m);
                 if (serDef != null) {
                     ser = provider.serializerInstance(mserDef);
                 }
             }
         }
         if (ser == null) {
             ser = ;
         }
         // #124: May have a content converter
         ser = findConvertingContentSerializer(providerpropertyser);
         if (ser == null) {
             ser = provider.findValueSerializer(String.classproperty);
         } else if (ser instanceof ContextualSerializer) {
             ser = ((ContextualSerializerser).createContextual(providerproperty);
         }
         // Optimization: default serializer just writes String, so we can avoid a call:
         if (isDefaultSerializer(ser)) {
             ser = null;
        }
        // note: will never have TypeSerializer, because Strings are "natural" type
        if (ser == ) {
            return this;
        }
        return new IndexedStringListSerializer(ser);
    }
    /*
    /**********************************************************
    /* Actual serialization
    /**********************************************************
     */
    @Override
    public void serialize(List<StringvalueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        final int len = value.size();
        // [JACKSON-805]
        if ((len == 1) && provider.isEnabled(.)) {
            _serializeUnwrapped(valuejgenprovider);
            return;
        }
        
        jgen.writeStartArray();
        if ( == null) {
            serializeContents(valuejgenproviderlen);
        } else {
            serializeUsingCustom(valuejgenproviderlen);
        }
        jgen.writeEndArray();
    }
    private final void _serializeUnwrapped(List<StringvalueJsonGenerator jgenSerializerProvider provider)
        throws IOExceptionJsonGenerationException
    {
        if ( == null) {
            serializeContents(valuejgenprovider, 1);
        } else {
            serializeUsingCustom(valuejgenprovider, 1);
        }
    }
    
    @Override
    public void serializeWithType(List<StringvalueJsonGenerator jgenSerializerProvider provider,
            TypeSerializer typeSer)
        throws IOExceptionJsonGenerationException
    {
        final int len = value.size();
        typeSer.writeTypePrefixForArray(valuejgen);
        if ( == null) {
            serializeContents(valuejgenproviderlen);
        } else {
            serializeUsingCustom(valuejgenproviderlen);
        }
        typeSer.writeTypeSuffixForArray(valuejgen);
    }
    
    private final void serializeContents(List<StringvalueJsonGenerator jgenSerializerProvider provider,
            int len)
        throws IOExceptionJsonGenerationException
    {
        int i = 0;
        try {
            for (; i < len; ++i) {
                String str = value.get(i);
                if (str == null) {
                    provider.defaultSerializeNull(jgen);
                } else {
                    jgen.writeString(str);
                }
            }
        } catch (Exception e) {
            wrapAndThrow(providerevaluei);
        }
    }
    private final void serializeUsingCustom(List<StringvalueJsonGenerator jgenSerializerProvider provider,
            int len)
        throws IOExceptionJsonGenerationException
    {
        int i = 0;
        try {
            final JsonSerializer<Stringser = ;
            for (i = 0; i < len; ++i) {
                String str = value.get(i);
                if (str == null) {
                    provider.defaultSerializeNull(jgen);
                } else {
                    ser.serialize(strjgenprovider);
                }
            }
        } catch (Exception e) {
            wrapAndThrow(providerevaluei);
        }
    }
New to GrepCode? Check out our FAQ X