- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用 GSON 来反序列化:
"starterItems": {
"Appeltaart": 3,
"Soap_50": 3
}
...进入 Guava ImmutableMap
:
private ImmutableMap<String,Integer> starterItems;
我以为我会使用常规的 GSON 映射解析,然后制作结果的不可变副本,如下所示:
gb.registerTypeAdapter(ImmutableMap.class, new JsonDeserializer<ImmutableMap>() {
@SuppressWarnings("unchecked")
@Override public ImmutableMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return ImmutableMap.copyOf((Map) context.deserialize(json, Map.class));
}
});
但不出所料,这太简单了(没有类型信息)。我收到错误:
com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object {"Appeltaart":3,"Soap_50":3} given the type interface java.util.Map
我可以做我想做的事吗?
最佳答案
这并不是那么简单,因为您可能希望维护类型参数以构建包含正确类型的映射。为此,您可以使用 TypeAdapterFactory
, 并在那里要求代表 TypeAdapter
, 使用完全指定的 TypeToken
.
public class ImmutableMapTypeAdapterFactory implements TypeAdapterFactory {
public static final ImmutableMapTypeAdapterFactory INSTANCE = new ImmutableMapTypeAdapterFactory();
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!ImmutableMap.class.isAssignableFrom(type.getRawType())) {
return null;
}
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@Override
@SuppressWarnings("unchecked")
public T read(JsonReader in) throws IOException {
return (T) ImmutableMap.copyOf((Map) delegate.read(in));
}
};
}
}
现在你有另一个问题:默认的 GSON MapTypeAdapterFactory
将尝试创建 ImmutableMap
的实例, 并进行修改。这显然行不通。你应该创建一个 TypeToken<HashMap<K, V>>
来自 TypeToken<ImmutableMap<K, V>>
,但老实说,我不知道你该怎么做。相反,您可以使用 InstanceCreator
欺骗 GSON 构建一个 HashMap
当 ImmutableMap
实际上是必需的:
public static <K,V> InstanceCreator<Map<K, V>> newCreator() {
return new InstanceCreator<Map<K, V>>() {
@Override
public Map<K, V> createInstance(Type type) {
return new HashMap<K, V>();
}
};
}
显然,您必须同时注册 TypeAdapterFactory 和 InstanceCreator:
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapterFactory(new ImmutableMapTypeAdapterFactory());
b.registerTypeAdapter(ImmutableMap.class, ImmutableMapTypeAdapterFactory.newCreator());
关于java - 使用 GSON 反序列化为 ImmutableMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13623175/
这个问题已经有答案了: Java-syntax for explicitly specifying generic arguments in method calls (3 个回答) 已关闭 1 年前
我正在学习使用 Guava 库并引用this我看到使用构建器来构造不可变映射的实例。构建器构造函数如何创建抽象类实例? static final ImmutableMap WORD_TO_INT
很像 ImmutableList 可以这样扩展: ImmutableList originalList = ImmutableList.of(1, 2, 3); ImmutableList exten
我有ImmutableMap和List定义顺序。我想要ImmutableList与该命令。例如: map (, , ) list ["kk", "m", "a"] 因此,我想要给定列表中具有定义顺序的
在 Guava 的 ImmutableMap 的 javadocs 中说: Performance notes: unlike HashMap, ImmutableMap is not optimiz
如何处理字符串列表并将其收集到 Map 或 Immutable map 中,只为那些存在值的字符串 String anotherParam = "xyz"; Map.Builder resultMap
在下面非常简单的代码中: public static void test(Map t){ for(Map.Entry e : t.entrySet()){ //The next l
我正在关注this tutorial为了尝试向我的基于 dropwizard 的 API 添加身份验证。 我遇到一个问题,因为本教程需要编写一个 validator 类,并使用以下代码: privat
我在支付网关公司工作,在分析我遇到的代码时ImmuatbleMap 对象。我想知道有关的所有细节。它是什么?它比 map 有什么优势?核心 java 是否支持 ImmuatbleMap? 最佳答案 I
我在 IntelliJ 中有一个包含 pom.xml 的 java 项目,它使用集合 4.0.0 test test 1.0-SNAPSHOT
我想使用 GSON 来反序列化: "starterItems": { "Appeltaart": 3, "Soap_50": 3 } ...进入 Guava ImmutableMap:
比如说,我有一个名为 DomainObject 的类, class DomainObject { private Long id; private String domainParam; }
我和一位同事一起做了一些测试,我们从数据库中提取数据(大约 350,000 条记录),将每条记录转换为一个对象和一个关键对象,然后将它们填充到一个 ImmutableMap.Builder 中。 当我
我们目前正在将 Guava 用于其不可变集合,但我惊讶地发现他们的 map 没有方法可以轻松创建只需稍作修改的新 map 。最重要的是,他们的构建器不允许为键分配新值或删除键。 因此,如果我只想修改一
我一直在使用 Guava 的 ImmutableMap用Builder有一段时间没有考虑它是如何/为什么工作的。我一直按照 Javadoc 描述的方式使用生成器: ImmutableMap WORD_
我有一个返回 map 的方法。我最初会返回方法生成的 HashMap,但认为返回 ImmutableMap 会更好。不幸的是,以下语句拒绝在 eclipse 中工作: HashMap map = ne
我需要 Google Collection ImmutableMap 和 LinkedHashMap 的组合——具有定义迭代顺序的不可变映射。看来 ImmutableMap 本身其实已经定义了迭代顺序
当我序列化 HashMap 时,我可以保留类型信息,以便我可以将它反序列化为相应的类型,但这在序列化 com.google.common.collect 时不起作用.ImmutableMap。如何保留
这个问题在这里已经有了答案: Java 8 collector for Guava immutable collections? (5 个回答) 6年前关闭。 我发现自己想要 Collectors.t
我在 Android 项目中使用 Guava 集合。刚刚注意到以下行为: 在 Activity1 中: Intent i = new Intent(this, Activity2.class); i.
我是一名优秀的程序员,十分优秀!