Java Code Examples for com.google.gson.internal.Primitives#isPrimitive()

The following examples show how to use com.google.gson.internal.Primitives#isPrimitive() . 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: GsonBuilder.java    From letv with Apache License 2.0 6 votes vote down vote up
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
    boolean z = (typeAdapter instanceof JsonSerializer) || (typeAdapter instanceof JsonDeserializer) || (typeAdapter instanceof InstanceCreator) || (typeAdapter instanceof TypeAdapter);
    C$Gson$Preconditions.checkArgument(z);
    if (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
        throw new IllegalArgumentException("Cannot register type adapters for " + type);
    }
    if (typeAdapter instanceof InstanceCreator) {
        this.instanceCreators.put(type, (InstanceCreator) typeAdapter);
    }
    if ((typeAdapter instanceof JsonSerializer) || (typeAdapter instanceof JsonDeserializer)) {
        this.factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(TypeToken.get(type), typeAdapter));
    }
    if (typeAdapter instanceof TypeAdapter) {
        this.factories.add(TypeAdapters.newFactory(TypeToken.get(type), (TypeAdapter) typeAdapter));
    }
    return this;
}
 
Example 2
Source File: ParameterizedTypeFixtures.java    From gson with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override public MyParameterizedType<T> deserialize(JsonElement json, Type typeOfT,
    JsonDeserializationContext context) throws JsonParseException {
  Type genericClass = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
  Class<?> rawType = $Gson$Types.getRawType(genericClass);
  String className = rawType.getSimpleName();
  JsonElement jsonElement = json.getAsJsonObject().get(className);

  T value;
  if (genericClass == Integer.class) {
    value = (T) Integer.valueOf(jsonElement.getAsInt());
  } else if (genericClass == String.class) {
    value = (T) jsonElement.getAsString();
  } else {
    value = (T) jsonElement;
  }

  if (Primitives.isPrimitive(genericClass)) {
    PrimitiveTypeAdapter typeAdapter = new PrimitiveTypeAdapter();
    value = (T) typeAdapter.adaptType(value, rawType);
  }
  return new MyParameterizedType<T>(value);
}
 
Example 3
Source File: FieldAttributeModel.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Build a new field model based on the name and Java type
 *
 * @param fieldName the name of the field
 * @param type the Java raw type that will allow further analyzes
 * @param declarationClass
 */
public FieldAttributeModel(String fieldName, Type type, Class declarationClass) {
  this.fieldName = fieldName;
  this.type = type;
  this.typeName = convertType(type);
  this.dtsType = convertTypeForDTS(declarationClass, type);
  this.declarationClass = declarationClass;

  if (typeName.startsWith("Array<") || typeName.startsWith("Map<")) {
    this.needInitialize = true;
  }

  if (this.type instanceof ParameterizedType) {
    ParameterizedType parameterizedType = (ParameterizedType) this.type;
    Type rawType = parameterizedType.getRawType();
    analyzeParametrizedType(parameterizedType, rawType);
  } else if (Primitives.isPrimitive(this.type)
      || Primitives.isWrapperType(this.type)
      || String.class.equals(this.type)) {
    this.isPrimitive = true;
  } else if (this.type instanceof Class && ((Class) this.type).isAnnotationPresent(DTO.class)) {
    this.isDto = true;
    dtoImpl = this.type.getTypeName() + "Impl";
  } else if (this.type instanceof Class && ((Class) this.type).isEnum()) {
    this.isEnum = true;
  }
}
 
Example 4
Source File: ReflectiveTypeAdapterFactory.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
private j a(Gson gson, Field field, String s, TypeToken typetoken, boolean flag, boolean flag1)
{
    return new i(this, s, flag, flag1, gson, typetoken, field, Primitives.isPrimitive(typetoken.getRawType()));
}