本文整理汇总了Java中com.google.gson.Gson.getAdapter方法的典型用法代码示例。如果您正苦于以下问题:Java Gson.getAdapter方法的具体用法?Java Gson.getAdapter怎么用?Java Gson.getAdapter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.Gson
的用法示例。
在下文中一共展示了Gson.getAdapter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import com.google.gson.Gson; //导入方法依赖的package包/类
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);
}
};
}
示例2: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Map.class.isAssignableFrom(rawType)) {
return null;
}
Class<?> rawTypeOfSrc = $Gson$Types.getRawType(type);
Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(type, rawTypeOfSrc);
TypeAdapter<?> keyAdapter = getKeyAdapter(gson, keyAndValueTypes[0]);
TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(keyAndValueTypes[1]));
ObjectConstructor<T> constructor = constructorConstructor.get(typeToken);
@SuppressWarnings({"unchecked", "rawtypes"})
// we don't define a type parameter for the key or value types
TypeAdapter<T> result = new Adapter(gson, keyAndValueTypes[0], keyAdapter,
keyAndValueTypes[1], valueAdapter, constructor);
return result;
}
示例3: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
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 (TypeAdapter<T>) new TypeAdapter<Timestamp>() {
@Override public Timestamp read(JsonReader in) throws IOException {
Date date = dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
@Override public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例4: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();
TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class);
TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class);
if (Location.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new LocationTypeAdapter();
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter);
} else if (GUISignature.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter);
} else if (MaterialAndData.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new MaterialAndDataAdapter();
}
return null;
}
示例5: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegateAdapter.write(out, value);
}
@Override
@SuppressWarnings("PointlessBooleanExpression")
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = jsonElementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has(ERROR) && jsonObject.get(ERROR).getAsBoolean() == false) {
if (jsonObject.has(DATA) && isJson(jsonObject, DATA)) {
jsonElement = jsonObject.get(DATA);
}
}
}
return delegateAdapter.fromJsonTree(jsonElement);
}
}.nullSafe();
}
示例6: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(
final Gson gson,
final TypeToken<T> typeToken
) {
// get raw type
Class rawType = typeToken.getRawType();
// check if the type applies to this factory
if (!LoanableSequence.class.isAssignableFrom(rawType)) {
return null;
}
try {
// we need the nested type of the sequence but since RTI is using generics
// or any other proper interface, we need to get the class from the method
// "<NestedType> get(int)" that is generated into every sequence
// get method
Method getMethod = rawType.getMethod("get", int.class);
// get nested type
Class nestedType = getMethod.getReturnType();
// get type adapter for nested type
TypeAdapter<?> nestedTypeAdapter = gson.getAdapter(nestedType);
// return new type adapter
return (TypeAdapter<T>) new SequenceTypeAdapter(
rawType,
nestedTypeAdapter
);
} catch (NoSuchMethodException e) {
// if method '<NestedType> get(int)' is not found,
// we cannot provide a type adapter
return null;
}
}
示例7: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if(Instant.class == type.getRawType()) {
return (TypeAdapter<T>) new InstantTypeAdapter(gson.getAdapter(Date.class));
}
return null;
}
示例8: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken){
if(typeToken.getRawType() != Optional.class){
return null;
}
Type actualType = ((ParameterizedType)typeToken.getType()).getActualTypeArguments()[0];
TypeAdapter<?> typeAdapter = gson.getAdapter(TypeToken.get(actualType));
return new OptionalTypeAdapter(typeAdapter);
}
示例9: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType) && (!(type instanceof Class) || !((Class) type).isArray())) {
return null;
}
Type componentType = C$Gson$Types.getArrayComponentType(type);
return new ArrayTypeAdapter(gson, gson.getAdapter(TypeToken.get(componentType)), C$Gson$Types.getRawType(componentType));
}
示例10: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!Map.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}
Type[] keyAndValueTypes = C$Gson$Types.getMapKeyAndValueTypes(type, C$Gson$Types.getRawType(type));
return new Adapter(gson, keyAndValueTypes[0], getKeyAdapter(gson, keyAndValueTypes[0]), keyAndValueTypes[1], gson.getAdapter(TypeToken.get(keyAndValueTypes[1])), this.constructorConstructor.getConstructor(typeToken));
}
示例11: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType) && (!(type instanceof Class) || !((Class)
type).isArray())) {
return null;
}
Type componentType = C$Gson$Types.getArrayComponentType(type);
return new ArrayTypeAdapter(gson, gson.getAdapter(TypeToken.get(componentType)),
C$Gson$Types.getRawType(componentType));
}
示例12: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return null;
}
Type elementType = C$Gson$Types.getCollectionElementType(type, rawType);
return new Adapter(gson, elementType, gson.getAdapter(TypeToken.get(elementType)), this.constructorConstructor.getConstructor(typeToken));
}
示例13: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
return null;
}
Type componentType = $Gson$Types.getArrayComponentType(type);
TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
return new ArrayTypeAdapter(
gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
}
示例14: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementTypeAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@Override
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementTypeAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("status")) {
int status = jsonObject.get("status").getAsInt();
String message = jsonObject.get("message").getAsString();
if (status == 0) {
//do nothing
} else {
throw new ApiException(status, message);
}
}
if (jsonObject.has(dataElementName)) {
jsonElement = jsonObject.get(dataElementName);
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
示例15: scanFields
import com.google.gson.Gson; //导入方法依赖的package包/类
/** adds all declared fields in the given inheritanceType to the polymorphicFields member field */
private void scanFields(TypeToken inheritanceType, Map<String, Type> actualTypeArguments, Gson gson) {
Class rawClass = inheritanceType.getRawType();
for (Field field: rawClass.getDeclaredFields()) {
if (!Modifier.isTransient(field.getModifiers())) {
Type fieldType = field.getGenericType();
Type concreteFieldType = concretize(fieldType, actualTypeArguments);
TypeToken<?> concreteFieldTypeToken = TypeToken.get(concreteFieldType);
TypeAdapter<?> fieldTypeAdapter = gson.getAdapter(concreteFieldTypeToken);
@SuppressWarnings("unchecked")
PolymorphicField polymorphicField = new PolymorphicField(field, fieldTypeAdapter);
polymorphicFields.put(field.getName(), polymorphicField);
}
}
}