本文整理汇总了Java中com.google.gson.TypeAdapter.read方法的典型用法代码示例。如果您正苦于以下问题:Java TypeAdapter.read方法的具体用法?Java TypeAdapter.read怎么用?Java TypeAdapter.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.TypeAdapter
的用法示例。
在下文中一共展示了TypeAdapter.read方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decoderFor
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
/**
* Creates a "reader" which is able to deserialize binary data into a immutables instance (using GSON type adapter).
*/
public static <T> Decoder<T> decoderFor(final Class<T> type, final TypeAdapter<T> adapter) {
checkNotNull(type, "type");
checkNotNull(adapter, "adapter");
return new Decoder<T>() {
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
if (!(reader instanceof AbstractBsonReader)) {
throw new UnsupportedOperationException(String.format("Only readers of type %s supported. Yours is %s",
AbstractBsonReader.class.getName(), reader.getClass().getName()));
}
try {
return adapter.read(new org.immutables.mongo.repository.internal.BsonReader((AbstractBsonReader) reader));
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't encode %s", type), e);
}
}
};
}
示例2: read
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public T read(JsonReader in) throws IOException {
List<Exception> exceptions = new ArrayList<>(subtypes.length);
ReaderSupplier readerSupplier = readForSupplier(in);
for (TypeAdapter<?> typeAdapter : adapters) {
try {
return (T) typeAdapter.read(readerSupplier.create());
} catch (Exception ex) {
exceptions.add(ex);
}
}
JsonParseException failure = new JsonParseException(
String.format(
"Cannot parse %s with following subtypes: %s",
type,
Arrays.toString(subtypes)));
for (Exception exception : exceptions) {
failure.addSuppressed(exception);
}
throw failure;
}
示例3: create
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
T obj = delegate.read(in);
if (obj instanceof PostProcessable) {
((PostProcessable) obj).postProcess();
}
return obj;
}
};
}
示例4: create
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
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 new TypeAdapter<Timestamp>() {
public Timestamp read(JsonReader in) throws IOException {
Date date = (Date) dateTypeAdapter.read(in);
return date != null ? new Timestamp(date.getTime()) : null;
}
public void write(JsonWriter out, Timestamp value) throws IOException {
dateTypeAdapter.write(out, value);
}
};
}
示例5: 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);
}
};
}
示例6: create
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
public T read(JsonReader in) throws IOException {
T obj = delegate.read(in);
if (obj instanceof PostProcessable) {
((PostProcessable)obj).postProcess();
}
return obj;
}
};
}
示例7: create
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> tokenType) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, tokenType);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@SuppressWarnings("unchecked")
@Override
public T read(JsonReader arg0) throws IOException {
T t = delegate.read(arg0);
if (List.class.isAssignableFrom(tokenType.getRawType())) {
List<?> list = (List<?>) t;
return (T) Collections.unmodifiableList(list);
}
return t;
}
};
}
示例8: create
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
@SuppressWarnings("unchecked")
Class<T> rawType = (Class<T>) type.getRawType();
if (rawType != Post.class) {
return null;
}
final TypeAdapter delegate = gson.getDelegateAdapter(this, type);
//noinspection unchecked
return (TypeAdapter<T>) new TypeAdapter<Post>() {
@Override
public void write(JsonWriter out, Post value) throws IOException {
//noinspection unchecked
delegate.write(out, value);
}
@Override
public Post read(JsonReader in) throws IOException {
Post post = (Post) delegate.read(in);
// Empty posts imported from Ghost 0.11.x have mobiledoc == null, which is incorrect
// but we do need to handle it. Drafts created in Ghost 1.0 on the other hand, do
// have mobiledoc set to a valid, empty mobiledoc document.
if (post.getMobiledoc() != null && !post.getMobiledoc().isEmpty()) {
// Post JSON example:
// {
// "mobiledoc": "{\"version\": \"0.3.1\", ... }",
// ...
// }
post.setMarkdown(GhostApiUtils.mobiledocToMarkdown(post.getMobiledoc()));
} else {
post.setMarkdown("");
}
return post;
}
};
}
示例9: 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));
}
};
}
示例10: genePositionAdapter
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
private <T> TypeAdapter<T> genePositionAdapter(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<String> stringAdapter =
gson.getAdapter(new TypeToken<String>(){});
return (T) new GenePosition(stringAdapter.read(reader));
}
};
}
示例11: read
import com.google.gson.TypeAdapter; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public T read(JsonReader in) throws IOException {
Object res;
in.beginObject();
LOG.info(in.nextName());
String clazzName = in.nextString();
LOG.info(clazzName);
LOG.info(in.nextName());
try {
Class<?> typeOfT = Class.forName(clazzName);
// TypeAdapter<?> del = gson.getDelegateAdapter(new ObjectTypeAdapterFactory(),
// );
TypeAdapter<?> del = gson.getAdapter(TypeToken.get(typeOfT));
LOG.info("ta: " + del + " taDel: " + delegate + " tt: " + TypeToken.get(typeOfT));
if (typeOfT.isArray()) {
del = com.google.gson.internal.bind.ArrayTypeAdapter.FACTORY.create(gson, TypeToken.get(typeOfT));
} else {
del = delegate;
}
JsonToken peek = in.peek();
switch (peek) {
case STRING:
res = in.nextString();
break;
case BOOLEAN:
res = in.nextBoolean();
break;
case NUMBER:
if (Long.class.equals(typeOfT)) {
res = in.nextLong();
} else if (Integer.class.equals(typeOfT)) {
res = in.nextInt();
} else {
res = in.nextDouble();
}
break;
default:
res = (T) del.read(in);
}
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
// res = delegate.read(in);
in.endObject();
return (T) res;
}