1 package com.fasterxml.jackson.databind.ser;
2 
3 import com.fasterxml.jackson.databind.*;
4 import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
5 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
6 
7 /**
8  * Intermediate base class for serializers used for serializing
9  * types that contain element(s) of other types, such as arrays,
10  * {@link java.util.Collection}s (<code>Lists</code>, <code>Sets</code>
11  * etc) and {@link java.util.Map}s and iterable things
12  * ({@link java.util.Iterator}s).
13  */
14 @SuppressWarnings("serial")
15 public abstract class ContainerSerializer<T>
16     extends StdSerializer<T>
17 {
18     /*
19     /**********************************************************
20     /* Construction, initialization
21     /**********************************************************
22      */
23 
ContainerSerializer(Class<T> t)24     protected ContainerSerializer(Class<T> t) {
25         super(t);
26     }
27 
28     /**
29      * @since 2.5
30      */
ContainerSerializer(JavaType fullType)31     protected ContainerSerializer(JavaType fullType) {
32         super(fullType);
33     }
34 
35     /**
36      * Alternate constructor that is (alas!) needed to work
37      * around kinks of generic type handling
38      *
39      * @param t
40      */
ContainerSerializer(Class<?> t, boolean dummy)41     protected ContainerSerializer(Class<?> t, boolean dummy) {
42         super(t, dummy);
43     }
44 
ContainerSerializer(ContainerSerializer<?> src)45     protected ContainerSerializer(ContainerSerializer<?> src) {
46         super(src._handledType, false);
47     }
48 
49     /**
50      * Factory(-like) method that can be used to construct a new container
51      * serializer that uses specified {@link TypeSerializer} for decorating
52      * contained values with additional type information.
53      *
54      * @param vts Type serializer to use for contained values; can be null,
55      *    in which case 'this' serializer is returned as is
56      * @return Serializer instance that uses given type serializer for values if
57      *    that is possible (or if not, just 'this' serializer)
58      */
withValueTypeSerializer(TypeSerializer vts)59     public ContainerSerializer<?> withValueTypeSerializer(TypeSerializer vts) {
60         if (vts == null) return this;
61         return _withValueTypeSerializer(vts);
62     }
63 
64     /*
65     /**********************************************************
66     /* Extended API
67     /**********************************************************
68      */
69 
70     /**
71      * Accessor for finding declared (static) element type for
72      * type this serializer is used for.
73      */
getContentType()74     public abstract JavaType getContentType();
75 
76     /**
77      * Accessor for serializer used for serializing contents
78      * (List and array elements, Map values etc) of the
79      * container for which this serializer is used, if it is
80      * known statically.
81      * Note that for dynamic types this may return null; if so,
82      * caller has to instead use {@link #getContentType()} and
83      * {@link com.fasterxml.jackson.databind.SerializerProvider#findContentValueSerializer}.
84      */
getContentSerializer()85     public abstract JsonSerializer<?> getContentSerializer();
86 
87     /*
88     /**********************************************************
89     /* Abstract methods for sub-classes to implement
90     /**********************************************************
91      */
92 
93 // since 2.5: should be declared abstract in future (2.9?)
94 //    @Override
95 //    public abstract boolean isEmpty(SerializerProvider prov, T value);
96 
97     /**
98      * Method called to determine if the given value (of type handled by
99      * this serializer) contains exactly one element.
100      *<p>
101      * Note: although it might seem sensible to instead define something
102      * like "getElementCount()" method, this would not work well for
103      * containers that do not keep track of size (like linked lists may
104      * not).
105      *<p>
106      * Note, too, that as of now (2.9) this method is only called by serializer
107      * itself; and specifically is not used for non-array/collection types
108      * like <code>Map</code> or <code>Map.Entry</code> instances.
109      */
hasSingleElement(T value)110     public abstract boolean hasSingleElement(T value);
111 
112     /**
113      * Method that needs to be implemented to allow construction of a new
114      * serializer object with given {@link TypeSerializer}, used when
115      * addition type information is to be embedded.
116      */
_withValueTypeSerializer(TypeSerializer vts)117     protected abstract ContainerSerializer<?> _withValueTypeSerializer(TypeSerializer vts);
118 
119     /*
120     /**********************************************************
121     /* Helper methods for sub-types
122     /**********************************************************
123      */
124 
125     /**
126      * Helper method used to encapsulate logic for determining whether there is
127      * a property annotation that overrides element type; if so, we can
128      * and need to statically find the serializer.
129      *
130      * @since 2.1
131      *
132      * @deprecated Since 2.7: should not be needed; should be enough to see if
133      *     type has 'isStatic' modifier
134      */
135     @Deprecated
hasContentTypeAnnotation(SerializerProvider provider, BeanProperty property)136     protected boolean hasContentTypeAnnotation(SerializerProvider provider,
137             BeanProperty property)
138     {
139         /*
140         if (property != null) {
141             AnnotationIntrospector intr = provider.getAnnotationIntrospector();
142             AnnotatedMember m = property.getMember();
143             if ((m != null) && (intr != null)) {
144                 if (intr.findSerializationContentType(m, property.getType()) != null) {
145                     return true;
146                 }
147             }
148         }
149         */
150         return false;
151     }
152 }
153