当前位置: 首页>>代码示例>>Java>>正文


Java Gson.getAdapter方法代码示例

本文整理汇总了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);
        }
    };
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:17,代码来源:TypeAdapters.java

示例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;
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:21,代码来源:MapTypeAdapterFactory.java

示例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);
    }
  };
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:19,代码来源:TypeAdapters.java

示例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;
}
 
开发者ID:EntryPointKR,项目名称:MCLibrary,代码行数:17,代码来源:BukkitTypeAdapterFactory.java

示例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();
}
 
开发者ID:drakeet,项目名称:rebase-android,代码行数:29,代码来源:ResponseTypeAdapterFactory.java

示例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;
  }
}
 
开发者ID:aguther,项目名称:dds-examples,代码行数:40,代码来源:SequenceTypeAdapterFactory.java

示例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;
}
 
开发者ID:OvercastNetwork,项目名称:ProjectAres,代码行数:8,代码来源:InstantTypeAdapter.java

示例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);
}
 
开发者ID:hotpads,项目名称:datarouter,代码行数:11,代码来源:OptionalTypeAdapterFactory.java

示例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));
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:9,代码来源:ArrayTypeAdapter.java

示例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));
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:9,代码来源:MapTypeAdapterFactory.java

示例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));
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:11,代码来源:ArrayTypeAdapter.java

示例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));
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:10,代码来源:CollectionTypeAdapterFactory.java

示例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));
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:13,代码来源:ArrayTypeAdapter.java

示例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();
}
 
开发者ID:Lazyeraser,项目名称:DereHelper,代码行数:36,代码来源:ApiTypeAdapterFactory.java

示例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);
    }
  }
}
 
开发者ID:rockscript,项目名称:rockscript,代码行数:16,代码来源:PolymorphicTypeFields.java


注:本文中的com.google.gson.Gson.getAdapter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。