@@ -54,12 +54,14 @@ public class VPackParser {
54
54
private final Map <ValueType , VPackJsonDeserializer > deserializers ;
55
55
private final Map <String , Map <ValueType , VPackJsonDeserializer >> deserializersByName ;
56
56
private final Map <Class <?>, VPackJsonSerializer <?>> serializers ;
57
+ private final Map <String , Map <Class <?>, VPackJsonSerializer <?>>> serializersByName ;
57
58
58
59
public VPackParser () {
59
60
super ();
60
61
deserializers = new HashMap <ValueType , VPackJsonDeserializer >();
61
62
deserializersByName = new HashMap <String , Map <ValueType , VPackJsonDeserializer >>();
62
63
serializers = new HashMap <Class <?>, VPackJsonSerializer <?>>();
64
+ serializersByName = new HashMap <String , Map <Class <?>, VPackJsonSerializer <?>>>();
63
65
}
64
66
65
67
public String toJson (final VPackSlice vpack ) throws VPackException {
@@ -102,11 +104,36 @@ private VPackJsonDeserializer getDeserializer(final String attribute, final Valu
102
104
return deserializer ;
103
105
}
104
106
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 ) {
106
121
serializers .put (type , serializer );
107
122
return this ;
108
123
}
109
124
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
+
110
137
private void parse (
111
138
final VPackSlice parent ,
112
139
final String attribute ,
@@ -187,7 +214,7 @@ public VPackSlice fromJson(final String json) throws VPackException {
187
214
public VPackSlice fromJson (final String json , final boolean includeNullValues ) throws VPackException {
188
215
final VPackBuilder builder = new VPackBuilder ();
189
216
final JSONParser parser = new JSONParser ();
190
- final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , serializers );
217
+ final ContentHandler contentHandler = new VPackContentHandler (builder , includeNullValues , this );
191
218
try {
192
219
parser .parse (json , contentHandler );
193
220
} catch (final ParseException e ) {
@@ -201,14 +228,14 @@ private static class VPackContentHandler implements ContentHandler {
201
228
private final VPackBuilder builder ;
202
229
private String attribute ;
203
230
private final boolean includeNullValues ;
204
- private final Map < Class <?>, VPackJsonSerializer <?>> serializers ;
231
+ private final VPackParser parser ;
205
232
206
233
public VPackContentHandler (final VPackBuilder builder , final boolean includeNullValues ,
207
- final Map < Class <?>, VPackJsonSerializer <?>> serializers ) {
234
+ final VPackParser parser ) {
208
235
this .builder = builder ;
209
236
this .includeNullValues = includeNullValues ;
237
+ this .parser = parser ;
210
238
attribute = null ;
211
- this .serializers = serializers ;
212
239
}
213
240
214
241
private void add (final ValueType value ) throws ParseException {
@@ -315,7 +342,7 @@ public boolean primitive(final Object value) throws ParseException, IOException
315
342
add (ValueType .NULL );
316
343
}
317
344
} else {
318
- final VPackJsonSerializer <?> serializer = serializers . get ( value .getClass ());
345
+ final VPackJsonSerializer <?> serializer = parser . getSerializer ( attribute , value .getClass ());
319
346
if (serializer != null ) {
320
347
try {
321
348
((VPackJsonSerializer <Object >) serializer ).serialize (builder , attribute , value );
0 commit comments