@@ -53,11 +53,13 @@ public class VPackParser {
53
53
private static final String NON_REPRESENTABLE_TYPE = "(non-representable type)" ;
54
54
private final Map <ValueType , VPackJsonDeserializer > deserializers ;
55
55
private final Map <String , Map <ValueType , VPackJsonDeserializer >> deserializersByName ;
56
+ private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
56
57
57
58
public VPackParser () {
58
59
super ();
59
60
deserializers = new HashMap <ValueType , VPackJsonDeserializer >();
60
61
deserializersByName = new HashMap <String , Map <ValueType , VPackJsonDeserializer >>();
62
+ serializers = new HashMap <Class <?>, VPackJsonSerializer <?>>();
61
63
}
62
64
63
65
public String toJson (final VPackSlice vpack ) throws VPackException {
@@ -100,6 +102,11 @@ private VPackJsonDeserializer getDeserializer(final String attribute, final Valu
100
102
return deserializer ;
101
103
}
102
104
105
+ public VPackParser registerSerializer (final Class <?> type , final VPackJsonSerializer <?> serializer ) {
106
+ serializers .put (type , serializer );
107
+ return this ;
108
+ }
109
+
103
110
private void parse (
104
111
final VPackSlice parent ,
105
112
final String attribute ,
@@ -180,7 +187,7 @@ public VPackSlice fromJson(final String json) throws VPackException {
180
187
public VPackSlice fromJson (final String json , final boolean includeNullValues ) throws VPackException {
181
188
final VPackBuilder builder = new VPackBuilder ();
182
189
final JSONParser parser = new JSONParser ();
183
- final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues );
190
+ final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , serializers );
184
191
try {
185
192
parser .parse (json , contentHandler );
186
193
} catch (final ParseException e ) {
@@ -194,11 +201,14 @@ private static class VPackContentHandler implements ContentHandler {
194
201
private final VPackBuilder builder ;
195
202
private String attribute ;
196
203
private final boolean includeNullValues ;
204
+ private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
197
205
198
- public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ) {
206
+ public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ,
207
+ final Map <Class <?>, VPackJsonSerializer <?>> serializers ) {
199
208
this .builder = builder ;
200
209
this .includeNullValues = includeNullValues ;
201
210
attribute = null ;
211
+ this .serializers = serializers ;
202
212
}
203
213
204
214
private void add (final ValueType value ) throws ParseException {
@@ -297,20 +307,31 @@ public boolean endArray() throws ParseException, IOException {
297
307
return true ;
298
308
}
299
309
310
+ @ SuppressWarnings ("unchecked" )
300
311
@ Override
301
312
public boolean primitive (final Object value ) throws ParseException , IOException {
302
313
if (value == null ) {
303
314
if (includeNullValues ) {
304
315
add (ValueType .NULL );
305
316
}
306
- } else if (String .class .isAssignableFrom (value .getClass ())) {
307
- add (String .class .cast (value ));
308
- } else if (Boolean .class .isAssignableFrom (value .getClass ())) {
309
- add (Boolean .class .cast (value ));
310
- } else if (Double .class .isAssignableFrom (value .getClass ())) {
311
- add (Double .class .cast (value ));
312
- } else if (Number .class .isAssignableFrom (value .getClass ())) {
313
- add (Long .class .cast (value ));
317
+ } else {
318
+ final VPackJsonSerializer <?> serializer = serializers .get (value .getClass ());
319
+ if (serializer != null ) {
320
+ try {
321
+ ((VPackJsonSerializer <Object >) serializer ).serialize (builder , attribute , value );
322
+ attribute = null ;
323
+ } catch (final VPackBuilderException e ) {
324
+ throw new ParseException (ParseException .ERROR_UNEXPECTED_EXCEPTION );
325
+ }
326
+ } else if (String .class .isAssignableFrom (value .getClass ())) {
327
+ add (String .class .cast (value ));
328
+ } else if (Boolean .class .isAssignableFrom (value .getClass ())) {
329
+ add (Boolean .class .cast (value ));
330
+ } else if (Double .class .isAssignableFrom (value .getClass ())) {
331
+ add (Double .class .cast (value ));
332
+ } else if (Number .class .isAssignableFrom (value .getClass ())) {
333
+ add (Long .class .cast (value ));
334
+ }
314
335
}
315
336
return true ;
316
337
}
0 commit comments