@@ -54,12 +54,14 @@ public class VPackParser {
5454 private final Map <ValueType , VPackJsonDeserializer > deserializers ;
5555 private final Map <String , Map <ValueType , VPackJsonDeserializer >> deserializersByName ;
5656 private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
57+ private final Map <String , Map <Class <?>, VPackJsonSerializer <?>>> serializersByName ;
5758
5859 public VPackParser () {
5960 super ();
6061 deserializers = new HashMap <ValueType , VPackJsonDeserializer >();
6162 deserializersByName = new HashMap <String , Map <ValueType , VPackJsonDeserializer >>();
6263 serializers = new HashMap <Class <?>, VPackJsonSerializer <?>>();
64+ serializersByName = new HashMap <String , Map <Class <?>, VPackJsonSerializer <?>>>();
6365 }
6466
6567 public String toJson (final VPackSlice vpack ) throws VPackException {
@@ -102,11 +104,36 @@ private VPackJsonDeserializer getDeserializer(final String attribute, final Valu
102104 return deserializer ;
103105 }
104106
105- public VPackParser registerSerializer (final Class <?> type , final VPackJsonSerializer <?> serializer ) {
107+ public <T > VPackParser registerSerializer (
108+ final String attribute ,
109+ final Class <T > type ,
110+ final VPackJsonSerializer <T > serializer ) {
111+ Map <Class <?>, VPackJsonSerializer <?>> byName = serializersByName .get (attribute );
112+ if (byName == null ) {
113+ byName = new HashMap <Class <?>, VPackJsonSerializer <?>>();
114+ serializersByName .put (attribute , byName );
115+ }
116+ byName .put (type , serializer );
117+ return this ;
118+ }
119+
120+ public <T > VPackParser registerSerializer (final Class <T > type , final VPackJsonSerializer <T > serializer ) {
106121 serializers .put (type , serializer );
107122 return this ;
108123 }
109124
125+ private VPackJsonSerializer <?> getSerializer (final String attribute , final Class <?> type ) {
126+ VPackJsonSerializer <?> serializer = null ;
127+ final Map <Class <?>, VPackJsonSerializer <?>> byName = serializersByName .get (attribute );
128+ if (byName != null ) {
129+ serializer = byName .get (type );
130+ }
131+ if (serializer == null ) {
132+ serializer = serializers .get (type );
133+ }
134+ return serializer ;
135+ }
136+
110137 private void parse (
111138 final VPackSlice parent ,
112139 final String attribute ,
@@ -187,7 +214,7 @@ public VPackSlice fromJson(final String json) throws VPackException {
187214 public VPackSlice fromJson (final String json , final boolean includeNullValues ) throws VPackException {
188215 final VPackBuilder builder = new VPackBuilder ();
189216 final JSONParser parser = new JSONParser ();
190- final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , serializers );
217+ final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , this );
191218 try {
192219 parser .parse (json , contentHandler );
193220 } catch (final ParseException e ) {
@@ -201,14 +228,14 @@ private static class VPackContentHandler implements ContentHandler {
201228 private final VPackBuilder builder ;
202229 private String attribute ;
203230 private final boolean includeNullValues ;
204- private final Map < Class <?>, VPackJsonSerializer <?>> serializers ;
231+ private final VPackParser parser ;
205232
206233 public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ,
207- final Map < Class <?>, VPackJsonSerializer <?>> serializers ) {
234+ final VPackParser parser ) {
208235 this .builder = builder ;
209236 this .includeNullValues = includeNullValues ;
237+ this .parser = parser ;
210238 attribute = null ;
211- this .serializers = serializers ;
212239 }
213240
214241 private void add (final ValueType value ) throws ParseException {
@@ -315,7 +342,7 @@ public boolean primitive(final Object value) throws ParseException, IOException
315342 add (ValueType .NULL );
316343 }
317344 } else {
318- final VPackJsonSerializer <?> serializer = serializers . get ( value .getClass ());
345+ final VPackJsonSerializer <?> serializer = parser . getSerializer ( attribute , value .getClass ());
319346 if (serializer != null ) {
320347 try {
321348 ((VPackJsonSerializer <Object >) serializer ).serialize (builder , attribute , value );
0 commit comments