下面列出了怎么用com.google.gson.internal.Primitives的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
@SuppressWarnings("unchecked")
public static<T> String getExpectedJson(MyParameterizedType<T> obj) {
Class<T> clazz = (Class<T>) obj.value.getClass();
boolean addQuotes = !clazz.isArray() && !Primitives.unwrap(clazz).isPrimitive();
StringBuilder sb = new StringBuilder("{\"");
sb.append(obj.value.getClass().getSimpleName()).append("\":");
if (addQuotes) {
sb.append("\"");
}
sb.append(obj.value.toString());
if (addQuotes) {
sb.append("\"");
}
sb.append("}");
return sb.toString();
}
@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);
}
/**
* Retrieving only one object
* @param key - Like saving preferences, this String is a key to save and get specific data
* @param objectModel - Base Class that you want to recover. For example String.class, or MyObject.class
* @return the saved object, or {@code null} if the object does not exists
*/
public <T> T retrieveModel(String key, Class<T> objectModel) {
String modelString = EasySaveUtil.retrieveData(key, context);
Object model = null;
try {
model = new Gson().fromJson(modelString, objectModel);
} catch (Exception e) {
e.printStackTrace();
}
return Primitives.wrap(objectModel).cast(model);
}
/**
* 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;
}
}
public Object fromJson(Reader reader, Class class1)
{
JsonReader jsonreader = new JsonReader(reader);
Object obj = fromJson(jsonreader, ((Type) (class1)));
a(obj, jsonreader);
return Primitives.wrap(class1).cast(obj);
}
private static void validateField(
String name,
Object object,
Field field,
Set<Field> ignoredFields) {
try {
field.setAccessible(true);
String fullName = name + "." + field.getName();
Object fieldValue = field.get(object);
boolean mustBeSet = !ignoredFields.contains(field);
if (mustBeSet) {
assertNotNull(fullName + " is null", fieldValue);
}
if (fieldValue != null) {
if (Primitives.isWrapperType(fieldValue.getClass())) {
// Special-case the mutable hash code field.
if (mustBeSet && !fullName.endsWith("cachedHashCode")) {
assertNotEquals(
"Primitive value must not be default: " + fullName,
Defaults.defaultValue(Primitives.unwrap(fieldValue.getClass())),
fieldValue);
}
} else {
assertFullyPopulated(fullName, fieldValue, ignoredFields);
}
}
} catch (IllegalAccessException e) {
throw Throwables.propagate(e);
}
}
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
return Primitives.wrap(classOfT).cast(fromJson(json, (Type) classOfT));
}
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
JsonReader jsonReader = new JsonReader(json);
Object object = fromJson(jsonReader, (Type) classOfT);
assertFullConsumption(object, jsonReader);
return Primitives.wrap(classOfT).cast(object);
}
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
return Primitives.wrap(classOfT).cast(fromJson(json, (Type) classOfT));
}
public <T> T get(Class<T> clazz) {
return Primitives.wrap(clazz).cast(r);
}
public Object fromJson(JsonElement jsonelement, Class class1)
{
Object obj = fromJson(jsonelement, ((Type) (class1)));
return Primitives.wrap(class1).cast(obj);
}
public Object fromJson(String s, Class class1)
{
Object obj = fromJson(s, ((Type) (class1)));
return Primitives.wrap(class1).cast(obj);
}
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()));
}
/**
* This method deserializes the Json read from the specified reader into an object of the
* specified class. It is not suitable to use if the specified class is a generic type since it
* will not have the generic type information because of the Type Erasure feature of Java.
* Therefore, this method should not be used if the desired type is a generic type. Note that
* this method works fine if the any of the fields of the specified object are generics, just the
* object itself should not be a generic type. For the cases when the object is of generic type,
* invoke {@link #fromJson(Reader, Type)}. If you have the Json in a String form instead of a
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
*
* @param <T> the type of the desired object
* @param json the reader producing the Json from which the object is to be deserialized.
* @param classOfT the class of T
* @return an object of type T from the string. Returns {@code null} if {@code json} is at EOF.
* @throws JsonIOException if there was a problem reading from the Reader
* @throws JsonSyntaxException if json is not a valid representation for an object of type
* @since 1.2
*/
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
JsonReader jsonReader = newJsonReader(json);
Object object = fromJson(jsonReader, classOfT);
assertFullConsumption(object, jsonReader);
return Primitives.wrap(classOfT).cast(object);
}
/**
* This method deserializes the Json read from the specified reader into an object of the
* specified class. It is not suitable to use if the specified class is a generic type since it
* will not have the generic type information because of the Type Erasure feature of Java.
* Therefore, this method should not be used if the desired type is a generic type. Note that
* this method works fine if the any of the fields of the specified object are generics, just the
* object itself should not be a generic type. For the cases when the object is of generic type,
* invoke {@link #fromJson(Reader, Type)}. If you have the Json in a String form instead of a
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
*
* @param <T> the type of the desired object
* @param json the reader producing the Json from which the object is to be deserialized.
* @param classOfT the class of T
* @return an object of type T from the string. Returns {@code null} if {@code json} is at EOF.
* @throws JsonIOException if there was a problem reading from the Reader
* @throws JsonSyntaxException if json is not a valid representation for an object of type
* @since 1.2
*/
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
JsonReader jsonReader = new JsonReader(json);
Object object = fromJson(jsonReader, classOfT);
assertFullConsumption(object, jsonReader);
return Primitives.wrap(classOfT).cast(object);
}
/**
* This method deserializes the specified Json into an object of the specified class. It is not
* suitable to use if the specified class is a generic type since it will not have the generic
* type information because of the Type Erasure feature of Java. Therefore, this method should not
* be used if the desired type is a generic type. Note that this method works fine if the any of
* the fields of the specified object are generics, just the object itself should not be a
* generic type. For the cases when the object is of generic type, invoke
* {@link #fromJson(String, Type)}. If you have the Json in a {@link Reader} instead of
* a String, use {@link #fromJson(Reader, Class)} instead.
*
* @param <T> the type of the desired object
* @param json the string from which the object is to be deserialized
* @param classOfT the class of T
* @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}
* or if {@code json} is empty.
* @throws JsonSyntaxException if json is not a valid representation for an object of type
* classOfT
*/
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
/**
* This method deserializes the Json read from the specified parse tree into an object of the
* specified type. It is not suitable to use if the specified class is a generic type since it
* will not have the generic type information because of the Type Erasure feature of Java.
* Therefore, this method should not be used if the desired type is a generic type. Note that
* this method works fine if the any of the fields of the specified object are generics, just the
* object itself should not be a generic type. For the cases when the object is of generic type,
* invoke {@link #fromJson(JsonElement, Type)}.
* @param <T> the type of the desired object
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
* be deserialized
* @param classOfT The class of T
* @return an object of type T from the json. Returns {@code null} if {@code json} is {@code null}
* or if {@code json} is empty.
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
* @since 1.3
*/
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
/**
* This method deserializes the specified Json into an object of the specified class. It is not
* suitable to use if the specified class is a generic type since it will not have the generic
* type information because of the Type Erasure feature of Java. Therefore, this method should not
* be used if the desired type is a generic type. Note that this method works fine if the any of
* the fields of the specified object are generics, just the object itself should not be a
* generic type. For the cases when the object is of generic type, invoke
* {@link #fromJson(String, Type)}. If you have the Json in a {@link Reader} instead of
* a String, use {@link #fromJson(Reader, Class)} instead.
*
* @param <T> the type of the desired object
* @param json the string from which the object is to be deserialized
* @param classOfT the class of T
* @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}.
* @throws JsonSyntaxException if json is not a valid representation for an object of type
* classOfT
*/
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
/**
* This method deserializes the Json read from the specified parse tree into an object of the
* specified type. It is not suitable to use if the specified class is a generic type since it
* will not have the generic type information because of the Type Erasure feature of Java.
* Therefore, this method should not be used if the desired type is a generic type. Note that
* this method works fine if the any of the fields of the specified object are generics, just the
* object itself should not be a generic type. For the cases when the object is of generic type,
* invoke {@link #fromJson(JsonElement, Type)}.
* @param <T> the type of the desired object
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
* be deserialized
* @param classOfT The class of T
* @return an object of type T from the json. Returns {@code null} if {@code json} is {@code null}.
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
* @since 1.3
*/
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}