當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonAdapter類代碼示例

本文整理匯總了Java中com.google.gson.annotations.JsonAdapter的典型用法代碼示例。如果您正苦於以下問題:Java JsonAdapter類的具體用法?Java JsonAdapter怎麽用?Java JsonAdapter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JsonAdapter類屬於com.google.gson.annotations包,在下文中一共展示了JsonAdapter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getTypeAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  if (TypeAdapter.class.isAssignableFrom(value)) {
        Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
  }
  if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  }

  throw new IllegalArgumentException(
      "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
 
開發者ID:odoo-mobile-intern,項目名稱:odoo-work,代碼行數:19,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例2: getTypeAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> fieldType, JsonAdapter annotation) {
  Class<?> value = annotation.value();
  TypeAdapter<?> typeAdapter;
  if (TypeAdapter.class.isAssignableFrom(value)) {
    Class<TypeAdapter<?>> typeAdapterClass = (Class<TypeAdapter<?>>) value;
    typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterClass)).construct();
  } else if (TypeAdapterFactory.class.isAssignableFrom(value)) {
    Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
    typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterFactory))
        .construct()
        .create(gson, fieldType);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
  }
  if (typeAdapter != null) {
    typeAdapter = typeAdapter.nullSafe();
  }
  return typeAdapter;
}
 
開發者ID:MyJojoX,項目名稱:MyJojoXUtils,代碼行數:23,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例3: serializeConfigurableProperties

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
private void serializeConfigurableProperties(JsonObject configJsonObject, Object configInstance, JsonSerializationContext jsonSerializationContext) {
    List<Field> configurableFields = Arrays.stream(configInstance.getClass().getFields())
            .filter(field -> field.getAnnotation(ConfigurableProperty.class) != null).collect(Collectors.toList());

    configurableFields.forEach(field -> {
        String fieldName = determineNameToUseForField(field);
        Object fieldValue = ReflectionUtils.getValue(field, configInstance);
        if (fieldValue != null) {
            JsonElement fieldJsonObject;
            if (field.getAnnotation(JsonAdapter.class) != null) {
                try {
                    JsonSerializer customSerializer = (JsonSerializer) field.getAnnotation(JsonAdapter.class).value().newInstance();
                    fieldJsonObject = customSerializer.serialize(fieldValue, field.getType(), jsonSerializationContext);
                } catch (InstantiationException | IllegalAccessException e) {
                    throw new RuntimeReflectiveOperationException(e);
                }
            } else {
                fieldJsonObject = jsonSerializationContext.serialize(fieldValue);
            }
            configJsonObject.add(fieldName, fieldJsonObject);
        }
    });
}
 
開發者ID:vmware,項目名稱:workflowTools,代碼行數:24,代碼來源:WorkflowConfigMapper.java

示例4: create

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
開發者ID:odoo-mobile-intern,項目名稱:odoo-work,代碼行數:9,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例5: getFieldAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}
 
開發者ID:odoo-mobile-intern,項目名稱:odoo-work,代碼行數:9,代碼來源:ReflectiveTypeAdapterFactory.java

示例6: create

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
    JsonAdapter annotation = (JsonAdapter) targetType.getRawType().getAnnotation(JsonAdapter
            .class);
    if (annotation == null) {
        return null;
    }
    return getTypeAdapter(this.constructorConstructor, gson, targetType, annotation);
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:9,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例7: getTypeAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson
        gson, TypeToken<?> fieldType, JsonAdapter annotation) {
    Class<?> value = annotation.value();
    if (TypeAdapter.class.isAssignableFrom(value)) {
        return (TypeAdapter) constructorConstructor.get(TypeToken.get((Class) value))
                .construct();
    }
    if (TypeAdapterFactory.class.isAssignableFrom(value)) {
        return ((TypeAdapterFactory) constructorConstructor.get(TypeToken.get((Class) value))
                .construct()).create(gson, fieldType);
    }
    throw new IllegalArgumentException("@JsonAdapter value must be TypeAdapter or " +
            "TypeAdapterFactory reference.");
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:15,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例8: getFieldAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
    JsonAdapter annotation = (JsonAdapter) field.getAnnotation(JsonAdapter.class);
    if (annotation != null) {
        TypeAdapter<?> adapter = JsonAdapterAnnotationTypeAdapterFactory.getTypeAdapter(this
                .constructorConstructor, gson, fieldType, annotation);
        if (adapter != null) {
            return adapter;
        }
    }
    return gson.getAdapter((TypeToken) fieldType);
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:12,代碼來源:ReflectiveTypeAdapterFactory.java

示例9: create

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
開發者ID:MyJojoX,項目名稱:MyJojoXUtils,代碼行數:10,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例10: getFieldAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}
 
開發者ID:MyJojoX,項目名稱:MyJojoXUtils,代碼行數:9,代碼來源:ReflectiveTypeAdapterFactory.java

示例11: create

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
開發者ID:PhilipShilling,項目名稱:SteamLib,代碼行數:11,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例12: getTypeAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
 
開發者ID:PhilipShilling,項目名稱:SteamLib,代碼行數:31,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例13: getTypeAdapter

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException(
        "@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
            + "JsonSerializer or JsonDeserializer reference.");
  }

  if (typeAdapter != null && annotation.nullSafe()) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
 
開發者ID:ASL-Robotics,項目名稱:1797-2017,代碼行數:31,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java

示例14: deserialize

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@Override
public WorkflowConfig deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    Class typeClass = (Class) type;
    List<Field> additionalConfigFields = Arrays.stream(typeClass.getFields())
            .filter(field -> field.getAnnotation(SectionConfig.class) != null).collect(Collectors.toList());

    WorkflowConfig config = (WorkflowConfig) ReflectionUtils.newInstance(typeClass);

    List<Field> configurableFields = Arrays.stream(typeClass.getFields())
            .filter(field -> field.getAnnotation(ConfigurableProperty.class) != null).collect(Collectors.toList());

    JsonObject configJsonObject = jsonElement.getAsJsonObject();
    configurableFields.forEach(field -> {
        String fieldName = determineNameToUseForField(field);
        JsonElement fieldJsonObject = configJsonObject.get(fieldName);
        if (fieldJsonObject != null) {
            Object fieldValue;
            if (field.getAnnotation(JsonAdapter.class) != null) {
                JsonDeserializer customDeserializer = (JsonDeserializer) ReflectionUtils.newInstance(field.getAnnotation(JsonAdapter.class).value());
                fieldValue = customDeserializer.deserialize(fieldJsonObject, field.getType(), jsonDeserializationContext);
            } else {
                fieldValue = jsonDeserializationContext.deserialize(fieldJsonObject, field.getType());
            }
            setFieldValue(field, config, fieldValue);
        }
    });

    additionalConfigFields.forEach(field -> {
        Object fieldConfig = jsonDeserializationContext.deserialize(jsonElement, field.getType());
        setFieldValue(field, config, fieldConfig);
    });
    return config;
}
 
開發者ID:vmware,項目名稱:workflowTools,代碼行數:34,代碼來源:WorkflowConfigMapper.java

示例15: create

import com.google.gson.annotations.JsonAdapter; //導入依賴的package包/類
@SuppressWarnings({"rawtypes", "unchecked"})
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
開發者ID:dobrakmato,項目名稱:StaticMC,代碼行數:9,代碼來源:JsonAdapterAnnotationTypeAdapterFactory.java


注:本文中的com.google.gson.annotations.JsonAdapter類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。