- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想让 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 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());
}
}
但是,我得到以下信息
toJSon : {"Malaysia":"RM","UnitedStates":"USD"}
fromJSon : {Malaysia=RM, UnitedStates=USD}
fromJSon : class java.util.LinkedHashMap
即使我使用了 new TypeToken<EnumMap<Country, String>>(){}.getType()
具体我想要EnumMap
而不是 LinkedHashMap
如何让 gson 返回 EnumMap
?
最佳答案
即使使用类型标记,Gson 也只能将数据反序列化为具有默认构造函数的类。而 EnumMap
没有(它需要使用其元素将匹配的枚举类型进行实例化)。解决此问题的最简单方法是定义和使用 InstanceCreator
:
This interface is implemented to create instances of a class that does not define a no-args constructor. If you can modify the class, you should instead add a private, or public no-args constructor. However, that is not possible for library classes, such as JDK classes, or a third-party library that you do not have source-code of. In such cases, you should define an instance creator for the class. Implementations of this interface should be registered with GsonBuilder.registerTypeAdapter(Type, Object) method before Gson will be able to use them.
下面是一些示例代码:
实例创建器:
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);
}
}
测试代码:
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);
关于java - GSON fromJson 返回 LinkedHashMap 而不是 EnumMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127904/
我是一名 Java 新手,在概念化如何解决尝试创建表示某些分层数据的 LinkedHashMap 的问题时遇到问题。 这是我目前所知的。在此示例中,我有一个 LinkedHashMap,它显示了展平的
我有 LinkedHashMap 列表,如何不可分割地访问每个 LinkedHashMap。 List> listOfRecords 我想要 List abc= new listOsRecords.1
我有一个 LinkedHashMap,其中包含另一个 LinkedHashMap,如下所示: LinkedHashMap> containerMap = new LinkedHashMap>(); 我
我有 2 个 LinkedHashMap, LinkedHashMap, Color> map; LinkedHashMap, Color> totalMap; 两者是相同的,但 map 已通过使用撤
我有一个 map 列表,我需要单独使用其中的每张 map 以用于进一步的目的。这是我正在使用的代码片段 for(int i = 0; i ()); } int j=0; whi
我有一个扩展 LinkedHashMap 的类 (EntireFile)。我尝试转换: EntireFile old = (EntireFile) functionReturningLinkedHas
我有一个构建 LinkedHashMap 的方法,这样我就可以保持顺序。这是我的方法: public class MyClass { public Map buildMap() {
我是 Scala 的新手。我一直在尝试将 java LinkedHashMap 转换为 Scala 中的等效集合(LinkedHashMap?)以保留插入顺序。 尝试按照其他线程中的建议进行操作,但似
我有一个LinkedHashMap看起来像这样(真的不知道如何说明 HashMap): { "10/10/2010 10:10:10" => "SomeText1", "10/10/2019
我被包裹了LinkedHashMap>进入列表; List>> list = new ArrayList(mainCodesMap.entrySet()); 哪个mainCodeMap是 Map> 的
我有两个链接的 HashMap (key - String, value = String[]),它们在两个链接的 HashMap 中具有相同的大小和相同的键,我希望能够根据键,验证一个链接 Hash
假设我有以下数据结构: LinkedHashMap> foodFamilies = new LinkedHashMap<>(); 看起来像这样: {Fruit = [{Name = Apple,
如何对 LinkedHashMap 的 Arraylist 进行排序 前任。 ArrayList> list = new ArrayList>(); LinkedHashMap lin
LinkedHashMap lHashMap = new LinkedHashMap(); lHashMap.put("One", new Integer(1)); lHashMap.
关闭。这个问题需要details or clarity .它目前不接受答案。 想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题. 8年前关闭。 Improve thi
我有一个简单的问题来找到数组 A 中的第一个唯一元素。但是,令我困扰的是使用不同方法的时间复杂度。到目前为止,我已经尝试过这两种方法。 第一种方法: LinkedHashMap> map = new
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 4 年前。 我正在尝试将一对放入 linkedhashmap 中,但是当我放入 2
我需要用Java实现很久以前在Delphi中实现的代码,我尝试使用LinkedHashMap(在Delphi中使用TStringlist),因为我需要在插入元素时获取元素的索引,问题是是它不起作用..
在正确实现 hashCode 和 equals() 的情况下,以下代码会返回 false? myLinkedHashMap.containsKey(myLinkedHashMap.keySet().i
我让 XStream 使用此 xml 为我构建链接 HashMap : #!/masterofsoundtrack/broadcast #!/masterofsoun
我是一名优秀的程序员,十分优秀!