@@ -53,11 +53,13 @@ public class VPackParser {
5353 private static final String NON_REPRESENTABLE_TYPE = "(non-representable type)" ;
5454 private final Map <ValueType , VPackJsonDeserializer > deserializers ;
5555 private final Map <String , Map <ValueType , VPackJsonDeserializer >> deserializersByName ;
56+ private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
5657
5758 public VPackParser () {
5859 super ();
5960 deserializers = new HashMap <ValueType , VPackJsonDeserializer >();
6061 deserializersByName = new HashMap <String , Map <ValueType , VPackJsonDeserializer >>();
62+ serializers = new HashMap <Class <?>, VPackJsonSerializer <?>>();
6163 }
6264
6365 public String toJson (final VPackSlice vpack ) throws VPackException {
@@ -100,6 +102,11 @@ private VPackJsonDeserializer getDeserializer(final String attribute, final Valu
100102 return deserializer ;
101103 }
102104
105+ public VPackParser registerSerializer (final Class <?> type , final VPackJsonSerializer <?> serializer ) {
106+ serializers .put (type , serializer );
107+ return this ;
108+ }
109+
103110 private void parse (
104111 final VPackSlice parent ,
105112 final String attribute ,
@@ -180,7 +187,7 @@ public VPackSlice fromJson(final String json) throws VPackException {
180187 public VPackSlice fromJson (final String json , final boolean includeNullValues ) throws VPackException {
181188 final VPackBuilder builder = new VPackBuilder ();
182189 final JSONParser parser = new JSONParser ();
183- final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues );
190+ final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , serializers );
184191 try {
185192 parser .parse (json , contentHandler );
186193 } catch (final ParseException e ) {
@@ -194,11 +201,14 @@ private static class VPackContentHandler implements ContentHandler {
194201 private final VPackBuilder builder ;
195202 private String attribute ;
196203 private final boolean includeNullValues ;
204+ private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
197205
198- public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ) {
206+ public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ,
207+ final Map <Class <?>, VPackJsonSerializer <?>> serializers ) {
199208 this .builder = builder ;
200209 this .includeNullValues = includeNullValues ;
201210 attribute = null ;
211+ this .serializers = serializers ;
202212 }
203213
204214 private void add (final ValueType value ) throws ParseException {
@@ -297,20 +307,31 @@ public boolean endArray() throws ParseException, IOException {
297307 return true ;
298308 }
299309
310+ @ SuppressWarnings ("unchecked" )
300311 @ Override
301312 public boolean primitive (final Object value ) throws ParseException , IOException {
302313 if (value == null ) {
303314 if (includeNullValues ) {
304315 add (ValueType .NULL );
305316 }
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+ }
314335 }
315336 return true ;
316337 }
0 commit comments