Java Code Examples for com.google.gson.reflect.TypeToken#getRawType()
The following examples show how to use
com.google.gson.reflect.TypeToken#getRawType() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeAdapters.java From letv with Apache License 2.0 | 6 votes |
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { if (typeToken.getRawType() != Timestamp.class) { return null; } final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class); return new TypeAdapter<Timestamp>() { public Timestamp read(JsonReader in) throws IOException { Date date = (Date) dateTypeAdapter.read(in); return date != null ? new Timestamp(date.getTime()) : null; } public void write(JsonWriter out, Timestamp value) throws IOException { dateTypeAdapter.write(out, value); } }; }
Example 2
Source File: ReflectiveTypeAdapterFactory.java From letv with Apache License 2.0 | 6 votes |
private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) { Map<String, BoundField> result = new LinkedHashMap(); if (!raw.isInterface()) { Type declaredType = type.getType(); while (raw != Object.class) { for (Field field : raw.getDeclaredFields()) { boolean serialize = excludeField(field, true); boolean deserialize = excludeField(field, false); if (serialize || deserialize) { field.setAccessible(true); BoundField boundField = createBoundField(context, field, getFieldName(field), TypeToken.get(C$Gson$Types.resolve(type.getType(), raw, field.getGenericType())), serialize, deserialize); BoundField previous = (BoundField) result.put(boundField.name, boundField); if (previous != null) { throw new IllegalArgumentException(declaredType + " declares multiple JSON fields named " + previous.name); } } } type = TypeToken.get(C$Gson$Types.resolve(type.getType(), raw, raw.getGenericSuperclass())); raw = type.getRawType(); } } return result; }
Example 3
Source File: ResultSSHResultTypeAdapterFactory.java From salt-netapi-client with MIT License | 6 votes |
@Override @SuppressWarnings("unchecked") public <A> TypeAdapter<A> create(Gson gson, TypeToken<A> typeToken) { boolean isResult = typeToken.getRawType() == Result.class; Type type = typeToken.getType(); boolean isSSHResult = isResultSSHResult(type); if (isResult && isSSHResult) { Type parameterType = ((ParameterizedType) type).getActualTypeArguments()[0]; TypeAdapter<SSHResult<A>> sshResultAdapter = (TypeAdapter<SSHResult<A>>) gson.getAdapter(TypeToken.get(parameterType)); return (TypeAdapter<A>) resultAdapter(sshResultAdapter); } else { return null; } }
Example 4
Source File: l.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
public TypeAdapter create(Gson gson, TypeToken typetoken) { if (typetoken.getRawType() == java/sql/Time) { return new TimeTypeAdapter(); } else { return null; } }
Example 5
Source File: PolymorphicTypeResolver.java From rockscript with Apache License 2.0 | 5 votes |
public void add(TypeToken<?> typeToken) { Class<?> rawType = typeToken.getRawType(); this.actualTypeTokens.put(rawType, typeToken); Type genericSuperclass = rawType.getGenericSuperclass(); if (genericSuperclass!=null) { add(TypeToken.get(genericSuperclass)); } }
Example 6
Source File: PostTypeAdapterFactory.java From quill with MIT License | 5 votes |
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { @SuppressWarnings("unchecked") Class<T> rawType = (Class<T>) type.getRawType(); if (rawType != Post.class) { return null; } final TypeAdapter delegate = gson.getDelegateAdapter(this, type); //noinspection unchecked return (TypeAdapter<T>) new TypeAdapter<Post>() { @Override public void write(JsonWriter out, Post value) throws IOException { //noinspection unchecked delegate.write(out, value); } @Override public Post read(JsonReader in) throws IOException { Post post = (Post) delegate.read(in); // Empty posts imported from Ghost 0.11.x have mobiledoc == null, which is incorrect // but we do need to handle it. Drafts created in Ghost 1.0 on the other hand, do // have mobiledoc set to a valid, empty mobiledoc document. if (post.getMobiledoc() != null && !post.getMobiledoc().isEmpty()) { // Post JSON example: // { // "mobiledoc": "{\"version\": \"0.3.1\", ... }", // ... // } post.setMarkdown(GhostApiUtils.mobiledocToMarkdown(post.getMobiledoc())); } else { post.setMarkdown(""); } return post; } }; }
Example 7
Source File: JsonAdapterAnnotationTypeAdapterFactory.java From gson with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) { Class<? super T> rawType = targetType.getRawType(); JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class); if (annotation == null) { return null; } return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation); }
Example 8
Source File: ObjectTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (type.getRawType() == Object.class) { return (TypeAdapter<T>) new ObjectTypeAdapter(gson); } return null; }
Example 9
Source File: JsonAdapterAnnotationOnFieldsTest.java From gson with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) { Class<?> cls = type.getRawType(); if (Long.class.isAssignableFrom(cls)) { return (TypeAdapter<T>) ADAPTER; } else if (long.class.isAssignableFrom(cls)) { return (TypeAdapter<T>) ADAPTER; } throw new IllegalStateException("Non-long field of type " + type + " annotated with @JsonAdapter(LongToStringTypeAdapterFactory.class)"); }
Example 10
Source File: EnumTypeAdapter.java From graphical-lsp with Eclipse Public License 2.0 | 5 votes |
@Override @SuppressWarnings({ "unchecked", "rawtypes" }) public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Class<?> rawType = typeToken.getRawType(); if (!Enum.class.isAssignableFrom(rawType) || rawType == Enum.class) return null; if (!rawType.isEnum()) rawType = rawType.getSuperclass(); try { return new EnumTypeAdapter(rawType); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
Example 11
Source File: TypeAdapters.java From letv with Apache License 2.0 | 5 votes |
public static <TT> TypeAdapterFactory newFactory(final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) { return new TypeAdapterFactory() { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Class<? super T> rawType = typeToken.getRawType(); return (rawType == unboxed || rawType == boxed) ? typeAdapter : null; } public String toString() { return "Factory[type=" + boxed.getName() + "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]"; } }; }
Example 12
Source File: InnerClassTypeAdapterFactory.java From synthea with Apache License 2.0 | 5 votes |
@Override public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; } 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(); try { String subclassName = baseType.getName() + "$" + label.replaceAll("\\s", ""); Class<?> subclass = Class.forName(subclassName); @SuppressWarnings("unchecked") TypeAdapter<R> delegate = (TypeAdapter<R>) gson.getDelegateAdapter( InnerClassTypeAdapterFactory.this, TypeToken.get(subclass)); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label); } return delegate.fromJsonTree(jsonElement); } catch (ClassNotFoundException e) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label); } } @Override public void write(JsonWriter out, R value) throws IOException { throw new NotImplementedException("Write not implemented for InnerClassTypeAdapter"); } }.nullSafe(); }
Example 13
Source File: IrisObjectTypeAdapterFactory.java From arcusplatform with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (type.getRawType() == Object.class) { return (TypeAdapter<T>) new IrisObjectTypeAdapter(gson); } else { return null; } }
Example 14
Source File: ConstructorConstructor.java From MiBandDecompiled with Apache License 2.0 | 5 votes |
public ObjectConstructor get(TypeToken typetoken) { Type type = typetoken.getType(); Class class1 = typetoken.getRawType(); InstanceCreator instancecreator = (InstanceCreator)a.get(type); Object obj; if (instancecreator != null) { obj = new d(this, instancecreator, type); } else { InstanceCreator instancecreator1 = (InstanceCreator)a.get(class1); if (instancecreator1 != null) { return new h(this, instancecreator1, type); } obj = a(class1); if (obj == null) { obj = a(type, class1); if (obj == null) { return b(type, class1); } } } return ((ObjectConstructor) (obj)); }
Example 15
Source File: LaReflectiveTypeAdapterFactory.java From lastaflute with Apache License 2.0 | 5 votes |
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) { final Class<? super T> raw = type.getRawType(); if (!Object.class.isAssignableFrom(raw)) { return null; // it's a primitive! } final ObjectConstructor<T> constructor = constructorConstructor.get(type); return new ReflextiveAdapter<T>(constructor, getBoundFields(gson, type, raw)); }
Example 16
Source File: TypeAdapters.java From letv with Apache License 2.0 | 5 votes |
public static <TT> TypeAdapterFactory newFactory(final Class<TT> type, final TypeAdapter<TT> typeAdapter) { return new TypeAdapterFactory() { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { return typeToken.getRawType() == type ? typeAdapter : null; } public String toString() { return "Factory[type=" + type.getName() + ",adapter=" + typeAdapter + "]"; } }; }
Example 17
Source File: SqlDateTypeAdapter.java From framework with GNU Affero General Public License v3.0 | 4 votes |
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { return typeToken.getRawType() == java.sql.Date.class ? (TypeAdapter<T>) new SqlDateTypeAdapter() : null; }
Example 18
Source File: ISO8601DateTypeAdapter.java From edx-app-android with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") // Type equality is ensured at runtime. @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { return typeToken.getRawType() != Date.class ? null : (TypeAdapter<T>) new ISO8601DateTypeAdapter().nullSafe(); }
Example 19
Source File: HarvesterAnimationStateMachine.java From EmergingTechnology with MIT License | 4 votes |
@Override @SuppressWarnings("unchecked") @Nullable public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (type.getRawType() != ImmutableMultimap.class || !(type.getType() instanceof ParameterizedType)) { return null; } final Type[] typeArguments = ((ParameterizedType) type.getType()).getActualTypeArguments(); if (typeArguments.length != 2 || typeArguments[0] != String.class || typeArguments[1] != String.class) { return null; } final TypeAdapter<Map<String, Collection<String>>> mapAdapter = gson .getAdapter(new TypeToken<Map<String, Collection<String>>>() { }); final TypeAdapter<Collection<String>> collectionAdapter = gson .getAdapter(new TypeToken<Collection<String>>() { }); return (TypeAdapter<T>) new TypeAdapter<ImmutableMultimap<String, String>>() { @Override public void write(JsonWriter out, ImmutableMultimap<String, String> value) throws IOException { mapAdapter.write(out, value.asMap()); } @Override public ImmutableMultimap<String, String> read(JsonReader in) throws IOException { ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder(); in.beginObject(); while (in.hasNext()) { String key = in.nextName(); switch (in.peek()) { case STRING: builder.put(key, in.nextString()); break; case BEGIN_ARRAY: builder.putAll(key, collectionAdapter.read(in)); break; default: throw new JsonParseException("Expected String or Array, got " + in.peek()); } } in.endObject(); return builder.build(); } }; }
Example 20
Source File: ConstructorConstructor.java From gson with Apache License 2.0 | 4 votes |
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { final Type type = typeToken.getType(); final Class<? super T> rawType = typeToken.getRawType(); // first try an instance creator @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> typeCreator = (InstanceCreator<T>) instanceCreators.get(type); if (typeCreator != null) { return new ObjectConstructor<T>() { @Override public T construct() { return typeCreator.createInstance(type); } }; } // Next try raw type match for instance creators @SuppressWarnings("unchecked") // types must agree final InstanceCreator<T> rawTypeCreator = (InstanceCreator<T>) instanceCreators.get(rawType); if (rawTypeCreator != null) { return new ObjectConstructor<T>() { @Override public T construct() { return rawTypeCreator.createInstance(type); } }; } ObjectConstructor<T> defaultConstructor = newDefaultConstructor(rawType); if (defaultConstructor != null) { return defaultConstructor; } ObjectConstructor<T> defaultImplementation = newDefaultImplementationConstructor(type, rawType); if (defaultImplementation != null) { return defaultImplementation; } // finally try unsafe return newUnsafeAllocator(type, rawType); }