List of usage examples for com.google.gson.reflect TypeToken getRawType
public final Class<? super T> getRawType()
From source file:com.dampcake.gson.immutable.ImmutableAdapterFactory.java
License:Apache License
private <T> TypeAdapter getDelegate(Gson gson, TypeToken<T> type) { Class<?> iface = type.getRawType(); if (!iface.isInterface()) iface = interfaceMap.get(iface); checkState(iface != null, "Non-mappable type found"); return gson.getDelegateAdapter(this, TypeToken.get(iface)); }
From source file:com.datastore_android_sdk.serialization.ForeignCollectionTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); Class<? super T> rawType = typeToken.getRawType(); if (!ForeignCollection.class.isAssignableFrom(rawType)) { return null; }//from ww w . j av a 2 s . c o m final Type elementType = $Gson$Types.getCollectionElementType(type, rawType); TypeToken<?> elementTypeToken = TypeToken.get(elementType); // Specify a TypeAdapter for the element only if we can retreive a concrete type element type TypeAdapter<?> elementTypeAdapter = elementType instanceof TypeVariable ? null : gson.getAdapter(elementTypeToken); @SuppressWarnings({ "unchecked", "rawtypes" }) TypeAdapter<T> result = (TypeAdapter<T>) new Adapter(gson, elementType, elementTypeAdapter) { @Override public ObjectConstructor<T> getConstructor(final ForeignCollectionDeserializationContext context) { return new ObjectConstructor<T>() { public T construct() { if (creator == null) { return null; } else { return (T) creator.createInstance(elementType, context.getParent(), context.getColumnName(), context.getOrderColumnName(), context.getOrderAscending()); } } }; } }; return result; }
From source file:com.datastore_android_sdk.serialization.ModelTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) { Class<? super T> raw = type.getRawType(); if (!Model.class.isAssignableFrom(raw)) { return null; // it's not a Model! }/*ww w.ja v a 2 s .c o m*/ ObjectConstructor<T> constructor = constructorConstructor.get(type); ModelSerializationStrategy serializationStrategy = findSerializationStrategy(raw); return new ModelTypeAdapter<T>(constructor, serializationStrategy, getBoundFields(gson, type, raw)); }
From source file:com.datastore_android_sdk.serialization.ModelTypeAdapterFactory.java
License:Apache License
/** * Searches the registered serialization strategies to find the best matching * strategy for the given {@code type}./* ww w . j a v a2 s . c o m*/ * * @param type * The raw type of the object to find a serialization strategy * @return The best matching serialization strategy or {@code null} if * serialization strategy registered for the given {@code type} */ private ModelSerializationStrategy findSerializationStrategy(Class<?> type) { if (type != null) { Class<?> rawType = type; TypeToken<?> typeToken = TypeToken.get(rawType); // Find if there is a serialization strategy registered for the super // classes while (rawType != Object.class) { if (serializationStrategies.containsKey(rawType)) { return serializationStrategies.get(rawType); } typeToken = TypeToken .get($Gson$Types.resolve(typeToken.getType(), rawType, rawType.getGenericSuperclass())); rawType = typeToken.getRawType(); } // Find if there is a serialization strategy registered for an interface // the type implement Type[] interfaces = type.getGenericInterfaces(); for (Type i : interfaces) { if (serializationStrategies.containsKey(i)) { return serializationStrategies.get(i); } } } return null; }
From source file:com.datastore_android_sdk.serialization.ModelTypeAdapterFactory.java
License:Apache License
/** * Creates the bound fields for the given type. * //from ww w. j a va 2 s .c o m * @param context the Gson context * @param type the type of the class * @param raw the raw type of the class * @return a map of bound fields */ protected Map<String, ModelBoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) { Map<String, ModelBoundField> result = new LinkedHashMap<String, ModelBoundField>(); if (raw.isInterface()) { return result; } Class<?> rawType = raw; TypeToken<?> typeToken = type; Type declaredType = typeToken.getType(); while (rawType != Object.class) { Field[] fields = rawType.getDeclaredFields(); for (Field field : fields) { boolean serialize = includeField(field, true); boolean deserialize = includeField(field, false); if (!serialize && !deserialize) { continue; } field.setAccessible(true); Type fieldType = $Gson$Types.resolve(typeToken.getType(), rawType, field.getGenericType()); ModelBoundField boundField = createBoundField(context, field, getFieldName(field), TypeToken.get(fieldType), serialize, deserialize); BoundField previous = result.put(boundField.name, boundField); if (previous != null) { throw new IllegalArgumentException( declaredType + " declared multiple JSON fields named " + previous.name); } } typeToken = TypeToken .get($Gson$Types.resolve(typeToken.getType(), rawType, rawType.getGenericSuperclass())); rawType = typeToken.getRawType(); } return result; }
From source file:com.defano.wyldcard.runtime.serializer.PostConstructAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { for (Class<?> t = type.getRawType(); (t != Object.class) && (t.getSuperclass() != null); t = t.getSuperclass()) { for (Method m : t.getDeclaredMethods()) { if (m.isAnnotationPresent(PostConstruct.class)) { m.setAccessible(true);//from w w w. ja v a 2 s . co m TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new PostConstructAdapter<>(delegate, m); } } } return null; }
From source file:com.devicehive.json.adapters.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 .ja va 2 s . com*/ 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().remove(typeFieldName); JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(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); Streams.write(jsonObject, out); } }.nullSafe(); }
From source file:com.ecwid.mailchimp.internal.gson.MailChimpObjectTypeAdapter.java
License:Apache License
@SuppressWarnings("unchecked") private MailChimpObjectTypeAdapter(Gson gson, TypeToken<MailChimpObject> type) { this.gson = gson; try {// w ww . j a v a 2 s . co m this.constructor = ((Class<? extends MailChimpObject>) type.getRawType()).getDeclaredConstructor(); this.constructor.setAccessible(true); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("No no-arg counstructor found in " + type.getRawType()); } }
From source file:com.etermax.conversations.repository.impl.elasticsearch.mapper.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 . ja va2 s.com 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().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(); }
From source file:com.example.killnono.common.datacore.net.converter.json.XJsonConverterFactory.java
License:Open Source License
@Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {/*from w w w . j a va 2 s.c om*/ TypeToken<?> typeToken = TypeToken.get(type); if (typeToken.getRawType() == JSONObject.class) { return new JsonResponseBodyConverter<JSONObject>(); } else if (typeToken.getRawType() == JSONArray.class) { return new JsonArrayResponseBodyConverter<JSONArray>(); } else { try { throw new Throwable("?"); } catch (Throwable throwable) { throwable.printStackTrace(); return null; } } }