map子類,gson 獲取hasmap_GSON fromJson return LinkedHashMap instead of EnumMap

 2023-11-11 阅读 26 评论 0

摘要:問題I want to make gson able to return an EnumMap object. I use the following codemap子類、package sandbox;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;javamap原理?import java.util.EnumMap;import java.util.Map;/**hashmap的實現,**

問題

I want to make gson able to return an EnumMap object. I use the following code

map子類、package sandbox;

import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;

javamap原理?import java.util.EnumMap;

import java.util.Map;

/**

hashmap的實現,*

* @author yccheok

*/

gson使用,public class Sandbox {

public static void main(String[] args) throws InterruptedException {

testGson();

map.getOrDefault?}

public static enum Country {

Malaysia,

hashMap。UnitedStates

}

public static void testGson() {

Map enumMap = new EnumMap(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>(){}.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>(){}.getType() to specific I want EnumMap instead of LinkedHashMap

How can I make gson to return EnumMap?

回答1:

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:

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.

Heres some example code:

InstanceCreator:

class EnumMapInstanceCreator, V> implements

InstanceCreator> {

private final Class enumClazz;

public EnumMapInstanceCreator(final Class enumClazz) {

super();

this.enumClazz = enumClazz;

}

@Override

public EnumMap createInstance(final Type type) {

return new EnumMap(enumClazz);

}

}

Test code:

final Gson gson = new GsonBuilder().registerTypeAdapter(

new TypeToken>() {

}.getType(),

new EnumMapInstanceCreator(Country.class))

.create();

final Map enumMap = new EnumMap(

Country.class);

enumMap.put(Country.Malaysia, "RM");

enumMap.put(Country.UnitedStates, "USD");

String string = gson.toJson(enumMap);

System.out.println("toJSon : " + string);

final Map reverseEnumMap = gson.fromJson(string,

new TypeToken>() {

}.getType());

System.out.println("fromJSon (Class): " + reverseEnumMap.getClass());

System.out.println("fromJSon : " + reverseEnumMap);

來源:https://stackoverflow.com/questions/16127904/gson-fromjson-return-linkedhashmap-instead-of-enummap

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/170817.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息