本文整理匯總了Java中com.google.gson.TypeAdapter.write方法的典型用法代碼示例。如果您正苦於以下問題:Java TypeAdapter.write方法的具體用法?Java TypeAdapter.write怎麽用?Java TypeAdapter.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.gson.TypeAdapter
的用法示例。
在下文中一共展示了TypeAdapter.write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encoderFor
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
/**
* Creates "writer" which can serialize existing immutable instance into <a href="http://bsonspec.org/">BSON format</a>
* consumed by mongo server.
*/
public static <T> Encoder<T> encoderFor(final Class<T> type, final TypeAdapter<T> adapter) {
checkNotNull(type, "type");
checkNotNull(adapter, "adapter");
return new Encoder<T>() {
@Override
public void encode(org.bson.BsonWriter writer, T value, EncoderContext encoderContext) {
try {
adapter.write(new org.immutables.mongo.repository.internal.BsonWriter(writer), value);
} catch (IOException e) {
throw new RuntimeException(String.format("Couldn't write value of class %s: %s", type.getName(), value),e);
}
}
@Override
public Class<T> getEncoderClass() {
return type;
}
};
}
示例2: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
@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 NumberPredictObjectTypeAdapter) {
out.beginObject();
out.endObject();
return;
}
typeAdapter.write(out, value);
}
示例3: 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);
}
};
}
示例4: 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);
}
示例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包/類
@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;
}
};
}
示例7: 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;
}
};
}
示例8: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
@Override
public void write(JsonWriter out, GsonProperty property, Object value) throws IOException {
final Class<?> simpleType = property.getType();
out.beginObject();
if(value != null) {
final SparseArray sa = (SparseArray) value;
if(simpleType.isPrimitive() || isBoxedClass(simpleType)){
for (int size = sa.size(), i = size - 1; i >= 0; i--) {
out.name(sa.keyAt(i) + "");
writePrimitiveOrItsBox(out, simpleType, sa.valueAt(i));
}
}else {
TypeAdapter adapter = getTypeAdapter(simpleType);
for (int size = sa.size(), i = size - 1; i >= 0; i--) {
out.name(sa.keyAt(i) + "");
adapter.write(out, sa.valueAt(i));
}
}
}
out.endObject();
}
示例9: 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));
}
};
}
示例10: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
public void write(JsonWriter out, Object value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
TypeAdapter<Object> typeAdapter = this.gson.getAdapter(value.getClass());
if (typeAdapter instanceof ObjectTypeAdapter) {
out.beginObject();
out.endObject();
return;
}
typeAdapter.write(out, value);
}
示例11: toJsonTree
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
private static <T> JsonElement toJsonTree(TypeAdapter<T> typeAdapter, T value) {
try {
JsonTreeWriter jsonWriter = new JsonTreeWriter();
jsonWriter.setLenient(true);
typeAdapter.write(jsonWriter, value);
return jsonWriter.get();
} catch (Throwable e) {
throw new JsonIOException(e);
}
}
示例12: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void write(JsonWriter out, T value) throws IOException {
// Order of preference for choosing type adapters
// First preference: a type adapter registered for the runtime type
// Second preference: a type adapter registered for the declared type
// Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type)
// Fourth preference: reflective type adapter for the declared type
TypeAdapter chosen = delegate;
Type runtimeType = getRuntimeTypeIfMoreSpecific(type, value);
if (runtimeType != type) {
TypeAdapter runtimeTypeAdapter = context.getAdapter(TypeToken.get(runtimeType));
if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
// The user registered a type adapter for the runtime type, so we will use that
chosen = runtimeTypeAdapter;
} else if (!(delegate instanceof ReflectiveTypeAdapterFactory.Adapter)) {
// The user registered a type adapter for Base class, so we prefer it over the
// reflective type adapter for the runtime type
chosen = delegate;
} else {
// Use the type adapter for runtime type
chosen = runtimeTypeAdapter;
}
}
chosen.write(out, value);
}
示例13: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
public void write(JsonWriter out, T value) throws IOException {
TypeAdapter chosen = this.delegate;
Type runtimeType = getRuntimeTypeIfMoreSpecific(this.type, value);
if (runtimeType != this.type) {
TypeAdapter runtimeTypeAdapter = this.context.getAdapter(TypeToken.get(runtimeType));
if (!(runtimeTypeAdapter instanceof Adapter)) {
chosen = runtimeTypeAdapter;
} else if (this.delegate instanceof Adapter) {
chosen = runtimeTypeAdapter;
} else {
chosen = this.delegate;
}
}
chosen.write(out, value);
}
示例14: 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;
}
};
}
示例15: write
import com.google.gson.TypeAdapter; //導入方法依賴的package包/類
@Override
public void write(JsonWriter out, R value) throws IOException {
TypeAdapter<R> adapter = (TypeAdapter<R>) adapters.get(value.getClass());
if (adapter == null) {
throw new JsonParseException("Adapter not found for serializing " + value);
}
adapter.write(out, value);
}