Code example for TypeToken

Methods: getType

0
abstract class AbstractSpecificationCodec<T> implements JsonSerializer<T>, JsonDeserializer<T> {
 
  protected final <V> JsonElement serializeMap(Map<String, V> map,
                                               JsonSerializationContext context,
                                               Class<V> valueType) {
    Type type = new TypeToken<Map<String, V>>() { }.where(new TypeParameter<V>() { }, valueType).getType();
    return context.serialize(map, type);
  } 
 
  protected final <V> Map<String, V> deserializeMap(JsonElement json,
                                                    JsonDeserializationContext context,
                                                    Class<V> valueType) {
    Type type = new TypeToken<Map<String, V>>() { }.where(new TypeParameter<V>() { }, valueType).getType();
    Map<String, V> map = context.deserialize(json, type);
    return map == null ? ImmutableMap.<String, V>of() : map;
  } 
 
  protected final <V> JsonElement serializeSet(Set<V> set, JsonSerializationContext context, Class<V> valueType) {
    Type type = new TypeToken<Set<V>>() { }.where(new TypeParameter<V>() { }, valueType).getType();
    return context.serialize(set, type);