I m使用一些可变的收集器和I m可治愈的如何利用Gson将其erial除。 由于没有人回答,我已经找到了解决办法,我简化了这个问题并提出了我自己的答案。
我有两个问题:
- How to write a single
Deserializer
working for allImmutableList<XXX>
? - How to register it for all
ImmutableList<XXX>
?
I m使用一些可变的收集器和I m可治愈的如何利用Gson将其erial除。 由于没有人回答,我已经找到了解决办法,我简化了这个问题并提出了我自己的答案。
我有两个问题:
Deserializer
working for all ImmutableList<XXX>
?ImmutableList<XXX>
?最新情况:https://github.com/acebaggins/gson-serializers 覆盖许多瓜瓦收集:
ImmutableList
ImmutableSet
ImmutableSortedSet
ImmutableMap
ImmutableSortedMap
如何写给为所有易变分子工作的单一科学家?
该设想简单易行,即将所通过<代码>Type,即ImmutableList<T>
改为List<T>
,使用,将其改为
易变性:
。
class MyJsonDeserializer implements JsonDeserializer<ImmutableList<?>> {
@Override
public ImmutableList<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
final Type type2 = ParameterizedTypeImpl.make(List.class, ((ParameterizedType) type).getActualTypeArguments(), null);
final List<?> list = context.deserialize(json, type2);
return ImmutableList.copyOf(list);
}
}
多个<代码> Java图书馆课程 我使用这些工具,但没有一个用于公共使用。 我用<代码>sun.reflect.generics.reflectiveObjects.ParaileizedTypeImpl测试。
How to register it for all ImmutableList?
这一部分是三边,登记的第一个论点是java.lang.reflect。 误导我使用<代码> 工作类型代码>,简单使用<代码>Clas
:
final Gson gson = new GsonBuilder()
.registerTypeAdapter(ImmutableList.class, myJsonDeserializer)
.create();
另一项没有参数的实施工作 类型Im
@Override
public ImmutableList<?> deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException {
@SuppressWarnings("unchecked")
final TypeToken<ImmutableList<?>> immutableListToken = (TypeToken<ImmutableList<?>>) TypeToken.of(type);
final TypeToken<? super ImmutableList<?>> listToken = immutableListToken.getSupertype(List.class);
final List<?> list = context.deserialize(json, listToken.getType());
return ImmutableList.copyOf(list);
}
@maaartinus已经涉及第二个问题,因此,我对第一个不需要<代码>的问题提出补充的瓜瓦解决办法。 ParametrizedTypeImpl
public final class ImmutableListDeserializer implements JsonDeserializer<ImmutableList<?>> {
@Override
public ImmutableList<?> deserialize(final JsonElement json, final Type type,
final JsonDeserializationContext context)
throws JsonParseException {
final Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
final Type parameterizedType = listOf(typeArguments[0]).getType();
final List<?> list = context.deserialize(json, parameterizedType);
return ImmutableList.copyOf(list);
}
private static <E> TypeToken<List<E>> listOf(final Type arg) {
return new TypeToken<List<E>>() {}
.where(new TypeParameter<E>() {}, (TypeToken<E>) TypeToken.of(arg));
}
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...