问题描述
我想让gson能够返回一个 EnumMap
对象。我使用下面的代码:
package sandbox;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.EnumMap;
import java.util.Map;
/ **
*
* @author yccheok
* /
公共类沙箱{
public static void main (String [] args)throws InterruptedException {
testGson();
public static enum Country {
Malaysia,
UnitedStates
}
public static void testGson(){
地图< Country,String> enumMap = new EnumMap< Country,String>(Country.class);
enumMap.put(Country.Malaysia,RM);
enumMap.put(Country.UnitedStates,USD);
Gson gson = new Gson();
String string = gson.toJson(enumMap);
System.out.println(toJSon:+ string);
enumMap = gson.fromJson(string,new TypeToken< EnumMap< Country,String>>(){}。getType());
System.out.println(fromJSon:+ enumMap);
System.out.println(fromJSon:+ enumMap.getClass());
但是,我收到以下
toJSon:{Malaysia:RM,UnitedStates:USD}
fromJSon:{Malaysia = RM ,UnitedStates = USD}
fromJSon:class java.util.LinkedHashMap
即使我有使用新TypeToken< EnumMap< Country,String>>(){}。getType()
指定我想 EnumMap
而不是 LinkedHashMap
如何让gson返回 EnumMap
?
即使使用类型标记,Gson也只能将数据反序列化为类有一个默认的构造函数。并且 EnumMap
没有一个(它需要使用其元素将匹配的枚举类型实例化)。解决此问题的最简单方法是定义并使用:
Heres一些示例代码:
InstanceCreator:
class EnumMapInstanceCreator< K extends Enum< K>,V>实现
InstanceCreator< EnumMap< K,V>> {
private final Class< K> enumClazz;
public EnumMapInstanceCreator(final Class< K> enumClazz){
super();
this.enumClazz = enumClazz;
}
@Override
public EnumMap< K,V> createInstance(final类型){
返回新的EnumMap< K,V>(enumClazz);
测试代码:
final Gson gson = new GsonBuilder()。registerTypeAdapter(
New TypeToken< EnumMap< Country,String>>(){
} .getType(),
New EnumMapInstanceCreator< Country,String>(Country.class))
.create();
最终地图< Country,String> enumMap = new EnumMap< Country,String>(
Country.class);
enumMap.put(Country.Malaysia,RM);
enumMap.put(Country.UnitedStates,USD);
String string = gson.toJson(enumMap);
System.out.println(toJSon:+ string);
最终地图< Country,String> reverseEnumMap = gson.fromJson(string,
new TypeToken< EnumMap< Country,String>>(){
} .getType());
System.out.println(fromJSon(Class):+ reverseEnumMap.getClass());
System.out.println(fromJSon:+ reverseEnumMap);
I want to make gson able to return an EnumMap
object. I use the following code
package sandbox;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.EnumMap;
import java.util.Map;
/**
*
* @author yccheok
*/
public class Sandbox {
public static void main(String[] args) throws InterruptedException {
testGson();
}
public static enum Country {
Malaysia,
UnitedStates
}
public static void testGson() {
Map<Country, String> enumMap = new EnumMap<Country, String>(Country.class);
enumMap.put(Country.Malaysia, "RM");
enumMap.put(Country.UnitedStates, "USD");
Gson gson = new Gson();
String string = gson.toJson(enumMap);
System.out.println("toJSon : " + string);
enumMap = gson.fromJson(string, new TypeToken<EnumMap<Country, String>>(){}.getType());
System.out.println("fromJSon : " + enumMap);
System.out.println("fromJSon : " + enumMap.getClass());
}
}
However, I'm getting the following
toJSon : {"Malaysia":"RM","UnitedStates":"USD"}
fromJSon : {Malaysia=RM, UnitedStates=USD}
fromJSon : class java.util.LinkedHashMap
even though I had used new TypeToken<EnumMap<Country, String>>(){}.getType()
to specific I want EnumMap
instead of LinkedHashMap
How can I make gson to return EnumMap
?
Even with a type token, Gson can only deserialize data into classes that have a default constructor. And EnumMap
doesn't have one (it needs to be instantiated with the type of enum that its elements will match). The easiest way around this problem is to define and use an InstanceCreator
:
Heres some example code:
InstanceCreator:
class EnumMapInstanceCreator<K extends Enum<K>, V> implements
InstanceCreator<EnumMap<K, V>> {
private final Class<K> enumClazz;
public EnumMapInstanceCreator(final Class<K> enumClazz) {
super();
this.enumClazz = enumClazz;
}
@Override
public EnumMap<K, V> createInstance(final Type type) {
return new EnumMap<K, V>(enumClazz);
}
}
Test code:
final Gson gson = new GsonBuilder().registerTypeAdapter(
new TypeToken<EnumMap<Country, String>>() {
}.getType(),
new EnumMapInstanceCreator<Country, String>(Country.class))
.create();
final Map<Country, String> enumMap = new EnumMap<Country, String>(
Country.class);
enumMap.put(Country.Malaysia, "RM");
enumMap.put(Country.UnitedStates, "USD");
String string = gson.toJson(enumMap);
System.out.println("toJSon : " + string);
final Map<Country, String> reverseEnumMap = gson.fromJson(string,
new TypeToken<EnumMap<Country, String>>() {
}.getType());
System.out.println("fromJSon (Class): " + reverseEnumMap.getClass());
System.out.println("fromJSon : " + reverseEnumMap);
这篇关于来自Json的GSON返回LinkedHashMap而不是EnumMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!