當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。