List of usage examples for com.google.gson TypeAdapter write
public abstract void write(JsonWriter out, T value) throws IOException;
From source file:org.immutables.gson.adapter.ExpectedSubtypesAdapter.java
License:Apache License
@Override public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue();//from w w w . j ava 2s. c o m return; } for (int i = 0; i < subtypes.length; i++) { TypeToken<? extends T> subtype = subtypes[i]; if (subtype.getRawType().isInstance(value)) { // safe unchecked, type is checked at runtime @SuppressWarnings("unchecked") TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) adapters.get(i); typeAdapter.write(out, value); return; } } gson.toJson(value, value.getClass(), out); }
From source file:org.jclouds.json.internal.IgnoreNullFluentIterableTypeAdapterFactory.java
License:Apache License
protected <E> TypeAdapter<FluentIterable<E>> newFluentIterableAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<FluentIterable<E>>() { public void write(JsonWriter out, FluentIterable<E> value) throws IOException { out.beginArray();/*from w w w .ja v a 2s. c o m*/ for (E element : value) { elementAdapter.write(out, element); } out.endArray(); } public FluentIterable<E> read(JsonReader in) throws IOException { in.beginArray(); Builder<E> builder = ImmutableList.<E>builder(); while (in.hasNext()) { E element = elementAdapter.read(in); if (element != null) builder.add(element); } in.endArray(); return FluentIterable.from(builder.build()); } }.nullSafe(); }
From source file:org.jclouds.json.internal.IgnoreNullIterableTypeAdapterFactory.java
License:Apache License
protected <E> TypeAdapter<Iterable<E>> newIterableAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<Iterable<E>>() { public void write(JsonWriter out, Iterable<E> value) throws IOException { out.beginArray();/* w w w .ja va 2 s. c om*/ for (E element : value) { elementAdapter.write(out, element); } out.endArray(); } public Iterable<E> read(JsonReader in) throws IOException { in.beginArray(); Builder<E> builder = ImmutableList.<E>builder(); while (in.hasNext()) { E element = elementAdapter.read(in); if (element != null) builder.add(element); } in.endArray(); return builder.build(); } }.nullSafe(); }
From source file:org.jclouds.json.internal.IgnoreNullMapTypeAdapterFactory.java
License:Apache License
private <K, V> TypeAdapter<Map<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) { return new TypeAdapter<Map<K, V>>() { public void write(JsonWriter out, Map<K, V> value) throws IOException { out.beginObject();/* ww w . ja va2s. c o m*/ for (Map.Entry<K, V> element : value.entrySet()) { out.name(String.valueOf(element.getKey())); valueAdapter.write(out, element.getValue()); } out.endObject(); } public Map<K, V> read(JsonReader in) throws IOException { Map<K, V> result = Maps.newLinkedHashMap(); in.beginObject(); while (in.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in); K name = keyAdapter.read(in); V value = valueAdapter.read(in); if (value != null) result.put(name, value); } in.endObject(); return result; } }.nullSafe(); }
From source file:org.jclouds.json.internal.IgnoreNullMultimapTypeAdapterFactory.java
License:Apache License
private <K, V> TypeAdapter<Multimap<K, V>> newMapAdapter(final TypeAdapter<K> keyAdapter, final TypeAdapter<V> valueAdapter) { return new TypeAdapter<Multimap<K, V>>() { public void write(JsonWriter out, Multimap<K, V> map) throws IOException { out.beginObject();//ww w. j a va2 s. com for (K key : map.keySet()) { out.name(String.valueOf(key)); out.beginArray(); for (V value : map.get(key)) { valueAdapter.write(out, value); } out.endArray(); } out.endObject(); } public Multimap<K, V> read(JsonReader in) throws IOException { ImmutableMultimap.Builder<K, V> result = ImmutableListMultimap.builder(); in.beginObject(); while (in.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in); K name = keyAdapter.read(in); in.beginArray(); while (in.hasNext()) { V value = valueAdapter.read(in); if (value != null) result.put(name, value); } in.endArray(); } in.endObject(); return result.build(); } }.nullSafe(); }
From source file:org.jclouds.json.internal.IgnoreNullSetTypeAdapterFactory.java
License:Apache License
private <E> TypeAdapter<Set<E>> newSetAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<Set<E>>() { public void write(JsonWriter out, Set<E> value) throws IOException { out.beginArray();//from ww w. jav a2s.co m for (E element : value) { elementAdapter.write(out, element); } out.endArray(); } public Set<E> read(JsonReader in) throws IOException { Set<E> result = Sets.newLinkedHashSet(); in.beginArray(); while (in.hasNext()) { E element = elementAdapter.read(in); if (element != null) result.add(element); } in.endArray(); return result; } }.nullSafe(); }
From source file:org.jclouds.json.internal.OptionalTypeAdapterFactory.java
License:Apache License
protected <E> TypeAdapter<Optional<E>> newOptionalAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<Optional<E>>() { public void write(JsonWriter out, Optional<E> value) throws IOException { if (!value.isPresent()) { out.nullValue();/*from w ww . j a v a 2s .c o m*/ return; } elementAdapter.write(out, value.get()); } public Optional<E> read(JsonReader in) throws IOException { Optional<E> result = Optional.absent(); if (in.peek() == JsonToken.NULL) { in.nextNull(); } else { E element = elementAdapter.read(in); if (element != null) { result = Optional.of(element); } } return result; } }; }
From source file:org.lanternpowered.server.script.json.CatalogTypeTypeAdapterFactory.java
License:MIT License
@Nullable @Override/*from w w w . java2 s .c o m*/ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { final Class<? super T> raw = type.getRawType(); if (!CatalogType.class.isAssignableFrom(raw)) { return null; } final TypeAdapter<JsonElement> jsonElementTypeAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { jsonElementTypeAdapter.write(out, new JsonPrimitive(((CatalogType) value).getId())); } @Override public T read(JsonReader in) throws IOException { final String id = jsonElementTypeAdapter.read(in).getAsString(); //noinspection unchecked return (T) Sponge.getRegistry().getType((Class<? extends CatalogType>) raw, id) .orElseThrow(() -> new IllegalArgumentException( "There does not exist a " + raw.getName() + " with id: " + id)); } }; }
From source file:org.lanternpowered.server.script.json.ObjectTypeAdapterFactory.java
License:MIT License
@Nullable @Override//from ww w .jav a 2 s . c om public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!type.isAssignableFrom(this.typeToken)) { return null; } final TypeAdapter<JsonElement> jsonElementTypeAdapter = gson.getAdapter(JsonElement.class); final TypeToken theTypeToken = type; return new TypeAdapter<T>() { @SuppressWarnings("unchecked") @Override public void write(JsonWriter out, T value) throws IOException { final JsonElement element = serialize((TypeToken<V>) theTypeToken, (V) value, gson); jsonElementTypeAdapter.write(out, element); } @SuppressWarnings("unchecked") @Override public T read(JsonReader in) throws IOException { final JsonElement element = jsonElementTypeAdapter.read(in); return (T) deserialize(theTypeToken, element, gson); } }; }
From source file:org.mule.runtime.extension.internal.persistence.ModelPropertyMapTypeAdapter.java
License:Open Source License
@Override public void write(JsonWriter out, HierarchyClassMap<ModelProperty> modelPropertyMap) throws IOException { out.beginObject();//from ww w . ja va 2 s . c om for (Map.Entry<Class<?>, ModelProperty> entry : modelPropertyMap.entrySet()) { final ModelProperty modelProperty = entry.getValue(); final Class<?> modelPropertyClass = entry.getKey(); if (modelProperty.isPublic()) { out.name(getSerializableModelPropertyName(modelPropertyClass)); final TypeAdapter adapter = gson.getAdapter(modelPropertyClass); adapter.write(out, modelProperty); } } out.endObject(); }