4
4
import java .lang .reflect .InvocationTargetException ;
5
5
import java .math .BigDecimal ;
6
6
import java .math .BigInteger ;
7
- import java .util .ArrayList ;
8
7
import java .util .Collection ;
9
8
import java .util .HashMap ;
10
9
import java .util .Iterator ;
15
14
16
15
import com .arangodb .velocypack .VPackBuilder .BuilderOptions ;
17
16
import com .arangodb .velocypack .exception .VPackException ;
18
- import com .arangodb .velocypack .exception .VPackKeyTypeException ;
19
17
import com .arangodb .velocypack .exception .VPackParserException ;
20
18
import com .arangodb .velocypack .internal .VPackCache ;
21
19
import com .arangodb .velocypack .internal .VPackCache .FieldInfo ;
22
20
import com .arangodb .velocypack .internal .VPackDeserializers ;
23
21
import com .arangodb .velocypack .internal .VPackInstanceCreators ;
22
+ import com .arangodb .velocypack .internal .VPackKeyMapAdapters ;
24
23
import com .arangodb .velocypack .internal .VPackOptionsImpl ;
25
24
import com .arangodb .velocypack .internal .VPackSerializers ;
26
25
@@ -37,26 +36,11 @@ public static interface VPackOptions extends BuilderOptions {
37
36
38
37
private static final String ATTR_KEY = "key" ;
39
38
private static final String ATTR_VALUE = "value" ;
40
- private static final Collection <Class <?>> KEY_TYPES ;
41
- static {
42
- KEY_TYPES = new ArrayList <Class <?>>();
43
- KEY_TYPES .add (Boolean .class );
44
- KEY_TYPES .add (Integer .class );
45
- KEY_TYPES .add (Long .class );
46
- KEY_TYPES .add (Float .class );
47
- KEY_TYPES .add (Short .class );
48
- KEY_TYPES .add (Double .class );
49
- KEY_TYPES .add (Number .class );
50
- KEY_TYPES .add (BigInteger .class );
51
- KEY_TYPES .add (BigDecimal .class );
52
- KEY_TYPES .add (String .class );
53
- KEY_TYPES .add (Character .class );
54
- KEY_TYPES .add (Enum .class );
55
- }
56
39
57
40
private final Map <Class <?>, VPackSerializer <?>> serializers ;
58
41
private final Map <Class <?>, VPackDeserializer <?>> deserializers ;
59
42
private final Map <Class <?>, VPackInstanceCreator <?>> instanceCreators ;
43
+ private final Map <Class <?>, VPackKeyMapAdapter <?>> keyMapAdapters ;
60
44
private final VPackOptions options ;
61
45
62
46
private final VPackCache cache ;
@@ -73,6 +57,7 @@ public VPack(final VPackOptions options) {
73
57
serializers = new HashMap <Class <?>, VPackSerializer <?>>();
74
58
deserializers = new HashMap <Class <?>, VPackDeserializer <?>>();
75
59
instanceCreators = new HashMap <Class <?>, VPackInstanceCreator <?>>();
60
+ keyMapAdapters = new HashMap <Class <?>, VPackKeyMapAdapter <?>>();
76
61
cache = new VPackCache ();
77
62
serializationContext = new VPackSerializationContext () {
78
63
@ Override
@@ -142,6 +127,18 @@ public <T> T deserialize(final VPackSlice vpack, final Class<T> type) throws VPa
142
127
deserializers .put (Number .class , VPackDeserializers .NUMBER );
143
128
deserializers .put (Character .class , VPackDeserializers .CHARACTER );
144
129
deserializers .put (char .class , VPackDeserializers .CHARACTER );
130
+
131
+ keyMapAdapters .put (String .class , VPackKeyMapAdapters .STRING );
132
+ keyMapAdapters .put (Boolean .class , VPackKeyMapAdapters .BOOLEAN );
133
+ keyMapAdapters .put (Integer .class , VPackKeyMapAdapters .INTEGER );
134
+ keyMapAdapters .put (Long .class , VPackKeyMapAdapters .LONG );
135
+ keyMapAdapters .put (Short .class , VPackKeyMapAdapters .SHORT );
136
+ keyMapAdapters .put (Double .class , VPackKeyMapAdapters .DOUBLE );
137
+ keyMapAdapters .put (Float .class , VPackKeyMapAdapters .FLOAT );
138
+ keyMapAdapters .put (BigInteger .class , VPackKeyMapAdapters .BIG_INTEGER );
139
+ keyMapAdapters .put (BigDecimal .class , VPackKeyMapAdapters .BIG_DECIMAL );
140
+ keyMapAdapters .put (Number .class , VPackKeyMapAdapters .NUMBER );
141
+ keyMapAdapters .put (Character .class , VPackKeyMapAdapters .CHARACTER );
145
142
}
146
143
147
144
public VPackOptions getOptions () {
@@ -277,11 +274,12 @@ private <T> Object deserializeMap(final VPackSlice vpack, final Class<T> type, f
277
274
final Class <?>[] parameterizedTypes = fieldInfo .getParameterizedTypes ();
278
275
final Class <?> keyType = parameterizedTypes [0 ];
279
276
final Class <?> valueType = parameterizedTypes [1 ];
280
- if (isStringableKeyType (keyType )) {
277
+ final VPackKeyMapAdapter <Object > keyMapAdapter = getKeyMapAdapter (keyType );
278
+ if (keyMapAdapter != null ) {
281
279
for (final Iterator <VPackSlice > iterator = vpack .iterator (); iterator .hasNext ();) {
282
280
final VPackSlice key = iterator .next ();
283
281
final VPackSlice valueAt = new VPackSlice (key .getVpack (), key .getStart () + key .getByteSize ());
284
- value .put (getKeyfromString (key .makeKey ().getAsString (), keyType ),
282
+ value .put (keyMapAdapter . deserialize (key .makeKey ().getAsString ()),
285
283
getValue (valueAt , valueType , null ));
286
284
}
287
285
} else {
@@ -296,37 +294,6 @@ private <T> Object deserializeMap(final VPackSlice vpack, final Class<T> type, f
296
294
return value ;
297
295
}
298
296
299
- private Object getKeyfromString (final String key , final Class <?> type ) throws VPackKeyTypeException {
300
- final Object result ;
301
- if (type == String .class ) {
302
- result = key ;
303
- } else if (type == Integer .class ) {
304
- result = Integer .valueOf (key );
305
- } else if (type == Long .class ) {
306
- result = Long .valueOf (key );
307
- } else if (type == Float .class ) {
308
- result = Float .valueOf (key );
309
- } else if (type == Short .class ) {
310
- result = Short .valueOf (key );
311
- } else if (type == Double .class || type == Number .class ) {
312
- result = Double .valueOf (key );
313
- } else if (type == BigInteger .class ) {
314
- result = new BigInteger (key );
315
- } else if (type == BigDecimal .class ) {
316
- result = new BigDecimal (key );
317
- } else if (type == Character .class && key .length () == 1 ) {
318
- result = key .charAt (0 );
319
- } else if (Enum .class .isAssignableFrom (type )) {
320
- final Class <? extends Enum > enumType = (Class <? extends Enum >) type ;
321
- result = Enum .valueOf (enumType , key );
322
- } else if (type == Boolean .class ) {
323
- result = Boolean .valueOf (key );
324
- } else {
325
- throw new VPackKeyTypeException (String .format ("can not convert key: %s in type: %s" , key , type .getName ()));
326
- }
327
- return result ;
328
- }
329
-
330
297
public VPackSlice serialize (final Object entity ) throws VPackParserException {
331
298
final VPackBuilder builder = new VPackBuilder (options );
332
299
try {
@@ -427,11 +394,13 @@ private void serializeMap(
427
394
final Map map = Map .class .cast (value );
428
395
if (map .size () > 0 ) {
429
396
final Class <?> keyType = fieldInfo .getParameterizedTypes ()[0 ];
430
- if (isStringableKeyType (keyType )) {
397
+ final VPackKeyMapAdapter <Object > keyMapAdapter = getKeyMapAdapter (keyType );
398
+ if (keyMapAdapter != null ) {
431
399
builder .add (name , new Value (ValueType .OBJECT ));
432
400
final Set <Entry <?, ?>> entrySet = map .entrySet ();
433
401
for (final Entry <?, ?> entry : entrySet ) {
434
- addValue (keyToString (entry .getKey ()), entry .getValue ().getClass (), entry .getValue (), builder , null );
402
+ addValue (keyMapAdapter .serialize (entry .getKey ()), entry .getValue ().getClass (), entry .getValue (),
403
+ builder , null );
435
404
}
436
405
builder .close ();
437
406
} else {
@@ -451,12 +420,12 @@ private void serializeMap(
451
420
}
452
421
}
453
422
454
- private boolean isStringableKeyType (final Class <?> type ) {
455
- return KEY_TYPES . contains ( type ) || Enum . class . isAssignableFrom (type );
456
- }
457
-
458
- private String keyToString ( final Object key ) {
459
- return Enum . class . isAssignableFrom ( key . getClass ()) ? Enum . class . cast ( key ). name () : key . toString () ;
423
+ private VPackKeyMapAdapter < Object > getKeyMapAdapter (final Class <?> type ) {
424
+ VPackKeyMapAdapter <?> adapter = keyMapAdapters . get (type );
425
+ if ( adapter == null && Enum . class . isAssignableFrom ( type )) {
426
+ adapter = VPackKeyMapAdapters . createEnumAdapter ( type );
427
+ }
428
+ return ( VPackKeyMapAdapter < Object >) adapter ;
460
429
}
461
430
462
431
}
0 commit comments