How to deserialize an integer key card in java

I am using the flexjson api to serialize and deserialize a map using whole keys. Now, when deserializing, the card keys are converted to a string. Is there a way that keys can be stored as integers.

Here is a sample code

Map<Integer,Object> map = new HashMap<Integer, Object>(); map.put(1, "a"); map.put(2,"b"); flexjson.JSONSerializer serializer = new flexjson.JSONSerializer(); String serializedMapString = serializer.serialize(map); flexjson.JSONDeserializer<Map<Integer,Object>> deserializer = new flexjson.JSONDeserializer<Map<Integer,Object>>(); Map<Integer,Object> deserializedMap = deserializer.deserialize(serializedMapString); for(Integer key: deserializedMap.keySet()){ System.out.println(key+"-"+deserializedMap.get(key)); } 

deserialization does not produce any errors, but the keys are converted to String.

+4
source share
1 answer

This is possible with Genson as follows:

 Genson genson = new Genson(); Map<Integer, Object> result = genson.deserialize(json, new GenericType<Map<Integer, Object>>() {}); 
+1
source

Source: https://habr.com/ru/post/1389605/


All Articles