List of usage examples for com.google.gson.reflect TypeToken getRawType
public final Class<? super T> getRawType()
From source file:com.github.piasy.common.model.AutoGenTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w ww . j a v a 2s . co m*/ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); final AutoGson annotation = rawType.getAnnotation(AutoGson.class); // Only deserialize classes decorated with @AutoGson. return annotation == null ? null : (TypeAdapter<T>) gson.getAdapter(annotation.autoClass()); }
From source file:com.github.sheigutn.pushbullet.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }// w w w .ja v a 2 s. co m if (baseDelegateAdapter == null) { baseDelegateAdapter = gson.getDelegateAdapter(RuntimeTypeAdapterFactory.this, TypeToken.get(baseType)); } final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @SuppressWarnings("unchecked") @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //remove(typeFieldName) changed to get(typeFieldName) if (labelJsonElement == null) { return (R) baseDelegateAdapter.fromJsonTree(jsonElement); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); //if (jsonObject.has(typeFieldName)) { // throw new JsonParseException("cannot serialize " + srcType.getName() // + " because it already defines a field named " + typeFieldName); <-- Commented //} JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.google.caliper.json.ImmutableListTypeAdatperFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from www.jav a 2 s . c om*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (typeToken.getRawType() != ImmutableList.class || !(type instanceof ParameterizedType)) { return null; } com.google.common.reflect.TypeToken<ImmutableList<?>> betterToken = (com.google.common.reflect.TypeToken<ImmutableList<?>>) com.google.common.reflect.TypeToken .of(typeToken.getType()); final TypeAdapter<ArrayList<?>> arrayListAdapter = (TypeAdapter<ArrayList<?>>) gson.getAdapter( TypeToken.get(betterToken.getSupertype(List.class).getSubtype(ArrayList.class).getType())); return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { ArrayList<?> arrayList = Lists.newArrayList((List<?>) value); arrayListAdapter.write(out, arrayList); } @Override public T read(JsonReader in) throws IOException { ArrayList<?> arrayList = arrayListAdapter.read(in); return (T) ImmutableList.copyOf(arrayList); } }; }
From source file:com.google.caliper.json.ImmutableMapTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w . j av a 2s .c o m*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (typeToken.getRawType() != ImmutableMap.class || !(type instanceof ParameterizedType)) { return null; } com.google.common.reflect.TypeToken<ImmutableMap<?, ?>> betterToken = (com.google.common.reflect.TypeToken<ImmutableMap<?, ?>>) com.google.common.reflect.TypeToken .of(typeToken.getType()); final TypeAdapter<HashMap<?, ?>> hashMapAdapter = (TypeAdapter<HashMap<?, ?>>) gson .getAdapter(TypeToken.get(betterToken.getSupertype(Map.class).getSubtype(HashMap.class).getType())); return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { HashMap<?, ?> hashMap = Maps.newHashMap((Map<?, ?>) value); hashMapAdapter.write(out, hashMap); } @Override public T read(JsonReader in) throws IOException { HashMap<?, ?> hashMap = hashMapAdapter.read(in); return (T) ImmutableMap.copyOf(hashMap); } }; }
From source file:com.google.caliper.json.NaturallySortedMapTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*w w w .j av a2s . co m*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (!CLASSES.contains(typeToken.getRawType()) || !(type instanceof ParameterizedType)) { return null; } com.google.common.reflect.TypeToken<SortedMap<?, ?>> betterToken = (com.google.common.reflect.TypeToken<SortedMap<?, ?>>) com.google.common.reflect.TypeToken .of(typeToken.getType()); final TypeAdapter<Map<?, ?>> mapAdapter = (TypeAdapter<Map<?, ?>>) gson .getAdapter(TypeToken.get(betterToken.getSupertype(Map.class).getType())); return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { TreeMap<?, ?> treeMap = Maps.newTreeMap((SortedMap<?, ?>) value); mapAdapter.write(out, treeMap); } @SuppressWarnings("rawtypes") @Override public T read(JsonReader in) throws IOException { TreeMap treeMap = Maps.newTreeMap(); treeMap.putAll(mapAdapter.read(in)); return (T) treeMap; } }; }
From source file:com.google.devtools.moe.client.gson.AutoValueGsonAdapter.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Class<?> type = typeToken.getRawType(); if (!Modifier.isAbstract(type.getModifiers())) { return null; // AutoValues are never concrete types. }// ww w . j av a 2 s. c o m try { Class<?> implType = Class.forName(implementationName(type)); @SuppressWarnings("unchecked") TypeAdapter<T> reflectiveDelegate = (TypeAdapter<T>) gson.getAdapter(implType); // TODO(cgruber) Discover a builder and supply a builder-aware adapter. return new AutoValueTypeAdapter<T>(gson, reflectiveDelegate, implType); } catch (ClassNotFoundException | NoClassDefFoundError e) { return null; // Not an AutoValue or no AutoValue implementation class was generated. } }
From source file:com.greensopinion.finance.services.persistence.CategoriesTypeAdapter.java
License:Apache License
public static TypeAdapterFactory factory(EncryptorProviderService encryptorProviderService) { return new TypeAdapterFactory() { @SuppressWarnings("unchecked") @Override/*ww w .java2 s . c o m*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (Categories.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter<T>) new CategoriesTypeAdapter(gson, encryptorProviderService); } return null; } }; }
From source file:com.greensopinion.finance.services.persistence.TransactionsTypeAdapter.java
License:Apache License
public static TypeAdapterFactory factory(EncryptorProviderService encryptorProviderService) { return new TypeAdapterFactory() { @SuppressWarnings("unchecked") @Override/* w w w. j a v a 2 s . c om*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (Transactions.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter<T>) new TransactionsTypeAdapter(gson, encryptorProviderService); } return null; } }; }
From source file:com.gs.reladomo.serial.gson.GsonWrappedTypeAdaptorFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { if (Serialized.class.equals(typeToken.getRawType())) { Type type = typeToken.getType(); if (type instanceof ParameterizedType) { Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments(); return new GsonWrappedTypedAdapter((Class) typeArguments[0]); }//ww w. j a v a 2 s .c o m return new GsonWrappedTypedAdapter(null); } if (SerializedList.class.equals(typeToken.getRawType())) { Type type = typeToken.getType(); if (type instanceof ParameterizedType) { Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments(); return new GsonWrappedListTypedAdapter((Class) typeArguments[0]); } return new GsonWrappedListTypedAdapter(null); } return null; }
From source file:com.gsaul.AethonSimulator.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w w w. j a v a2 s .c o m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName); //used to be .remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }