List of usage examples for com.google.gson TypeAdapter read
public abstract T read(JsonReader in) throws IOException;
From source file:org.immutables.gson.adapter.ExpectedSubtypesAdapter.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*from w w w. j a v a2s. co m*/ public T read(JsonReader in) throws IOException { List<Exception> exceptions = new ArrayList<>(subtypes.length); ReaderSupplier readerSupplier = readForSupplier(in); for (TypeAdapter<?> typeAdapter : adapters) { try { return (T) typeAdapter.read(readerSupplier.create()); } catch (Exception ex) { exceptions.add(ex); } } JsonParseException failure = new JsonParseException( String.format("Cannot parse %s with following subtypes: %s", type, Arrays.toString(subtypes))); for (Exception exception : exceptions) { failure.addSuppressed(exception); } throw failure; }
From source file:org.immutables.mongo.repository.internal.BsonEncoding.java
License:Apache License
public static <T> T unmarshalDbObject(DBObject dbObject, TypeAdapter<T> adaper) throws IOException { BasicOutputBuffer buffer = new BasicOutputBuffer(); encoder().writeObject(buffer, dbObject); BsonParser parser = BSON_FACTORY.createParser(buffer.toByteArray()); BsonReader reader = new BsonReader(parser); T instance = adaper.read(reader); reader.close();//from w ww .jav a2 s.co m return instance; }
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 ww . ja v a 2 s . co 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();/*from www . j ava 2s . 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();// w ww . j a v a 2s . c om 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();//from w w w .j a v a2s . c o m 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();// w w w. j a va 2s . c o 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 w w .j a v a 2 s. 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// www . j av a 2 s .com 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/*w w w . j a va 2s. com*/ 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); } }; }