Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize an EnumMap

I'm trying to figure out how to deserialize an EnumMap. I have been using the Gson library for everything else so far and have been successful. This is proving difficult.

Here's a basic idea:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
import com.google.gson.Gson;

enum FRUIT {
  APPLE, BANANA
}
EnumMap<FRUIT, String> fruitMap;
Gson gson = new Gson();

public void setFruitMap(String jsonString){
  Type typeToken = new TypeToken<EnumMap<FRUIT, String>>(){}.getType();
  fruitMap = gson.fromJson(jsonString, typeToken);
}

String fruitMapString = "{ \"BANANA\":\"tasty!\", \"APPLE\":\"gross!\" }";

setFruitMap(fruitMapString); //Error occurs here.

assertEquals("tasty!", fruitMap.get(FRUIT.BANANA));

When I run the above code, I get a java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.EnumMap

So it seems to me that the Gson library is not creating the EnumMap, but trying to convert after it's made the LinkedHashMap.

So I thought I'd go make my own deserialization logic. Here's an implementation that works. But.. it's kinda janky.

public JsonDeserializer<EnumMap<FRUIT, String>> deserializeFruitMap(){
  return new JsonDeserializer<EnumMap<FRUIT, String>>(){
    @Override
    public EnumMap<FRUIT, String> deserialize(JsonElement element, Type typeOf, JsonDerializationContext context){
      Type token = new TypeToken<HashMap<String, String>>(){}.getType();
      HashMap<String, String> tempMap = context.deserialize(element, token);
      EnumMap<FRUIT, String> fruitMap = new EnumMap<>(FRUIT.class);
      for(Entry<String, String> entry : tempMap.entrySet){
        fruitMap.put(FRUIT.valueOf(entry.getKey()), entry.getValue());
      }
      return fruitMap;
    }
  };
}

So it works, but it's not pretty. And it's very specific. I'd really like to abstract this into something like...

public <K extends Enum<K>, V> JsonDeserializer<EnumMap<K, V>> deserializeEnumMap(){
  return new JsonDeserializer<EnumMap<K, V>>(){
    @Override
    public EnumMap<K, V> deserialize(JsonElement element, Type typeOf, JsonDerializationContext context){
      Type token = new TypeToken<HashMap<String, String>>(){}.getType();
      HashMap<String, String> tempMap = context.deserialize(element, token);
      EnumMap<K, V> enumMap = new EnumMap<>(K.class); //This doesn't work.
      for(Entry<String, String> entry : tempMap.entrySet){
        fruitMap.put(K.valueOf(entry.getKey()), entry.getValue());
      }
      return enumMap;
    }
  };
}

Does anyone have any idea on: 1) How to improve the first method? 2) How to make the abstract version work? Thanks!

like image 603
C Taylor Avatar asked Mar 03 '19 06:03

C Taylor


2 Answers

By default Gson do not recognise EnumMap. It treats it like a regular Map. But this implementation does not have default constructor which Gson could use. We need to provide our InstanceCreator like below:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.EnumMap;

public class JsonApp {

    enum FRUIT {
        APPLE, BANANA
    }

    enum Apple {
        LIGOL,
        MUTSU
    }

    public static void main(String[] args) {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(EnumMap.class, new InstanceCreator<EnumMap>() {
                    @Override
                    public EnumMap createInstance(Type type) {
                        Type[] types = (((ParameterizedType) type).getActualTypeArguments());
                        return new EnumMap((Class<?>) types[0]);
                    }
                })
                .create();

        String jsonString = "{ \"BANANA\":\"tasty!\", \"APPLE\":\"gross!\" }";
        String jsonString1 = "{ \"LIGOL\":\"red!\", \"MUTSU\":\"green!\" }";

        Type enumMap1 = new TypeToken<EnumMap<FRUIT, String>>() {}.getType();
        System.out.println(gson.fromJson(jsonString, enumMap1).toString());

        Type enumMap2 = new TypeToken<EnumMap<Apple, String>>() {}.getType();
        System.out.println(gson.fromJson(jsonString1, enumMap2).toString());
    }
}

See also:

  • Convert java.lang.reflect.Type to Class clazz
  • Serializing a map of enums with Gson with custom serialization
  • GSON fromJson return LinkedHashMap instead of EnumMap
like image 129
Michał Ziober Avatar answered Sep 27 '22 21:09

Michał Ziober


according to the exception, it seems that Gson is able to deserialize the input into a LinkedHashMap. so instead of assignment, you can initialize the instance with the deserialization result:

public void setFruitMap(String jsonString){
  Type typeToken = new TypeToken<Map<FRUIT, String>>(){}.getType();
  fruitMap = new EnumMap<>(gson.fromJson(jsonString, typeToken));
}
like image 34
Sharon Ben Asher Avatar answered Sep 27 '22 19:09

Sharon Ben Asher