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


Java TypeToken.get方法代码示例

本文整理汇总了Java中com.google.gson.reflect.TypeToken.get方法的典型用法代码示例。如果您正苦于以下问题:Java TypeToken.get方法的具体用法?Java TypeToken.get怎么用?Java TypeToken.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.gson.reflect.TypeToken的用法示例。


在下文中一共展示了TypeToken.get方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: responseBodyConverter

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    TypeToken<?> typeToken = TypeToken.get(type);
    if (typeToken.getRawType() == JSONObject.class) {
        return new JsonResponseBodyConverter<JSONObject>();
    } else if (typeToken.getRawType() == JSONArray.class) {
        return new ListResponseBodyConverter<JSONArray>();
    } else {
        try {
            throw new Throwable("非法类型");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }

}
 
开发者ID:killnono,项目名称:Dalaran,代码行数:18,代码来源:JsonConverterFactory.java

示例2: responseBodyConverter

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    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;
        }
    }

}
 
开发者ID:killnono,项目名称:Dalaran,代码行数:18,代码来源:XJsonConverterFactory.java

示例3: getBoundFields

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
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;
}
 
开发者ID:JackChan1999,项目名称:letv,代码行数:24,代码来源:ReflectiveTypeAdapterFactory.java

示例4: registerTypeAdapter

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
/**
 * Configures Gson for custom serialization or deserialization. This method combines the
 * registration of an {@link TypeAdapter}, {@link InstanceCreator}, {@link JsonSerializer}, and a
 * {@link JsonDeserializer}. It is best used when a single object {@code typeAdapter} implements
 * all the required interfaces for custom serialization with Gson. If a type adapter was
 * previously registered for the specified {@code type}, it is overwritten.
 *
 * <p>This registers the type specified and no other types: you must manually register related
 * types! For example, applications registering {@code boolean.class} should also register {@code
 * Boolean.class}.
 *
 * @param type the type definition for the type adapter being registered
 * @param typeAdapter This object must implement at least one of the {@link TypeAdapter},
 * {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
 * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
  $Gson$Preconditions.checkArgument(typeAdapter instanceof JsonSerializer<?>
      || typeAdapter instanceof JsonDeserializer<?>
      || typeAdapter instanceof InstanceCreator<?>
      || typeAdapter instanceof TypeAdapter<?>);
  if (typeAdapter instanceof InstanceCreator<?>) {
    instanceCreators.put(type, (InstanceCreator) typeAdapter);
  }
  if (typeAdapter instanceof JsonSerializer<?> || typeAdapter instanceof JsonDeserializer<?>) {
    TypeToken<?> typeToken = TypeToken.get(type);
    factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(typeToken, typeAdapter));
  }
  if (typeAdapter instanceof TypeAdapter<?>) {
    factories.add(TypeAdapters.newFactory(TypeToken.get(type), (TypeAdapter)typeAdapter));
  }
  return this;
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:35,代码来源:GsonBuilder.java

示例5: getActualTypeArguments

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
/** creates a map that maps generic type argument names to type tokens */
private static Map<String, TypeToken> getActualTypeArguments(TypeToken<?> typeToken) {
  Class<?> rawClass = typeToken.getRawType();
  Type type = typeToken.getType();
  TypeVariable<? extends Class<?>>[] typeParameters = rawClass.getTypeParameters();
  if (typeParameters==null || !(type instanceof ParameterizedType)) {
    return null;
  }
  Map<String, TypeToken> genericTypes = new HashMap<>();
  ParameterizedType parameterizedType = (ParameterizedType) type;
  Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
  for (int i=0; i<typeParameters.length; i++) {
    String typeParameterName = typeParameters[i].getName();
    TypeToken<?> actualType = TypeToken.get(actualTypeArguments[i]);
    genericTypes.put(typeParameterName, actualType);
  }
  return genericTypes;
}
 
开发者ID:rockscript,项目名称:rockscript,代码行数:19,代码来源:PolymorphicTypeAdapter.java

示例6: getBoundFields

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) {
  Map<String, BoundField> result = new LinkedHashMap<String, BoundField>();
  if (raw.isInterface()) {
    return result;
  }

  Type declaredType = type.getType();
  while (raw != Object.class) {
    Field[] fields = raw.getDeclaredFields();
    for (Field field : fields) {
      boolean serialize = excludeField(field, true);
      boolean deserialize = excludeField(field, false);
      if (!serialize && !deserialize) {
        continue;
      }
      field.setAccessible(true);
      Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
      BoundField 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
            + " declares multiple JSON fields named " + previous.name);
      }
    }
    type = TypeToken.get($Gson$Types.resolve(type.getType(), raw, raw.getGenericSuperclass()));
    raw = type.getRawType();
  }
  return result;
}
 
开发者ID:odoo-mobile-intern,项目名称:odoo-work,代码行数:31,代码来源:ReflectiveTypeAdapterFactory.java

示例7: scanFields

import com.google.gson.reflect.TypeToken; //导入方法依赖的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

示例8: typeName

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
public PolymorphicTypeAdapterFactory typeName(TypeToken<?> type, String name) {
  typeNames.put(type, name);

  matchingTypes.add(type);
  Class<?> rawClass = type.getRawType();
  TypeToken<?> rawType = TypeToken.get(rawClass);
  if (!rawType.equals(type)) {
    matchingTypes.add(rawType);
  }

  return this;
}
 
开发者ID:rockscript,项目名称:rockscript,代码行数:13,代码来源:PolymorphicTypeAdapterFactory.java

示例9: create

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
  // TODO check if GSON does caching of the created TypeAdapter for the given type

  // extra caching could be added in this layer if there is only one polymorphic type
  // adapter for the whole hierarchy

  // https://google.github.io/gson/apidocs/com/google/gson/TypeAdapterFactory.html
  // If a factory cannot support a given type, it must return null when that type is passed to create(com.google.gson.Gson, com.google.gson.reflect.TypeToken<T>)

  if (type.getType() instanceof WildcardType) {
    WildcardType wildcardType = (WildcardType) type.getType();
    Type[] upperBounds = wildcardType.getUpperBounds();
    if (upperBounds!=null && upperBounds.length==1) {
      type = (TypeToken<T>) TypeToken.get(upperBounds[0]);
    } else {
      throw new RuntimeException("Unsupported wildcard type: "+type);
    }
  }
  if (matchingTypes.contains(type)) {
    if (typeAdapter==null) {
      typeAdapter = new PolymorphicTypeAdapter<T>(type, this, gson);
    }
    return (TypeAdapter<T>) this.typeAdapter;
  }

  return null;
}
 
开发者ID:rockscript,项目名称:rockscript,代码行数:30,代码来源:PolymorphicTypeAdapterFactory.java

示例10: getBoundFields

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
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;
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:28,代码来源:ReflectiveTypeAdapterFactory.java

示例11: getTypeToken

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
protected TypeToken<?> getTypeToken(Type type) {
  return TypeToken.get(type);
}
 
开发者ID:Huawei,项目名称:Server_Management_Common_eSightApi,代码行数:4,代码来源:GsonHttpMessageConverter.java

示例12: getDataType

import com.google.gson.reflect.TypeToken; //导入方法依赖的package包/类
@Override
protected TypeToken<String> getDataType() {
    return TypeToken.get(String.class);
}
 
开发者ID:tterrag1098,项目名称:MCBot,代码行数:5,代码来源:CommandPrefix.java


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