List of usage examples for com.google.gson TypeAdapter write
public abstract void write(JsonWriter out, T value) throws IOException;
From source file:com.flipkart.android.proteus.demo.converter.GsonRequestBodyConverter.java
License:Apache License
@Override public RequestBody convert(T value) throws IOException { TypeAdapter<T> adapter = getAdapter(); Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); jsonWriter.setSerializeNulls(true);//from w w w .j a va 2 s .co m adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); }
From source file:com.flipkart.android.proteus.demo.converter.UrlEncodedGsonRequestBodyConverter.java
License:Apache License
@Override public RequestBody convert(T value) throws IOException { //noinspection unchecked TypeAdapter<T> adapter = (TypeAdapter<T>) gson.getAdapter(TypeToken.get(type)); Writer writer = new StringWriter(); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close();//from w ww . j a va2 s. c o m return RequestBody.create(MEDIA_TYPE, URLEncoder.encode(writer.toString(), "UTF-8").getBytes()); }
From source file:com.flipkart.batching.gson.RuntimeTypeAdapterFactory.java
License:Open Source License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w w w . j a v a2s.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 = labelToTypeAdapter.get(entry.getKey()); if (delegate == null) { 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 reader) throws IOException { if (reader.peek() == com.google.gson.stream.JsonToken.NULL) { reader.nextNull(); return null; } if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) { reader.skipValue(); return null; } reader.beginObject(); String label = null; R result = null; while (reader.hasNext()) { String name = reader.nextName(); com.google.gson.stream.JsonToken jsonToken = reader.peek(); if (jsonToken == com.google.gson.stream.JsonToken.NULL) { reader.skipValue(); continue; } switch (name) { case "type": label = TypeAdapters.STRING.read(reader); break; case "value": @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = label == null ? null : (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } result = delegate.read(reader); break; default: reader.skipValue(); break; } } reader.endObject(); return result; } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); @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?"); } String label = subtypeToLabel.get(srcType); out.beginObject(); out.name("type"); out.value(label); out.name("value"); delegate.write(out, value); out.endObject(); } }.nullSafe(); }
From source file:com.github.kyriosdata.regras.infraestrutura.CustomRegraTypeAdapterFactory.java
License:Creative Commons License
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { if (type.getRawType() != Regra.class) return null; final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override//www.j a v a 2s .c o m public void write(final JsonWriter jsonWriter, final T t) throws IOException { jsonWriter.beginObject(); jsonWriter.name("tipo"); jsonWriter.value(getTipo(t.getClass().getName())); jsonWriter.name("obj"); delegate.write(jsonWriter, t); jsonWriter.endObject(); } @Override public T read(final JsonReader jsonReader) throws IOException, JsonParseException { JsonElement tree = Streams.parse(jsonReader); JsonObject object = tree.getAsJsonObject(); String clazz = object.get("tipo").getAsString(); JsonElement obj = object.get("obj").getAsJsonObject(); if ("expressao".equals(clazz)) { return (T) gson.getDelegateAdapter(CustomRegraTypeAdapterFactory.this, TypeToken.get(RegraExpressao.class)).fromJsonTree(obj); } if ("pontosPorRelato".equals(clazz)) { return (T) gson.getDelegateAdapter(CustomRegraTypeAdapterFactory.this, TypeToken.get(RegraPontosPorRelato.class)).fromJsonTree(obj); } throw new JsonParseException("Erro ao desserializar " + type + ". No uma regra?"); } }; }
From source file:com.google.caliper.json.ImmutableListTypeAdatperFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override//from w w w.j ava 2 s.c om public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { Type type = typeToken.getType(); if (typeToken.getRawType() != ImmutableList.class || !(type instanceof ParameterizedType)) { return null; } com.google.common.reflect.TypeToken<ImmutableList<?>> betterToken = (com.google.common.reflect.TypeToken<ImmutableList<?>>) com.google.common.reflect.TypeToken .of(typeToken.getType()); final TypeAdapter<ArrayList<?>> arrayListAdapter = (TypeAdapter<ArrayList<?>>) gson.getAdapter( TypeToken.get(betterToken.getSupertype(List.class).getSubtype(ArrayList.class).getType())); return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { ArrayList<?> arrayList = Lists.newArrayList((List<?>) value); arrayListAdapter.write(out, arrayList); } @Override public T read(JsonReader in) throws IOException { ArrayList<?> arrayList = arrayListAdapter.read(in); return (T) ImmutableList.copyOf(arrayList); } }; }
From source file:com.google.caliper.json.ImmutableMapTypeAdapterFactory.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w .j a va2 s. 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//w w w. ja v a 2s. c o m 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/*from ww w . j av a 2 s .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.hkm.disqus.api.gson.BlacklistsEntryTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { // Return null if not a blacklist entry object if (!type.getType().equals(Entry.class)) { return null; }/*ww w. jav a2 s. c om*/ // Get delegate final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); // Return adapter return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { delegate.write(out, value); } @Override public T read(JsonReader in) throws IOException { JsonElement jsonTree = gson.fromJson(in, JsonElement.class); JsonElement value = jsonTree.getAsJsonObject().get("value"); // Process the entry with the delegate T entry = delegate.fromJsonTree(jsonTree); // Process value if needed if (value.isJsonObject()) { ((Entry) entry).value = gson.fromJson(value, User.class); } // Return entry return entry; } }; }
From source file:com.hkm.disqus.api.gson.PostTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { // Return null if not a post object if (!type.getType().equals(Post.class)) { return null; }//from www . ja va 2s.c om // Get delegate final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); // Return adapter return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { delegate.write(out, value); } @Override public T read(JsonReader in) throws IOException { JsonElement jsonTree = gson.fromJson(in, JsonElement.class); JsonElement forum = jsonTree.getAsJsonObject().get("forum"); JsonElement thread = jsonTree.getAsJsonObject().get("thread"); // Process the post with the delegate T post = delegate.fromJsonTree(jsonTree); // Process forum and thread if needed if (forum.isJsonObject()) { ((Post) post).forum = gson.fromJson(forum, Forum.class); } if (thread.isJsonObject()) { ((Post) post).thread = gson.fromJson(thread, Thread.class); } // Return post return post; } }; }