List of usage examples for com.google.gson.reflect TypeToken getRawType
public final Class<? super T> getRawType()
From source file:jp.pay.model.ExternalAccountTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ExternalAccount.class.isAssignableFrom(type.getRawType())) { return null; // this class only serializes 'ExternalAccount' and its subtypes }/*from w w w . ja v a 2 s . c om*/ final String SOURCE_OBJECT_PROP = "object"; final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); final TypeAdapter<ExternalAccount> externalAccountAdapter = gson.getDelegateAdapter(this, TypeToken.get(ExternalAccount.class)); final TypeAdapter<Card> cardAdapter = gson.getDelegateAdapter(this, TypeToken.get(Card.class)); TypeAdapter<ExternalAccount> result = new TypeAdapter<ExternalAccount>() { public void write(JsonWriter out, ExternalAccount value) throws IOException { // TODO: check instance of for correct writer externalAccountAdapter.write(out, value); } public ExternalAccount read(JsonReader in) throws IOException { JsonObject object = elementAdapter.read(in).getAsJsonObject(); String sourceObject = object.getAsJsonPrimitive(SOURCE_OBJECT_PROP).getAsString(); if (sourceObject.equals("card")) { return cardAdapter.fromJsonTree(object); } else { return externalAccountAdapter.fromJsonTree(object); } } }.nullSafe(); return (TypeAdapter<T>) result; }
From source file:jp.yokomark.utils.gson.adapter.EnumTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*w w w. j a v a 2 s . c om*/ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final Class<? super T> rawType = type.getRawType(); if (!rawType.isEnum()) { // not an enum type, so do not create any of adapter. return null; } final Map<String, T> jsonNameToInstance = new HashMap<String, T>(); for (T constant : (T[]) rawType.getEnumConstants()) { jsonNameToInstance.put(constantNameToJsonName(((Enum<?>) constant).name()), constant); } return (new TypeAdapter<T>() { @Override public T read(final JsonReader reader) throws IOException { final String constValue = reader.nextString(); return jsonNameToInstance.get(constValue); } @Override public void write(final JsonWriter writer, T constant) throws IOException { writer.value(constantNameToJsonName(((Enum<?>) constant).name())); } }).nullSafe(); }
From source file:leafcutter.client.Serializer.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w w w .j av a 2s . c om final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = 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); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that // subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that // subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:mobi.tattu.utils.persistance.datastore.gson.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (!baseType.isAssignableFrom(type.getRawType())) { return null; }/*from w w w . j av a 2 s . co m*/ final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = 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); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to configure a subtype?"); } try { return delegate.fromJsonTree(jsonElement); } catch (JsonSyntaxException e) { return null; } } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to configure a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:mx.openpay.client.serialization.OpenpayTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { if (this.clazz.equals(type.getRawType())) { return (TypeAdapter<T>) this.getClassAdapter(gson, (TypeToken<C>) type); }/*from w w w.j av a 2 s.co m*/ return null; }
From source file:net.billforward.gson.typeadapters.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from www . j a va 2 s. com final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = 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); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { delegate = (TypeAdapter<R>) labelToDelegate.get("default_value_mapping"); } if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }; }
From source file:net.bpiwowar.experimaestro.tasks.XPMTypeAdapterFactory.java
License:Open Source License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeAttributes attributes, TypeToken<T> type) { // Predefined types if (type.getRawType() == File.class) return (TypeAdapter<T>) new FileAdapter(); // Look at registry to find a proxy for annotations for (Factory factory : registry) { if (type.getRawType().isAssignableFrom(factory.field.getType())) { // Found one factory, take the annotations from this field instead // of the original one attributes = new FieldAttributes(factory.field); break; }//from www . j a va2 s . co m } final ClassChooser annotation = attributes.getAnnotation(ClassChooser.class); if (annotation != null) { try { return new ClassChooserAdapter(gson, annotation); } catch (RuntimeException e) { throw new RuntimeException("Error while creating class chooser adapter for " + type); } } return null; }
From source file:net.chris54721.infinitycubed.utils.LowercaseEnumTypeAdapterFactory.java
License:Apache License
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { @SuppressWarnings("unchecked") Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; }/*from w w w .j a va 2s . c om*/ final Map<String, T> lowercaseToConstant = new HashMap<String, T>(); for (T constant : rawType.getEnumConstants()) { lowercaseToConstant.put(toLowercase(constant), constant); } return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(toLowercase(value)); } } public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } else { return lowercaseToConstant.get(reader.nextString()); } } }; }
From source file:net.daporkchop.toobeetooteebot.text.EnumTypeAdapterFactory.java
License:Open Source License
public <T> TypeAdapter<T> create(Gson p_create_1_, TypeToken<T> p_create_2_) { Class<T> oclass = (Class<T>) p_create_2_.getRawType(); if (!oclass.isEnum()) { return null; } else {/* w w w.ja v a 2 s. com*/ final Map<String, T> map = Maps.newHashMap(); for (T t : oclass.getEnumConstants()) { map.put(this.getName(t), t); } return new TypeAdapter<T>() { public void write(JsonWriter p_write_1_, T p_write_2_) throws IOException { if (p_write_2_ == null) { p_write_1_.nullValue(); } else { p_write_1_.value(EnumTypeAdapterFactory.this.getName(p_write_2_)); } } public T read(JsonReader p_read_1_) throws IOException { if (p_read_1_.peek() == JsonToken.NULL) { p_read_1_.nextNull(); return null; } else { return map.get(p_read_1_.nextString()); } } }; } }
From source file:net.feed_the_beast.launcher.json.EnumAdaptorFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*w ww . j a v a 2 s . c o m*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!type.getRawType().isEnum()) { return null; } final Map<String, T> map = Maps.newHashMap(); for (T c : (T[]) type.getRawType().getEnumConstants()) { map.put(c.toString().toLowerCase(Locale.US), c); } return new TypeAdapter<T>() { @Override public T read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull(); return null; } String name = reader.nextString(); if (name == null) { return null; } return map.get(name.toLowerCase(Locale.US)); } @Override public void write(JsonWriter writer, T value) throws IOException { if (value == null) { writer.nullValue(); } else { writer.value(value.toString().toLowerCase(Locale.US)); } } }; }