1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
/**
* Intermediate base class for serializers used for various
* Java arrays.
*
* @param <T> Type of arrays serializer handles
*/
@SuppressWarnings("serial")
public abstract class ArraySerializerBase<T>
extends ContainerSerializer<T>
{
protected final BeanProperty _property;
protected ArraySerializerBase(Class<T> cls)
{
super(cls);
_property = null;
}
protected ArraySerializerBase(Class<T> cls, BeanProperty property)
{
super(cls);
_property = property;
}
protected ArraySerializerBase(ArraySerializerBase<?> src)
{
super(src._handledType, false);
_property = src._property;
}
protected ArraySerializerBase(ArraySerializerBase<?> src, BeanProperty property)
{
super(src._handledType, false);
_property = property;
}
// NOTE: as of 2.5, sub-classes SHOULD override (in 2.4 and before, was final),
// at least if they can provide access to actual size of value and use `writeStartArray()`
// variant that passes size of array to output, which is helpful with some data formats
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException
{
if (provider.isEnabled(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
&& hasSingleElement(value)) {
serializeContents(value, gen, provider);
return;
}
gen.writeStartArray();
// [databind#631]: Assign current value, to be accessible by custom serializers
gen.setCurrentValue(value);
serializeContents(value, gen, provider);
gen.writeEndArray();
}
@Override
public final void serializeWithType(T value, JsonGenerator gen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException
{
// note: let's NOT consider [JACKSON-805] here; gets too complicated, and probably just won't work
typeSer.writeTypePrefixForArray(value, gen);
// [databind#631]: Assign current value, to be accessible by custom serializers
gen.setCurrentValue(value);
serializeContents(value, gen, provider);
typeSer.writeTypeSuffixForArray(value, gen);
}
protected abstract void serializeContents(T value, JsonGenerator jgen, SerializerProvider provider)
throws IOException;
}
|