本文整理了Java中com.google.gson.Gson.getDelegateAdapter()
方法的一些代码示例,展示了Gson.getDelegateAdapter()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gson.getDelegateAdapter()
方法的具体详情如下:
包路径:com.google.gson.Gson
类名称:Gson
方法名:getDelegateAdapter
[英]This method is used to get an alternate type adapter for the specified type. This is used to access a type adapter that is overridden by a TypeAdapterFactory that you may have registered. This features is typically used when you want to register a type adapter that does a little bit of work but then delegates further processing to the Gson default type adapter. Here is an example:
Let's say we want to write a type adapter that counts the number of objects being read from or written to JSON. We can achieve this by writing a type adapter factory that uses the getDelegateAdapter
method:
class StatsTypeAdapterFactory implements TypeAdapterFactory public T read(JsonReader in) throws IOException
++numReads;
return delegate.read(in);
}
};
}
}
}
This factory can now be used like this:
StatsTypeAdapterFactory stats = new StatsTypeAdapterFactory();
Note that this call will skip all factories registered before skipPast. In case of multiple TypeAdapterFactories registered it is up to the caller of this function to insure that the order of registration does not prevent this method from reaching a factory they would expect to reply from this call. Note that since you can not override type adapter factories for String and Java primitive types, our stats factory will not count the number of String or primitives that will be read or written.
[中]此方法用于获取指定类型的备用类型适配器。这用于访问可能已注册的TypeAdapterFactory覆盖的类型适配器。当您想要注册一个类型适配器时,通常会使用此功能,该类型适配器只需做一点工作,然后将进一步的处理委托给Gson默认类型适配器。以下是一个例子:
假设我们要编写一个类型适配器,用于计算从JSON读取或写入JSON的对象数。我们可以通过编写使用getDelegateAdapter
方法的类型适配器工厂来实现这一点:
class StatsTypeAdapterFactory implements TypeAdapterFactory public T read(JsonReader in) throws IOException
++numReads;
return delegate.read(in);
}
};
}
}
}
此工厂现在可以这样使用:
StatsTypeAdapterFactory stats = new StatsTypeAdapterFactory();
请注意,此调用将跳过在skipPast之前注册的所有工厂。如果注册了多个TypeAdapterFactory,则由此函数的调用方负责确保注册顺序不会阻止此方法到达他们希望通过此调用进行回复的工厂。请注意,由于您不能覆盖字符串和Java基元类型的类型适配器工厂,因此我们的统计工厂不会计算将要读取或写入的字符串或基元的数量。
代码示例来源:origin: apache/incubator-gobblin
private <S> void writeObject(S value, JsonWriter out) throws IOException {
if (value != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.add(OBJECT_TYPE, new JsonPrimitive(value.getClass().getName()));
TypeAdapter<S> delegate =
(TypeAdapter<S>) this.gson.getDelegateAdapter(this.factory, TypeToken.get(value.getClass()));
jsonObject.add(OBJECT_DATA, delegate.toJsonTree(value));
Streams.write(jsonObject, out);
} else {
out.nullValue();
}
}
代码示例来源:origin: apache/incubator-gobblin
private <S> S readValue(JsonObject jsonObject, TypeToken<S> defaultTypeToken) throws IOException {
try {
TypeToken<S> actualTypeToken;
if (jsonObject.isJsonNull()) {
return null;
} else if (jsonObject.has(OBJECT_TYPE)) {
String className = jsonObject.get(OBJECT_TYPE).getAsString();
Class<S> klazz = (Class<S>) Class.forName(className);
actualTypeToken = TypeToken.get(klazz);
} else if (defaultTypeToken != null) {
actualTypeToken = defaultTypeToken;
} else {
throw new IOException("Could not determine TypeToken.");
}
TypeAdapter<S> delegate = this.gson.getDelegateAdapter(this.factory, actualTypeToken);
S value = delegate.fromJsonTree(jsonObject.get(OBJECT_DATA));
return value;
} catch (ClassNotFoundException cnfe) {
throw new IOException(cnfe);
}
}
代码示例来源:origin: MovingBlocks/Terasology
@SuppressWarnings("unchecked")
@Override
public void write(JsonWriter out, R value) throws IOException {
Class<?> valueClass = value.getClass();
String valueTypeName = valueClass.getName();
TypeToken<?> valueType = TypeToken.get(valueClass);
TypeAdapter<R> delegate = (TypeAdapter<R>)
gson.getDelegateAdapter(PolymorphicTypeAdapterFactory.this, valueType);
if (delegate == null) {
throw new JsonParseException("Could not serialize " + valueClass.getName());
}
JsonElement jsonElement = delegate.toJsonTree(value);
if (valueClass != baseClass) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
JsonObject clone = new JsonObject();
clone.add(TYPE_FIELD_NAME, new JsonPrimitive(valueTypeName));
for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
clone.add(e.getKey(), e.getValue());
}
Streams.write(clone, out);
} else {
Streams.write(jsonElement, out);
}
}
代码示例来源:origin: MovingBlocks/Terasology
gson.getDelegateAdapter(PolymorphicTypeAdapterFactory.this, valueType);
代码示例来源:origin: apache/zeppelin
new LinkedHashMap<Class<?>, TypeAdapter<?>>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
subtypeToDelegate.put(entry.getValue(), delegate);
代码示例来源:origin: runelite/runelite
= new LinkedHashMap<Class<?>, TypeAdapter<?>>();
for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
labelToDelegate.put(entry.getKey(), delegate);
subtypeToDelegate.put(entry.getValue(), delegate);
代码示例来源:origin: immutables/immutables
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
boolean hasAdapter;
try {
TypeAdapter<T> adapter = gson.getDelegateAdapter(this, type);
hasAdapter = !isReflectiveTypeAdapter(adapter);
} catch (IllegalArgumentException e) {
hasAdapter = false;
}
if (hasAdapter) {
return null;
}
try {
@SuppressWarnings("unchecked")
Codec<T> codec = (Codec<T>) registry.get(type.getRawType());
return typeAdapterFromCodec(codec);
} catch (CodecConfigurationException e1) {
return null;
}
}
};
代码示例来源:origin: camunda/camunda-bpm-platform
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(Excluder.this, type));
}
};
代码示例来源:origin: camunda/camunda-bpm-platform
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(skipPast, typeToken));
}
代码示例来源:origin: stackoverflow.com
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
TypeAdapter<?> elementAdapter = gson.getAdapter(
TypeToken.get(parameterizedType.getActualTypeArguments()[0]));
代码示例来源:origin: Odoo-mobile/framework
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(skipPast, typeToken));
}
代码示例来源:origin: Odoo-mobile/framework
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(Excluder.this, type));
}
};
代码示例来源:origin: at.bestsolution.efxclipse.eclipse/com.google.gson
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(skipPast, typeToken));
}
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(skipPast, typeToken));
}
代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded
private TypeAdapter<T> delegate() {
TypeAdapter<T> d = delegate;
return d != null
? d
: (delegate = gson.getDelegateAdapter(Excluder.this, type));
}
};
代码示例来源:origin: stackoverflow.com
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
if(type.getType != Item.class) {
// Do not use custom adapter for other types
return null;
}
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
// your adapter code
}
代码示例来源:origin: org.mule.runtime/mule-extensions-api-persistence
@Override
protected TypeAdapter<ConstructModel> getDelegateAdapter(String kind) {
return gson.getDelegateAdapter(typeAdapterFactory, TypeToken.get((Class) ImmutableConstructModel.class));
}
}
代码示例来源:origin: org.eclipse.lsp4j/org.eclipse.lsp4j
InnerResourceOperationTypeAdapter(TypeAdapterFactory factory, Gson gson) {
createFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(CreateFile.class));
deleteFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(DeleteFile.class));
renameFileAdapter = gson.getDelegateAdapter(factory, TypeToken.get(RenameFile.class));
}
代码示例来源:origin: com.linkedin.gobblin/gobblin-utility
private <S> void writeObject(S value, JsonWriter out) throws IOException {
if (value != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.add(OBJECT_TYPE, new JsonPrimitive(value.getClass().getName()));
TypeAdapter<S> delegate =
(TypeAdapter<S>) this.gson.getDelegateAdapter(this.factory, TypeToken.get(value.getClass()));
jsonObject.add(OBJECT_DATA, delegate.toJsonTree(value));
Streams.write(jsonObject, out);
} else {
out.nullValue();
}
}
代码示例来源:origin: org.apache.gobblin/gobblin-api
private <S> void writeObject(S value, JsonWriter out) throws IOException {
if (value != null) {
JsonObject jsonObject = new JsonObject();
jsonObject.add(OBJECT_TYPE, new JsonPrimitive(value.getClass().getName()));
TypeAdapter<S> delegate =
(TypeAdapter<S>) this.gson.getDelegateAdapter(this.factory, TypeToken.get(value.getClass()));
jsonObject.add(OBJECT_DATA, delegate.toJsonTree(value));
Streams.write(jsonObject, out);
} else {
out.nullValue();
}
}
内容来源于网络,如有侵权,请联系作者删除!