Example usage for com.google.gson TypeAdapter read

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

Introduction

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

Prototype

public abstract T read(JsonReader in) throws IOException;

Source Link

Document

Reads one JSON value (an array, object, string, number, boolean or null) and converts it to a Java object.

Usage

From source file:com.google.caliper.json.ImmutableMapTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//ww w. j  a va  2s.  c  om
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (typeToken.getRawType() != ImmutableMap.class || !(type instanceof ParameterizedType)) {
        return null;
    }

    com.google.common.reflect.TypeToken<ImmutableMap<?, ?>> betterToken = (com.google.common.reflect.TypeToken<ImmutableMap<?, ?>>) com.google.common.reflect.TypeToken
            .of(typeToken.getType());
    final TypeAdapter<HashMap<?, ?>> hashMapAdapter = (TypeAdapter<HashMap<?, ?>>) gson
            .getAdapter(TypeToken.get(betterToken.getSupertype(Map.class).getSubtype(HashMap.class).getType()));
    return new TypeAdapter<T>() {
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            HashMap<?, ?> hashMap = Maps.newHashMap((Map<?, ?>) value);
            hashMapAdapter.write(out, hashMap);
        }

        @Override
        public T read(JsonReader in) throws IOException {
            HashMap<?, ?> hashMap = hashMapAdapter.read(in);
            return (T) ImmutableMap.copyOf(hashMap);
        }
    };
}

From source file:com.google.caliper.json.ImmutableMultimapTypeAdapterFactory.java

License:Apache License

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> TypeAdapter<T> create(Gson gson, com.google.gson.reflect.TypeToken<T> typeToken) {
    if (ImmutableListMultimap.class.isAssignableFrom(typeToken.getRawType())) {
        TypeToken<Map<?, List<?>>> mapToken = getMapOfListsToken((TypeToken) TypeToken.of(typeToken.getType()));
        final TypeAdapter<Map<?, List<?>>> adapter = (TypeAdapter<Map<?, List<?>>>) gson
                .getAdapter(com.google.gson.reflect.TypeToken.get(mapToken.getType()));
        return new TypeAdapter<T>() {
            @Override//from w  w w. j a va2s .  c  om
            public void write(JsonWriter out, T value) throws IOException {
                ImmutableListMultimap<?, ?> multimap = (ImmutableListMultimap<?, ?>) value;
                adapter.write(out, (Map) multimap.asMap());
            }

            @Override
            public T read(JsonReader in) throws IOException {
                Map<?, List<?>> value = adapter.read(in);
                ImmutableListMultimap.Builder builder = ImmutableListMultimap.builder();
                for (Entry<?, List<?>> entry : value.entrySet()) {
                    builder.putAll(entry.getKey(), entry.getValue());
                }
                return (T) builder.build();
            }
        };
    } else if (ImmutableSetMultimap.class.isAssignableFrom(typeToken.getRawType())) {
        TypeToken<Map<?, Set<?>>> mapToken = getMapOfSetsToken((TypeToken) TypeToken.of(typeToken.getType()));
        final TypeAdapter<Map<?, Set<?>>> adapter = (TypeAdapter<Map<?, Set<?>>>) gson
                .getAdapter(com.google.gson.reflect.TypeToken.get(mapToken.getType()));
        return new TypeAdapter<T>() {
            @Override
            public void write(JsonWriter out, T value) throws IOException {
                ImmutableSetMultimap<?, ?> multimap = (ImmutableSetMultimap<?, ?>) value;
                adapter.write(out, (Map) multimap.asMap());
            }

            @Override
            public T read(JsonReader in) throws IOException {
                Map<?, Set<?>> value = adapter.read(in);
                ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
                for (Entry<?, Set<?>> entry : value.entrySet()) {
                    builder.putAll(entry.getKey(), entry.getValue());
                }
                return (T) builder.build();
            }
        };
    } else {
        return null;
    }
}

From source file:com.google.caliper.json.NaturallySortedMapTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*  w  w  w. j  ava  2s.c  o  m*/
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (!CLASSES.contains(typeToken.getRawType()) || !(type instanceof ParameterizedType)) {
        return null;
    }

    com.google.common.reflect.TypeToken<SortedMap<?, ?>> betterToken = (com.google.common.reflect.TypeToken<SortedMap<?, ?>>) com.google.common.reflect.TypeToken
            .of(typeToken.getType());
    final TypeAdapter<Map<?, ?>> mapAdapter = (TypeAdapter<Map<?, ?>>) gson
            .getAdapter(TypeToken.get(betterToken.getSupertype(Map.class).getType()));
    return new TypeAdapter<T>() {
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            TreeMap<?, ?> treeMap = Maps.newTreeMap((SortedMap<?, ?>) value);
            mapAdapter.write(out, treeMap);
        }

        @SuppressWarnings("rawtypes")
        @Override
        public T read(JsonReader in) throws IOException {
            TreeMap treeMap = Maps.newTreeMap();
            treeMap.putAll(mapAdapter.read(in));
            return (T) treeMap;
        }
    };
}

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  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
        @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;
    }//w ww  .  j  ava  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.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 .ja 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 w  w . j  a  v  a 2  s  .co 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 OperationConfig(in.nextDouble());
            }

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

From source file:com.pcloud.sdk.internal.networking.serialization.UnmodifiableListTypeFactory.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/*w w w .j a v  a  2 s. co m*/
        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;
        }
    };
}

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  w w w . ja  v  a2 s.  com
        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/*from  w  w w .jav a 2 s .  c  o 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;
        }
    };
}