示例#1
0
 public <T> T fromJson(Reader paramReader, Class<T> paramClass)
     throws JsonSyntaxException, JsonIOException {
   paramReader = new JsonReader(paramReader);
   Object localObject = fromJson(paramReader, paramClass);
   assertFullConsumption(localObject, paramReader);
   return Primitives.wrap(paramClass).cast(localObject);
 }
示例#2
0
 public <T> T fromXml(final Reader json, final Class<T> classOfT)
     throws JsonSyntaxException, JsonIOException {
   final XmlReader jsonReader = new XmlReader(json, xmlParserCreator, options); // change reader
   final Object object = fromXml(jsonReader, classOfT);
   assertFullConsumption(object, jsonReader);
   return Primitives.wrap(classOfT).cast(object);
 }
示例#3
0
  /**
   * 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
   */
  public FieldAttributeModel(String fieldName, Type type) {
    this.fieldName = fieldName;
    this.type = type;
    this.typeName = convertType(type);

    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";
    }
  }
  private ReflectiveTypeAdapterFactory.BoundField createBoundField(
      final Gson context,
      final Field field,
      final String name,
      final TypeToken<?> fieldType,
      boolean serialize,
      boolean deserialize) {
    final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());
    // special casing primitives here saves ~5% on Android...
    JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
    TypeAdapter<?> mapped = null;
    if (annotation != null) {
      mapped =
          jsonAdapterFactory.getTypeAdapter(constructorConstructor, context, fieldType, annotation);
    }
    final boolean jsonAdapterPresent = mapped != null;
    if (mapped == null) mapped = context.getAdapter(fieldType);

    final TypeAdapter<?> typeAdapter = mapped;
    return new ReflectiveTypeAdapterFactory.BoundField(name, serialize, deserialize) {
      @SuppressWarnings({"unchecked", "rawtypes"}) // the type adapter and field type always agree
      @Override
      void write(JsonWriter writer, Object value) throws IOException, IllegalAccessException {
        Object fieldValue = field.get(value);
        TypeAdapter t =
            jsonAdapterPresent
                ? typeAdapter
                : new TypeAdapterRuntimeTypeWrapper(context, typeAdapter, fieldType.getType());
        t.write(writer, fieldValue);
      }

      @Override
      void read(JsonReader reader, Object value) throws IOException, IllegalAccessException {
        Object fieldValue = typeAdapter.read(reader);
        if (fieldValue != null || !isPrimitive) {
          field.set(value, fieldValue);
        }
      }

      @Override
      public boolean writeField(Object value) throws IOException, IllegalAccessException {
        if (!serialized) return false;
        Object fieldValue = field.get(value);
        return fieldValue != value; // avoid recursion for example for Throwable.cause
      }
    };
  }
示例#5
0
 public <T> T fromJson(JsonElement jsonElement, Class<T> cls) throws JsonSyntaxException {
   return Primitives.wrap(cls).cast(fromJson(jsonElement, (Type) cls));
 }
示例#6
0
 public <T> T fromJson(Reader reader, Class<T> cls) throws JsonSyntaxException, JsonIOException {
   JsonReader jsonReader = new JsonReader(reader);
   Object fromJson = fromJson(jsonReader, (Type) cls);
   assertFullConsumption(fromJson, jsonReader);
   return Primitives.wrap(cls).cast(fromJson);
 }
示例#7
0
 public <T> T fromJson(String str, Class<T> cls) throws JsonSyntaxException {
   return Primitives.wrap(cls).cast(fromJson(str, (Type) cls));
 }
示例#8
0
 public <T> T fromJson(String paramString, Class<T> paramClass) throws JsonSyntaxException {
   paramString = fromJson(paramString, paramClass);
   return Primitives.wrap(paramClass).cast(paramString);
 }
示例#9
0
 public <T> T fromJson(JsonElement paramJsonElement, Class<T> paramClass)
     throws JsonSyntaxException {
   paramJsonElement = fromJson(paramJsonElement, paramClass);
   return Primitives.wrap(paramClass).cast(paramJsonElement);
 }
示例#10
0
 public <T> T fromXml(final String json, final Class<T> classOfT) throws JsonSyntaxException {
   final Object object = fromXml(json, (Type) classOfT);
   return Primitives.wrap(classOfT).cast(object);
 }