本文整理汇总了Java中com.google.gson.internal.Primitives类的典型用法代码示例。如果您正苦于以下问题:Java Primitives类的具体用法?Java Primitives怎么用?Java Primitives使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Primitives类属于com.google.gson.internal包,在下文中一共展示了Primitives类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerTypeAdapter
import com.google.gson.internal.Primitives; //导入依赖的package包/类
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;
}
示例2: getExpectedJson
import com.google.gson.internal.Primitives; //导入依赖的package包/类
@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();
}
示例3: deserialize
import com.google.gson.internal.Primitives; //导入依赖的package包/类
@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);
}
示例4: checkGetter
import com.google.gson.internal.Primitives; //导入依赖的package包/类
private static void checkGetter(Method getter) {
checkNotNull(getter);
checkArgument(GETTER_PREFIX_PATTERN.matcher(getter.getName())
.find() && getter.getParameterTypes().length == 0,
"Method `%s` is not a getter.", getter);
checkArgument(getAnnotatedVersion(getter).isPresent(),
format("Entity column getter should be annotated with `%s`.",
Column.class.getName()));
final int modifiers = getter.getModifiers();
checkArgument(isPublic(modifiers) && !isStatic(modifiers),
"Entity column getter should be public instance method.");
final Class<?> returnType = getter.getReturnType();
final Class<?> wrapped = Primitives.wrap(returnType);
checkArgument(Serializable.class.isAssignableFrom(wrapped),
format("Cannot create column of non-serializable type %s by method %s.",
returnType,
getter));
}
示例5: FieldAttributeModel
import com.google.gson.internal.Primitives; //导入依赖的package包/类
/**
* 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";
} else if (this.type instanceof Class && ((Class) this.type).isEnum()) {
this.isEnum = true;
}
}
示例6: jsonToObject
import com.google.gson.internal.Primitives; //导入依赖的package包/类
public static <T> T jsonToObject(String jsonString, Class<T> classOfT) {
// Gson gson = new Gson();
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Date.class,
new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src,
Type typeOfSrc,
JsonSerializationContext context) {
SimpleDateFormat format = new SimpleDateFormat(
dateformat);
return new JsonPrimitive(format.format(src));
}
}).setDateFormat(dateformat).create();
Object object = gson.fromJson(jsonString, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
示例7: registerTypeAdapter
import com.google.gson.internal.Primitives; //导入依赖的package包/类
/**
* Configures Gson for custom serialization or deserialization. This method combines the
* registration of an {@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 an instance creator,
* serializer or deserializer was previously registered for the specified {@code type}, it is
* overwritten.
*
* @param type the type definition for the type adapter being registered
* @param typeAdapter This object must implement at least one of the {@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 (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
throw new IllegalArgumentException(
"Cannot register type adapters for " + type);
}
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;
}
示例8: registerTypeAdapter
import com.google.gson.internal.Primitives; //导入依赖的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.
*
* @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 (Primitives.isPrimitive(type) || Primitives.isWrapperType(type)) {
throw new IllegalArgumentException(
"Cannot register type adapters for " + type);
}
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;
}
示例9: deserialize
import com.google.gson.internal.Primitives; //导入依赖的package包/类
@SuppressWarnings("unchecked")
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);
}
示例10: read
import com.google.gson.internal.Primitives; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public T read() throws JsonSyntaxException, JsonIOException {
if (Primitives.isPrimitive(valueType)) {
Object parsed = mapper.fromJson(source, valueType);
return (T) Primitives.wrap(parsed.getClass()).cast(parsed);
}
else {
return mapper.fromJson(source, valueType);
}
}
示例11: read
import com.google.gson.internal.Primitives; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public T read(Reader source) throws JsonSyntaxException, JsonIOException {
requireNonNull(source, "source");
if (Primitives.isPrimitive(valueType)) {
Object parsed = mapper.fromJson(source, valueType);
return (T) Primitives.wrap(parsed.getClass()).cast(parsed);
}
else {
return mapper.fromJson(source, valueType);
}
}
示例12: fromJson
import com.google.gson.internal.Primitives; //导入依赖的package包/类
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);
}
示例13: validateField
import com.google.gson.internal.Primitives; //导入依赖的package包/类
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);
}
}
示例14: fromJson
import com.google.gson.internal.Primitives; //导入依赖的package包/类
public final <T> T fromJson(String paramString, Class<T> paramClass)
throws JsonSyntaxException
{
Object localObject;
if (paramString == null) {
localObject = null;
}
for (;;)
{
return Primitives.wrap(paramClass).cast(localObject);
JsonReader localJsonReader = new JsonReader(new StringReader(paramString));
localObject = fromJson(localJsonReader, paramClass);
if (localObject == null) {
continue;
}
try
{
if (localJsonReader.peek() == JsonToken.END_DOCUMENT) {
continue;
}
throw new JsonIOException("JSON document was not fully consumed.");
}
catch (MalformedJsonException localMalformedJsonException)
{
throw new JsonSyntaxException(localMalformedJsonException);
}
catch (IOException localIOException)
{
throw new JsonIOException(localIOException);
}
}
}
示例15: fromJson
import com.google.gson.internal.Primitives; //导入依赖的package包/类
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);
}