1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.mapper; |
20 | |
|
21 | |
import org.apache.johnzon.mapper.access.AccessMode; |
22 | |
import org.apache.johnzon.mapper.access.FieldAccessMode; |
23 | |
import org.apache.johnzon.mapper.access.FieldAndMethodAccessMode; |
24 | |
import org.apache.johnzon.mapper.access.MethodAccessMode; |
25 | |
import org.apache.johnzon.mapper.converter.BigDecimalConverter; |
26 | |
import org.apache.johnzon.mapper.converter.BigIntegerConverter; |
27 | |
import org.apache.johnzon.mapper.converter.BooleanConverter; |
28 | |
import org.apache.johnzon.mapper.converter.ByteConverter; |
29 | |
import org.apache.johnzon.mapper.converter.CachedDelegateConverter; |
30 | |
import org.apache.johnzon.mapper.converter.CharacterConverter; |
31 | |
import org.apache.johnzon.mapper.converter.ClassConverter; |
32 | |
import org.apache.johnzon.mapper.converter.DateConverter; |
33 | |
import org.apache.johnzon.mapper.converter.DoubleConverter; |
34 | |
import org.apache.johnzon.mapper.converter.FloatConverter; |
35 | |
import org.apache.johnzon.mapper.converter.IntegerConverter; |
36 | |
import org.apache.johnzon.mapper.converter.LongConverter; |
37 | |
import org.apache.johnzon.mapper.converter.ShortConverter; |
38 | |
import org.apache.johnzon.mapper.converter.StringConverter; |
39 | |
|
40 | |
import javax.json.JsonReaderFactory; |
41 | |
import javax.json.spi.JsonProvider; |
42 | |
import javax.json.stream.JsonGeneratorFactory; |
43 | |
|
44 | |
import java.math.BigDecimal; |
45 | |
import java.math.BigInteger; |
46 | |
import java.util.Collections; |
47 | |
import java.util.Comparator; |
48 | |
import java.util.Date; |
49 | |
import java.util.HashMap; |
50 | |
import java.util.Map; |
51 | |
|
52 | 48 | public class MapperBuilder { |
53 | 1 | private static final Map<Class<?>, Converter<?>> DEFAULT_CONVERTERS = new HashMap<Class<?>, Converter<?>>(); |
54 | |
|
55 | |
static { |
56 | |
|
57 | 1 | DEFAULT_CONVERTERS.put(Date.class, new DateConverter("yyyyMMddHHmmssZ")); |
58 | 1 | DEFAULT_CONVERTERS.put(Class.class, new ClassConverter()); |
59 | 1 | DEFAULT_CONVERTERS.put(String.class, new StringConverter()); |
60 | 1 | DEFAULT_CONVERTERS.put(BigDecimal.class, new BigDecimalConverter()); |
61 | 1 | DEFAULT_CONVERTERS.put(BigInteger.class, new BigIntegerConverter()); |
62 | 1 | DEFAULT_CONVERTERS.put(Byte.class, new CachedDelegateConverter<Byte>(new ByteConverter())); |
63 | 1 | DEFAULT_CONVERTERS.put(Character.class, new CharacterConverter()); |
64 | 1 | DEFAULT_CONVERTERS.put(Double.class, new DoubleConverter()); |
65 | 1 | DEFAULT_CONVERTERS.put(Float.class, new FloatConverter()); |
66 | 1 | DEFAULT_CONVERTERS.put(Integer.class, new IntegerConverter()); |
67 | 1 | DEFAULT_CONVERTERS.put(Long.class, new LongConverter()); |
68 | 1 | DEFAULT_CONVERTERS.put(Short.class, new ShortConverter()); |
69 | 1 | DEFAULT_CONVERTERS.put(Boolean.class, new CachedDelegateConverter<Boolean>(new BooleanConverter())); |
70 | 1 | DEFAULT_CONVERTERS.put(byte.class, DEFAULT_CONVERTERS.get(Byte.class)); |
71 | 1 | DEFAULT_CONVERTERS.put(char.class, new CharacterConverter()); |
72 | 1 | DEFAULT_CONVERTERS.put(double.class, DEFAULT_CONVERTERS.get(Double.class)); |
73 | 1 | DEFAULT_CONVERTERS.put(float.class, DEFAULT_CONVERTERS.get(Float.class)); |
74 | 1 | DEFAULT_CONVERTERS.put(int.class, DEFAULT_CONVERTERS.get(Integer.class)); |
75 | 1 | DEFAULT_CONVERTERS.put(long.class, DEFAULT_CONVERTERS.get(Long.class)); |
76 | 1 | DEFAULT_CONVERTERS.put(short.class, DEFAULT_CONVERTERS.get(Short.class)); |
77 | 1 | DEFAULT_CONVERTERS.put(boolean.class, DEFAULT_CONVERTERS.get(Boolean.class)); |
78 | 1 | } |
79 | |
|
80 | |
private JsonReaderFactory readerFactory; |
81 | |
private JsonGeneratorFactory generatorFactory; |
82 | 48 | private boolean doCloseOnStreams = false; |
83 | 48 | private boolean supportHiddenAccess = true; |
84 | 48 | private int version = -1; |
85 | 48 | private Comparator<String> attributeOrder = null; |
86 | 48 | private boolean skipNull = true; |
87 | 48 | private boolean skipEmptyArray = false; |
88 | 48 | private AccessMode accessMode = new MethodAccessMode(); |
89 | 48 | private final Map<Class<?>, Converter<?>> converters = new HashMap<Class<?>, Converter<?>>(DEFAULT_CONVERTERS); |
90 | |
|
91 | |
public Mapper build() { |
92 | 48 | if (readerFactory == null || generatorFactory == null) { |
93 | 48 | final JsonProvider provider = JsonProvider.provider(); |
94 | 48 | final Map<String, Object> config = Collections.<String, Object>emptyMap(); |
95 | 48 | if (readerFactory == null) { |
96 | 48 | readerFactory = provider.createReaderFactory(config); |
97 | |
} |
98 | 48 | if (generatorFactory == null) { |
99 | 48 | generatorFactory = provider.createGeneratorFactory(config); |
100 | |
} |
101 | |
} |
102 | |
|
103 | 48 | return new Mapper( |
104 | |
readerFactory, generatorFactory, |
105 | |
doCloseOnStreams, |
106 | |
converters, |
107 | |
version, |
108 | |
attributeOrder, |
109 | |
skipNull, skipEmptyArray, |
110 | |
accessMode, |
111 | |
supportHiddenAccess); |
112 | |
} |
113 | |
|
114 | |
public MapperBuilder setAccessMode(final AccessMode mode) { |
115 | 0 | this.accessMode = mode; |
116 | 0 | return this; |
117 | |
} |
118 | |
|
119 | |
public MapperBuilder setAccessModeName(final String mode) { |
120 | 1 | if ("field".equalsIgnoreCase(mode)) { |
121 | 1 | this.accessMode = new FieldAccessMode(); |
122 | 0 | } else if ("method".equalsIgnoreCase(mode)) { |
123 | 0 | this.accessMode = new MethodAccessMode(); |
124 | 0 | } else if ("both".equalsIgnoreCase(mode)) { |
125 | 0 | this.accessMode = new FieldAndMethodAccessMode(); |
126 | |
} else { |
127 | 0 | throw new IllegalArgumentException("Mode " + mode + " unsupported"); |
128 | |
} |
129 | 1 | return this; |
130 | |
} |
131 | |
|
132 | |
public MapperBuilder setSupportHiddenAccess(final boolean supportHiddenAccess) { |
133 | 1 | this.supportHiddenAccess = supportHiddenAccess; |
134 | 1 | return this; |
135 | |
} |
136 | |
|
137 | |
public MapperBuilder setAttributeOrder(final Comparator<String> attributeOrder) { |
138 | 8 | this.attributeOrder = attributeOrder; |
139 | 8 | return this; |
140 | |
} |
141 | |
|
142 | |
public MapperBuilder setReaderFactory(final JsonReaderFactory readerFactory) { |
143 | 0 | this.readerFactory = readerFactory; |
144 | 0 | return this; |
145 | |
} |
146 | |
|
147 | |
public MapperBuilder setGeneratorFactory(final JsonGeneratorFactory generatorFactory) { |
148 | 0 | this.generatorFactory = generatorFactory; |
149 | 0 | return this; |
150 | |
} |
151 | |
|
152 | |
public MapperBuilder setDoCloseOnStreams(final boolean doCloseOnStreams) { |
153 | 0 | this.doCloseOnStreams = doCloseOnStreams; |
154 | 0 | return this; |
155 | |
} |
156 | |
|
157 | |
public MapperBuilder addPropertyEditor(final Class<?> clazz, final Converter<?> converter) { |
158 | 0 | this.converters.put(clazz, converter); |
159 | 0 | return this; |
160 | |
} |
161 | |
|
162 | |
public MapperBuilder setVersion(final int version) { |
163 | 4 | this.version = version; |
164 | 4 | return this; |
165 | |
} |
166 | |
|
167 | |
public MapperBuilder setSkipNull(final boolean skipNull) { |
168 | 4 | this.skipNull = skipNull; |
169 | 4 | return this; |
170 | |
} |
171 | |
|
172 | |
public MapperBuilder setSkipEmptyArray(final boolean skipEmptyArray) { |
173 | 2 | this.skipEmptyArray = skipEmptyArray; |
174 | 2 | return this; |
175 | |
} |
176 | |
} |