Example usage for com.google.gson.reflect TypeToken getRawType

List of usage examples for com.google.gson.reflect TypeToken getRawType

Introduction

In this page you can find the example usage for com.google.gson.reflect TypeToken getRawType.

Prototype

public final Class<? super T> getRawType() 

Source Link

Document

Returns the raw (non-generic) type for this type.

Usage

From source file:com.prayer.vakit.times.gson.RuntimeTypeAdapterFactory.java

License:Apache License

@Override
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }// w ww  . j ava  2s  . co  m

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
    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:com.redhat.thermostat.gateway.service.commands.channel.coders.typeadapters.MessageTypeAdapterFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*w w  w .  j  av a2s .co m*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<?> clazz = type.getRawType();
    if (AgentRequest.class.isAssignableFrom(clazz)) {
        return (TypeAdapter<T>) new AgentRequestTypeAdapter(gson);
    } else if (WebSocketResponse.class.isAssignableFrom(clazz)) {
        return (TypeAdapter<T>) new WebSocketResponseTypeAdapter(gson);
    } else if (ClientRequest.class.isAssignableFrom(clazz)) {
        return (TypeAdapter<T>) new ClientRequestTypeAdapter(gson);
    } else if (Message.class.isAssignableFrom(clazz)) {
        return (TypeAdapter<T>) new MessageTypeAdapter(gson);
    }
    return null;
}

From source file:com.singhinderjeet.gracenoteapi.json.GracenoteApiTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*ww w .ja v a 2  s .  c  om*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (type.getRawType() == Date.class) {
        return (TypeAdapter<T>) new UtcDateTypeAdapter();
    } else {
        return gson.getDelegateAdapter(this, type);
    }
}

From source file:com.squareup.wire.MessageTypeAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
public MessageTypeAdapter(Wire wire, Gson gson, TypeToken<M> type) {
    this.wire = wire;
    this.gson = gson;
    this.type = (Class<M>) type.getRawType();
}

From source file:com.squareup.wire.WireTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   w w  w  .j  a v  a  2  s .  co  m*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (type.getRawType().equals(ByteString.class)) {
        return (TypeAdapter<T>) new ByteStringTypeAdapter();
    }
    if (Message.class.isAssignableFrom(type.getRawType())) {
        return (TypeAdapter<T>) new MessageTypeAdapter(wire, gson, type);
    }
    return null;
}

From source file:com.truethat.android.external.gson.RuntimeTypeAdapterFactory.java

License:Apache License

/**
 * This method was modified, to include {@link #typeFieldName} in serialized registered sub types.
 *//*from  w  ww .  j av  a 2  s . co  m*/
@Nullable
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType && !labelToSubtype.containsKey(type.getRawType().getSimpleName())) {
        return null;
    }

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();
    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 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);
        }

        @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);
        }
    }.nullSafe();
}

From source file:com.twitter.sdk.android.core.models.SafeListAdapter.java

License:Apache License

@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//from   www  . ja  va  2 s  .  co  m
        public void write(JsonWriter out, T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(JsonReader arg0) throws IOException {
            final T t = delegate.read(arg0);
            if (List.class.isAssignableFrom(tokenType.getRawType())) {
                if (t == null) {
                    return (T) Collections.EMPTY_LIST;
                }

                final List<?> list = (List<?>) t;
                return (T) Collections.unmodifiableList(list);
            }
            return t;
        }
    };
}

From source file:com.twitter.sdk.android.core.models.SafeMapAdapter.java

License:Apache License

@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//  ww w.j a  v  a 2  s . co m
        public void write(JsonWriter out, T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(JsonReader arg0) throws IOException {
            final T t = delegate.read(arg0);
            if (Map.class.isAssignableFrom(tokenType.getRawType())) {
                if (t == null) {
                    return (T) Collections.EMPTY_MAP;
                }

                final Map<?, ?> map = (Map<?, ?>) t;
                return (T) Collections.unmodifiableMap(map);
            }
            return t;
        }
    };
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.AbstractSeriesTypeAdapterFactory.java

@SuppressWarnings("unchecked")
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    return AbstractSeries.class.isAssignableFrom(type.getRawType())
            ? (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<AbstractSeries>) type)
            : null;//from w w  w.  j  a  v a 2s  .  com
}

From source file:com.vaadin.addon.charts.model.gsonhelpers.DataSeriesItemTypeAdapterFactory.java

@SuppressWarnings("unchecked")
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    return DataSeriesItem.class.isAssignableFrom(type.getRawType())
            ? (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<DataSeriesItem>) type)
            : null;//from   ww  w .  ja v a  2 s  .co m
}