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 java.math.BigDecimal; |
22 | |
import java.math.BigInteger; |
23 | |
import java.util.Comparator; |
24 | |
import java.util.Date; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.json.JsonReaderFactory; |
29 | |
import javax.json.spi.JsonProvider; |
30 | |
import javax.json.stream.JsonGenerator; |
31 | |
import javax.json.stream.JsonGeneratorFactory; |
32 | |
|
33 | |
import org.apache.johnzon.mapper.access.AccessMode; |
34 | |
import org.apache.johnzon.mapper.access.FieldAccessMode; |
35 | |
import org.apache.johnzon.mapper.access.FieldAndMethodAccessMode; |
36 | |
import org.apache.johnzon.mapper.access.MethodAccessMode; |
37 | |
import org.apache.johnzon.mapper.converter.BigDecimalConverter; |
38 | |
import org.apache.johnzon.mapper.converter.BigIntegerConverter; |
39 | |
import org.apache.johnzon.mapper.converter.BooleanConverter; |
40 | |
import org.apache.johnzon.mapper.converter.ByteConverter; |
41 | |
import org.apache.johnzon.mapper.converter.CachedDelegateConverter; |
42 | |
import org.apache.johnzon.mapper.converter.CharacterConverter; |
43 | |
import org.apache.johnzon.mapper.converter.ClassConverter; |
44 | |
import org.apache.johnzon.mapper.converter.DateConverter; |
45 | |
import org.apache.johnzon.mapper.converter.DoubleConverter; |
46 | |
import org.apache.johnzon.mapper.converter.FloatConverter; |
47 | |
import org.apache.johnzon.mapper.converter.IntegerConverter; |
48 | |
import org.apache.johnzon.mapper.converter.LongConverter; |
49 | |
import org.apache.johnzon.mapper.converter.ShortConverter; |
50 | |
import org.apache.johnzon.mapper.converter.StringConverter; |
51 | |
|
52 | 63 | 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 | 63 | private boolean doCloseOnStreams = false; |
83 | 63 | private boolean supportHiddenAccess = true; |
84 | 63 | private boolean supportGetterForCollections = false; |
85 | 63 | private int version = -1; |
86 | 63 | private int maxSize = -1; |
87 | 63 | private int bufferSize = -1; |
88 | |
private String bufferStrategy; |
89 | 63 | private Comparator<String> attributeOrder = null; |
90 | 63 | private boolean skipNull = true; |
91 | 63 | private boolean skipEmptyArray = false; |
92 | 63 | private boolean supportsComments = false; |
93 | |
protected boolean pretty; |
94 | 63 | private AccessMode accessMode = new MethodAccessMode(false); |
95 | |
private boolean treatByteArrayAsBase64; |
96 | 63 | private final Map<Class<?>, Converter<?>> converters = new HashMap<Class<?>, Converter<?>>(DEFAULT_CONVERTERS); |
97 | |
private boolean supportConstructors; |
98 | |
|
99 | |
public Mapper build() { |
100 | 63 | if (readerFactory == null || generatorFactory == null) { |
101 | 63 | final JsonProvider provider = JsonProvider.provider(); |
102 | 63 | final Map<String, Object> config = new HashMap<String, Object>(); |
103 | 63 | if (maxSize > 0) { |
104 | 0 | config.put("org.apache.johnzon.max-string-length", maxSize); |
105 | |
} |
106 | 63 | if (bufferSize > 0) { |
107 | 0 | config.put("org.apache.johnzon.default-char-buffer", bufferSize); |
108 | |
} |
109 | 63 | if (bufferStrategy != null) { |
110 | 0 | config.put("org.apache.johnzon.buffer-strategy", bufferStrategy); |
111 | |
} |
112 | 63 | if (pretty) { |
113 | 0 | config.put(JsonGenerator.PRETTY_PRINTING, true); |
114 | |
} |
115 | |
|
116 | 63 | if (generatorFactory == null) { |
117 | 63 | generatorFactory = provider.createGeneratorFactory(config); |
118 | |
} |
119 | |
|
120 | 63 | config.remove(JsonGenerator.PRETTY_PRINTING); |
121 | 63 | if (supportsComments) { |
122 | 0 | config.put("org.apache.johnzon.supports-comments", "true"); |
123 | |
} |
124 | 63 | if (readerFactory == null) { |
125 | 63 | readerFactory = provider.createReaderFactory(config); |
126 | |
} |
127 | |
} |
128 | |
|
129 | 63 | return new Mapper( |
130 | |
readerFactory, generatorFactory, |
131 | |
doCloseOnStreams, |
132 | |
converters, |
133 | |
version, |
134 | |
attributeOrder, |
135 | |
skipNull, skipEmptyArray, |
136 | |
accessMode, |
137 | |
supportHiddenAccess, |
138 | |
supportConstructors, |
139 | |
treatByteArrayAsBase64); |
140 | |
} |
141 | |
|
142 | |
public MapperBuilder setSupportGetterForCollections(final boolean useGetterForCollections) { |
143 | 1 | this.supportGetterForCollections = useGetterForCollections; |
144 | 1 | if (supportGetterForCollections) { |
145 | 1 | accessMode = new MethodAccessMode(supportGetterForCollections); |
146 | |
} |
147 | 1 | return this; |
148 | |
} |
149 | |
|
150 | |
public MapperBuilder setSupportsComments(final boolean supportsComments) { |
151 | 0 | this.supportsComments = supportsComments; |
152 | 0 | return this; |
153 | |
} |
154 | |
|
155 | |
public MapperBuilder setPretty(final boolean pretty) { |
156 | 0 | this.pretty = pretty; |
157 | 0 | return this; |
158 | |
} |
159 | |
|
160 | |
public MapperBuilder setBufferSize(final int bufferSize) { |
161 | 0 | this.bufferSize = bufferSize; |
162 | 0 | return this; |
163 | |
} |
164 | |
|
165 | |
public MapperBuilder setBufferStrategy(final String bufferStrategy) { |
166 | 0 | this.bufferStrategy = bufferStrategy; |
167 | 0 | return this; |
168 | |
} |
169 | |
|
170 | |
public MapperBuilder setMaxSize(final int size) { |
171 | 0 | this.maxSize = size; |
172 | 0 | return this; |
173 | |
} |
174 | |
|
175 | |
public MapperBuilder setAccessMode(final AccessMode mode) { |
176 | 1 | this.accessMode = mode; |
177 | 1 | return this; |
178 | |
} |
179 | |
|
180 | |
public MapperBuilder setAccessModeName(final String mode) { |
181 | 3 | if ("field".equalsIgnoreCase(mode)) { |
182 | 3 | this.accessMode = new FieldAccessMode(); |
183 | 0 | } else if ("method".equalsIgnoreCase(mode)) { |
184 | 0 | this.accessMode = new MethodAccessMode(true); |
185 | 0 | } else if ("strict-method".equalsIgnoreCase(mode)) { |
186 | 0 | this.accessMode = new MethodAccessMode(false); |
187 | 0 | } else if ("both".equalsIgnoreCase(mode)) { |
188 | 0 | this.accessMode = new FieldAndMethodAccessMode(); |
189 | |
} else { |
190 | 0 | throw new IllegalArgumentException("Mode " + mode + " unsupported"); |
191 | |
} |
192 | 3 | return this; |
193 | |
} |
194 | |
|
195 | |
public MapperBuilder setSupportHiddenAccess(final boolean supportHiddenAccess) { |
196 | 1 | this.supportHiddenAccess = supportHiddenAccess; |
197 | 1 | return this; |
198 | |
} |
199 | |
|
200 | |
public MapperBuilder setAttributeOrder(final Comparator<String> attributeOrder) { |
201 | 13 | this.attributeOrder = attributeOrder; |
202 | 13 | return this; |
203 | |
} |
204 | |
|
205 | |
public MapperBuilder setReaderFactory(final JsonReaderFactory readerFactory) { |
206 | 0 | this.readerFactory = readerFactory; |
207 | 0 | return this; |
208 | |
} |
209 | |
|
210 | |
public MapperBuilder setGeneratorFactory(final JsonGeneratorFactory generatorFactory) { |
211 | 0 | this.generatorFactory = generatorFactory; |
212 | 0 | return this; |
213 | |
} |
214 | |
|
215 | |
public MapperBuilder setDoCloseOnStreams(final boolean doCloseOnStreams) { |
216 | 0 | this.doCloseOnStreams = doCloseOnStreams; |
217 | 0 | return this; |
218 | |
} |
219 | |
|
220 | |
public MapperBuilder addPropertyEditor(final Class<?> clazz, final Converter<?> converter) { |
221 | 0 | this.converters.put(clazz, converter); |
222 | 0 | return this; |
223 | |
} |
224 | |
|
225 | |
public MapperBuilder setVersion(final int version) { |
226 | 4 | this.version = version; |
227 | 4 | return this; |
228 | |
} |
229 | |
|
230 | |
public MapperBuilder setSkipNull(final boolean skipNull) { |
231 | 4 | this.skipNull = skipNull; |
232 | 4 | return this; |
233 | |
} |
234 | |
|
235 | |
public MapperBuilder setSkipEmptyArray(final boolean skipEmptyArray) { |
236 | 2 | this.skipEmptyArray = skipEmptyArray; |
237 | 2 | return this; |
238 | |
} |
239 | |
|
240 | |
public MapperBuilder setTreatByteArrayAsBase64(final boolean treatByteArrayAsBase64) { |
241 | 1 | this.treatByteArrayAsBase64 = treatByteArrayAsBase64; |
242 | 1 | return this; |
243 | |
} |
244 | |
|
245 | |
public MapperBuilder setSupportConstructors(final boolean supportConstructors) { |
246 | 1 | this.supportConstructors = supportConstructors; |
247 | 1 | return this; |
248 | |
} |
249 | |
} |