本文整理汇总了Java中com.google.gson.TypeAdapter类的典型用法代码示例。如果您正苦于以下问题:Java TypeAdapter类的具体用法?Java TypeAdapter怎么用?Java TypeAdapter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeAdapter类属于com.google.gson包,在下文中一共展示了TypeAdapter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: customDecoder
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Test
public void customDecoder() throws Exception {
GsonDecoder decoder = new GsonDecoder(Arrays.<TypeAdapter<?>>asList(upperZone));
List<Zone> zones = new LinkedList<Zone>();
zones.add(new Zone("DENOMINATOR.IO."));
zones.add(new Zone("DENOMINATOR.IO.", "ABCD"));
Response response =
Response.builder()
.status(200)
.reason("OK")
.headers(Collections.<String, Collection<String>>emptyMap())
.body(zonesJson, UTF_8)
.build();
assertEquals(zones, decoder.decode(response, new TypeToken<List<Zone>>() {
}.getType()));
}
示例2: writable
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> Range<Comparable<Object>> writable(TypeAdapter<T> adapter, Range<T> range) {
if (range.hasLowerBound() && range.hasUpperBound()) {
return Range.range(
(Comparable<Object>) writable(adapter, range.lowerEndpoint()),
range.lowerBoundType(),
(Comparable<Object>) writable(adapter, range.upperEndpoint()),
range.upperBoundType());
} else if (range.hasLowerBound()) {
return Range.downTo(
(Comparable<Object>) writable(adapter, range.lowerEndpoint()),
range.lowerBoundType());
} else if (range.hasUpperBound()) {
return Range.upTo(
(Comparable<Object>) writable(adapter, range.upperEndpoint()),
range.upperBoundType());
}
throw new AssertionError();
}
示例3: create
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
if (typeToken.getRawType() != Timestamp.class) {
return null;
}
final TypeAdapter<Date> dateTypeAdapter = gson.getAdapter(Date.class);
return (TypeAdapter<T>) new TypeAdapter<Timestamp>() {
@Override public Timestamp read(JsonReader in) throws IOException {
Date date = dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
@Override public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例4: getTypeAdapter
import com.google.gson.TypeAdapter; //导入依赖的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.");
}
示例5: create
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();
TypeAdapter<Map> mapAdapter = gson.getAdapter(Map.class);
TypeAdapter<Collection> collectionAdapter = gson.getAdapter(Collection.class);
if (Location.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new LocationTypeAdapter();
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new ConfigurationSerializableAdapter(mapAdapter);
} else if (GUISignature.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new GUISignatureTypeAdapter(mapAdapter, collectionAdapter);
} else if (MaterialAndData.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new MaterialAndDataAdapter();
}
return null;
}
示例6: assertRegisterTypeAdapter
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Test
public void assertRegisterTypeAdapter() {
Gson beforeRegisterGson = GsonFactory.getGson();
GsonFactory.registerTypeAdapter(GsonFactoryTest.class, new TypeAdapter() {
@Override
public Object read(final JsonReader in) throws IOException {
return null;
}
@Override
public void write(final JsonWriter out, final Object value) throws IOException {
out.jsonValue("test");
}
});
assertThat(beforeRegisterGson.toJson(new GsonFactoryTest()), is("{}"));
assertThat(GsonFactory.getGson().toJson(new GsonFactoryTest()), is("test"));
}
示例7: write
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override public void write(JsonWriter out, Object value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
if (typeAdapter instanceof ObjectTypeAdapter) {
out.beginObject();
out.endObject();
return;
}
typeAdapter.write(out, value);
}
示例8: gatherParsers
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public void gatherParsers(GsonBuilder builder) {
builder.registerTypeHierarchyAdapter(TrickType.class, new TypeAdapter<TrickType>() {
@Override
public TrickType read(JsonReader in) throws IOException {
TrickType type = TrickType.byId.get(in.nextString());
if (type == null) {
return TrickType.STRING;
}
return type;
}
@Override
public void write(JsonWriter out, TrickType value) throws IOException {
out.value(value.getId());
}
});
}
示例9: newFactory
import com.google.gson.TypeAdapter; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(
final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == unboxed || rawType == boxed) ? (TypeAdapter<T>) typeAdapter : null;
}
@Override public String toString() {
return "Factory[type=" + boxed.getName()
+ "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例10: getFieldAdapter
import com.google.gson.TypeAdapter; //导入依赖的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);
}
示例11: create
import com.google.gson.TypeAdapter; //导入依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (type.getRawType() == Object.class) {
return (TypeAdapter<T>) new NumberPredictObjectTypeAdapter(gson);
}
return null;
}
示例12: create
import com.google.gson.TypeAdapter; //导入依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return null;
}
Type elementType = C$Gson$Types.getCollectionElementType(type, rawType);
return new Adapter(gson, elementType, gson.getAdapter(TypeToken.get(elementType)), this
.constructorConstructor.get(typeToken));
}
示例13: newFactoryForMultipleTypes
import com.google.gson.TypeAdapter; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(final Class<TT> base,
final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == base || rawType == sub) ? (TypeAdapter<T>) typeAdapter : null;
}
@Override public String toString() {
return "Factory[type=" + base.getName()
+ "+" + sub.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例14: mutationSetAdapter
import com.google.gson.TypeAdapter; //导入依赖的package包/类
private <T> TypeAdapter<T> mutationSetAdapter(TypeAdapter<T> delegate) {
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@SuppressWarnings("unchecked")
public T read(JsonReader reader) throws IOException {
final TypeAdapter<ArrayList<Mutation>> arrayListAdapter =
gson.getAdapter(new TypeToken<ArrayList<Mutation>>(){});
return (T) new MutationSet(arrayListAdapter.read(reader));
}
};
}
示例15: lookupAdapters
import com.google.gson.TypeAdapter; //导入依赖的package包/类
private List<TypeAdapter<? extends T>> lookupAdapters() {
List<TypeAdapter<? extends T>> adapters = new ArrayList<>(subtypes.length);
for (TypeToken<? extends T> subtype : subtypes) {
adapters.add(gson.getAdapter(subtype));
}
return adapters;
}