本文整理汇总了Java中com.google.gson.Gson.getDelegateAdapter方法的典型用法代码示例。如果您正苦于以下问题:Java Gson.getDelegateAdapter方法的具体用法?Java Gson.getDelegateAdapter怎么用?Java Gson.getDelegateAdapter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.Gson
的用法示例。
在下文中一共展示了Gson.getDelegateAdapter方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import com.google.gson.Gson; //导入方法依赖的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;
}
};
}
示例2: create
import com.google.gson.Gson; //导入方法依赖的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;
}
};
}
示例3: create
import com.google.gson.Gson; //导入方法依赖的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;
}
};
}
示例4: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegateAdapter.write(out, value);
}
@Override
@SuppressWarnings("PointlessBooleanExpression")
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = jsonElementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has(ERROR) && jsonObject.get(ERROR).getAsBoolean() == false) {
if (jsonObject.has(DATA) && isJson(jsonObject, DATA)) {
jsonElement = jsonObject.get(DATA);
}
}
}
return delegateAdapter.fromJsonTree(jsonElement);
}
}.nullSafe();
}
示例5: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
Class<?> rawType = type.getRawType();
final boolean skipSerialize = excludeClass(rawType, true);
final boolean skipDeserialize = excludeClass(rawType, false);
if (!skipSerialize && !skipDeserialize) {
return null;
}
return new TypeAdapter<T>() {
/** The delegate is lazily created because it may not be needed, and creating it may fail. */
private TypeAdapter<T> delegate;
@Override public T read(JsonReader in) throws IOException {
if (skipDeserialize) {
in.skipValue();
return null;
}
return delegate().read(in);
}
@Override public void write(JsonWriter out, T value) throws IOException {
if (skipSerialize) {
out.nullValue();
return;
}
delegate().write(out, value);
}
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(Excluder.this, type));
}
};
}
示例6: create
import com.google.gson.Gson; //导入方法依赖的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;
}
};
}
示例7: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<?> rawType = type.getRawType();
final boolean skipSerialize = excludeClass(rawType, true);
final boolean skipDeserialize = excludeClass(rawType, false);
if (!skipSerialize && !skipDeserialize) {
return null;
}
final Gson gson2 = gson;
final TypeToken<T> typeToken = type;
return new TypeAdapter<T>() {
private TypeAdapter<T> delegate;
public T read(JsonReader in) throws IOException {
if (!skipDeserialize) {
return delegate().read(in);
}
in.skipValue();
return null;
}
public void write(JsonWriter out, T value) throws IOException {
if (skipSerialize) {
out.nullValue();
} else {
delegate().write(out, value);
}
}
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = this.delegate;
if (d != null) {
return d;
}
d = gson2.getDelegateAdapter(Excluder.this, typeToken);
this.delegate = d;
return d;
}
};
}
示例8: create
import com.google.gson.Gson; //导入方法依赖的package包/类
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
Type classType = type.getType();
if (classType == MutationSet.class) {
return mutationSetAdapter(delegate);
}
else if (classType == GenePosition.class) {
return genePositionAdapter(delegate);
}
return delegate;
}
示例9: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementTypeAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@Override
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementTypeAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("status")) {
int status = jsonObject.get("status").getAsInt();
String message = jsonObject.get("message").getAsString();
if (status == 0) {
//do nothing
} else {
throw new ApiException(status, message);
}
}
if (jsonObject.has(dataElementName)) {
jsonElement = jsonObject.get(dataElementName);
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
示例10: create
import com.google.gson.Gson; //导入方法依赖的package包/类
@Override
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new MyTypeAdapter<T>(delegate, gson);
}