Example usage for com.google.gson TypeAdapter TypeAdapter

List of usage examples for com.google.gson TypeAdapter TypeAdapter

Introduction

In this page you can find the example usage for com.google.gson TypeAdapter TypeAdapter.

Prototype

TypeAdapter

Source Link

Usage

From source file:com.ibm.og.json.type.ChoiceConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!ChoiceConfig.class.equals(rawType)) {
        return null;
    }/*from ww  w.  j ava2s. c o m*/

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        @SuppressWarnings("rawtypes")
        public T read(final JsonReader in) throws IOException {
            final Class<?> genericType = (Class<?>) ((ParameterizedType) type.getType())
                    .getActualTypeArguments()[0];
            final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);

            // the tree api is used here rather than the stream api so that the full object can be
            // inspected and we can differentiate between a ChoiceConfig<T> object or the underlying T
            // object itself. With the stream api there would be no way to rewind the stream once this
            // determination is made
            //
            // this logic allows the user to configure a choice of T in both the standard form, or
            // compactly if the default choice weight is sufficient e.g.
            //
            // standard form
            // {"choice": {fields for T object}, "weight": 1.0} <- weight is optional here
            //
            // compact form where default weight is acceptable
            // {fields for T object}
            final JsonElement element = jsonElementAdapter.read(in);
            if (element.isJsonObject()) {
                final JsonObject object = element.getAsJsonObject();
                if (object.entrySet().size() <= 2 && object.has("choice")) {
                    return delegate.fromJsonTree(element);
                }
            }

            return (T) new ChoiceConfig(gson.getAdapter(TypeToken.get(genericType)).fromJsonTree(element));
        }
    }.nullSafe();
}

From source file:com.ibm.og.json.type.ContainerConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!ContainerConfig.class.equals(rawType)) {
        return null;
    }//from  w  w  w . jav  a2 s .  c  o  m

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.STRING == in.peek()) {
                return (T) new ContainerConfig(in.nextString());
            }

            return delegate.read(in);
        }

    }.nullSafe();
}

From source file:com.ibm.og.json.type.FilesizeConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!FilesizeConfig.class.equals(rawType)) {
        return null;
    }/*from  w w w .j a va 2 s . c o  m*/

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new FilesizeConfig(in.nextDouble());
            }

            return delegate.read(in);
        }

    }.nullSafe();
}

From source file:com.ibm.og.json.type.OperationConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!OperationConfig.class.equals(rawType)) {
        return null;
    }/*  w ww  .j  a  va2 s.com*/

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            if (JsonToken.NUMBER == in.peek()) {
                return (T) new OperationConfig(in.nextDouble());
            }

            return delegate.read(in);
        }
    }.nullSafe();
}

From source file:com.ibm.og.json.type.SelectionConfigTypeAdapterFactory.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!SelectionConfig.class.equals(rawType)) {
        return null;
    }/*from   w w  w. j  a v a  2  s .  c  o  m*/

    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            delegate.write(out, value);
        }

        @Override
        public T read(final JsonReader in) throws IOException {
            final Class<?> genericType = (Class<?>) ((ParameterizedType) type.getType())
                    .getActualTypeArguments()[0];
            final JsonElement element = gson.getAdapter(JsonElement.class).read(in);

            if (element.isJsonObject()) {
                final JsonObject object = element.getAsJsonObject();
                if (object.entrySet().size() <= 2 && object.has("choices")) {
                    return delegate.fromJsonTree(object);
                } else {
                    return (T) choice(genericType, object);
                }
            } else if (element.isJsonArray()) {
                return (T) choiceList(genericType, element.getAsJsonArray());
            }

            return (T) choice(genericType, element);
        }

        private <S> SelectionConfig<S> choice(final Class<S> clazz, final JsonElement element)
                throws IOException {
            final SelectionConfig<S> config = new SelectionConfig<S>();
            config.choices.add(gson.getAdapter(choiceToken(clazz)).fromJsonTree(element));
            return config;
        }

        private <S> SelectionConfig<S> choiceList(final Class<S> clazz, final JsonArray array)
                throws IOException {
            final SelectionConfig<S> config = new SelectionConfig<S>();
            config.choices = gson.getAdapter(choiceListToken(clazz)).fromJsonTree(array);
            return config;
        }

        // must use guava's TypeToken implementation to create a TypeToken instance with a dynamic
        // type; then convert back to gson's TypeToken implementation for use in calling code. See:
        // https://groups.google.com/forum/#!topic/guava-discuss/HdBuiO44uaw
        private <S> TypeToken<ChoiceConfig<S>> choiceToken(final Class<S> clazz) {
            @SuppressWarnings("serial")
            final com.google.common.reflect.TypeToken<ChoiceConfig<S>> choiceToken = new com.google.common.reflect.TypeToken<ChoiceConfig<S>>() {
            }.where(new TypeParameter<S>() {
            }, com.google.common.reflect.TypeToken.of(clazz));

            return (TypeToken<ChoiceConfig<S>>) TypeToken.get(choiceToken.getType());
        }

        private <S> TypeToken<List<ChoiceConfig<S>>> choiceListToken(final Class<S> clazz) {
            @SuppressWarnings("serial")
            final com.google.common.reflect.TypeToken<List<ChoiceConfig<S>>> choiceToken = new com.google.common.reflect.TypeToken<List<ChoiceConfig<S>>>() {
            }.where(new TypeParameter<S>() {
            }, com.google.common.reflect.TypeToken.of(clazz));

            return (TypeToken<List<ChoiceConfig<S>>>) TypeToken.get(choiceToken.getType());
        }
    }.nullSafe();
}

From source file:com.ibm.og.util.json.type.CaseInsensitiveEnumTypeAdapterFactory.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    @SuppressWarnings("unchecked")
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!rawType.isEnum()) {
        return null;
    }/* w  ww.j  av a 2s. co  m*/

    return new TypeAdapter<T>() {
        @Override
        public void write(final JsonWriter out, final T value) throws IOException {
            out.value(value.toString().toLowerCase(Locale.US));
        }

        @Override
        @SuppressWarnings("unchecked")
        public T read(final JsonReader in) throws IOException {
            final String s = in.nextString().toUpperCase(Locale.US);
            for (final Object enumEntry : rawType.getEnumConstants()) {
                if (enumEntry.toString().equals(s)) {
                    return (T) enumEntry;
                }
            }
            throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s));
        }
    }.nullSafe();
}

From source file:com.kotcrab.vis.editor.serializer.json.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (baseType.isAssignableFrom(type.getRawType()) == false) {
        return null;
    }//from  ww  w . j  a va  2  s .  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);
        }
    };
}

From source file:com.learn.mobile.library.dmobi.helper.RuntimeTypeAdapterFactory.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 a  v  a  2s .c o 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);
            // Do not remove typeFieldName
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(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.metinkale.prayerapp.vakit.times.gson.RuntimeTypeAdapterFactory.java

License:Apache License

@Override
public <R> TypeAdapter<R> create(@NonNull Gson gson, @NonNull TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }/*from w  w w  . j a  v  a  2  s  . 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, @NonNull 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.michaelfotiadis.crossyscore.core.data.parsers.gson.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(final Gson gson, final TypeToken<R> type) {
    if (!type.getRawType().equals(baseType)) {
        return null;
    }//from w w w  . j a  v  a 2  s. co  m

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<>();

    for (final Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        final 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(final JsonWriter out, final R value) throws IOException {
            if (value != null) {
                final Class<?> srcType = value.getClass();
                final String label = subtypeToLabel.get(srcType);
                @SuppressWarnings("unchecked")
                final // 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?");
                }
                final 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);
                }
                final JsonObject clone = new JsonObject();
                clone.add(typeFieldName, new JsonPrimitive(label));
                for (final Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                    clone.add(e.getKey(), e.getValue());
                }
                Streams.write(clone, out);
            } else {
                out.nullValue();
            }
        }

        @Override
        public R read(final JsonReader in) throws IOException {
            final JsonElement jsonElement = Streams.parse(in);
            // fix for null Json Elements
            if (jsonElement.isJsonNull()) {
                return null;
            }

            final JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            final String label = getBaseTypeLabel(labelJsonElement);
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            final 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);
        }

        private String getBaseTypeLabel(final JsonElement labelJsonElement) {
            final String label;

            if (labelJsonElement == null) {
                if (defaultSubTypeLabel == null) {
                    throw new JsonParseException("cannot deserialize " + baseType
                            + " because it does not define a field named " + typeFieldName);
                } else {
                    label = defaultSubTypeLabel;

                    if (!labelToDelegate.containsKey(label)) {
                        throw new IllegalStateException(
                                "WTF: Was looking for " + label + " in " + labelToDelegate.keySet());
                    }

                }
            } else {
                label = labelJsonElement.getAsString();
            }

            return label;
        }
    };
}