1 package com.fasterxml.jackson.databind.ser.impl;
2
3 import java.io.IOException;
4 import java.util.*;
5
6 import com.fasterxml.jackson.core.*;
7 import com.fasterxml.jackson.core.type.WritableTypeId;
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
10 import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
11 import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;
12 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
13 import com.fasterxml.jackson.databind.ser.std.StaticListSerializerBase;
14
15 /**
16  * Efficient implement for serializing {@link List}s that contains Strings and are random-accessible.
17  * The only complexity is due to possibility that serializer for {@link String}
18  * may be overridde; because of this, logic is needed to ensure that the default
19  * serializer is in use to use fastest mode, or if not, to defer to custom
20  * String serializer.
21  */

22 @JacksonStdImpl
23 public final class IndexedStringListSerializer
24     extends StaticListSerializerBase<List<String>>
25 {
26     private static final long serialVersionUID = 1L;
27
28     public final static IndexedStringListSerializer instance = new IndexedStringListSerializer();
29
30     /*
31     /**********************************************************
32     /* Life-cycle
33     /**********************************************************
34      */

35     
36     protected IndexedStringListSerializer() {
37         super(List.class);
38     }
39
40     public IndexedStringListSerializer(IndexedStringListSerializer src,
41             Boolean unwrapSingle) {
42         super(src, unwrapSingle);
43     }
44
45     @Override
46     public JsonSerializer<?> _withResolved(BeanProperty prop, Boolean unwrapSingle) {
47         return new IndexedStringListSerializer(this, unwrapSingle);
48     }
49     
50     @Override protected JsonNode contentSchema() { return createSchemaNode("string"true); }
51
52     @Override
53     protected void acceptContentVisitor(JsonArrayFormatVisitor visitor) throws JsonMappingException {
54         visitor.itemsFormat(JsonFormatTypes.STRING);
55     }
56
57     /*
58     /**********************************************************
59     /* Actual serialization
60     /**********************************************************
61      */

62
63     @Override
64     public void serialize(List<String> value, JsonGenerator g,
65             SerializerProvider provider) throws IOException
66     {
67         final int len = value.size();
68         if (len == 1) {
69             if (((_unwrapSingle == null) &&
70                     provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED))
71                     || (_unwrapSingle == Boolean.TRUE)) {
72                 serializeContents(value, g, provider, 1);
73                 return;
74             }
75         }
76         g.writeStartArray(value, len);
77         serializeContents(value, g, provider, len);
78         g.writeEndArray();
79     }
80
81     @Override
82     public void serializeWithType(List<String> value, JsonGenerator g, SerializerProvider provider,
83             TypeSerializer typeSer)
84         throws IOException
85     {
86         WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
87                 typeSer.typeId(value, JsonToken.START_ARRAY));
88         g.setCurrentValue(value);
89         serializeContents(value, g, provider, value.size());
90         typeSer.writeTypeSuffix(g, typeIdDef);
91     }
92
93     private final void serializeContents(List<String> value, JsonGenerator g,
94             SerializerProvider provider, int len) throws IOException
95     {
96         int i = 0;
97         try {
98             for (; i < len; ++i) {
99                 String str = value.get(i);
100                 if (str == null) {
101                     provider.defaultSerializeNull(g);
102                 } else {
103                     g.writeString(str);
104                 }
105             }
106         } catch (Exception e) {
107             wrapAndThrow(provider, e, value, i);
108         }
109     }
110 }
111