List of usage examples for com.google.gson TypeAdapter write
public abstract void write(JsonWriter out, T value) throws IOException;
From source file:funf.config.ContextInjectorTypeAdapaterFactory.java
License:Open Source License
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final TypeAdapter<T> delegateAdapter = delegate.create(gson, type); if (delegateAdapter == null) { return null; } else {// w ww . j a v a 2s. c o m return new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { delegateAdapter.write(out, value); } @Override public T read(JsonReader in) throws IOException { T value = delegateAdapter.read(in); if (value != null) { try { Field contextField = AnnotationUtil.getField(CONTEXT_FIELD, value.getClass()); if (contextField != null && Context.class.isAssignableFrom(contextField.getType())) { boolean isAccessible = contextField.isAccessible(); contextField.setAccessible(true); contextField.set(value, context); contextField.setAccessible(isAccessible); } } catch (SecurityException e) { // Swallow Log.v(LogUtil.TAG, e.getMessage()); } catch (IllegalArgumentException e) { // Swallow Log.v(LogUtil.TAG, e.getMessage()); } catch (IllegalAccessException e) { // Swallow Log.v(LogUtil.TAG, e.getMessage()); } } return value; } }; } }
From source file:io.hops.hopsworks.api.zeppelin.json.NotebookTypeAdapterFactory.java
License:Apache License
private TypeAdapter<C> customizeTypeAdapter(Gson gson, TypeToken<C> type) { final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type); final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter<C>() { @Override//w ww . ja v a 2s . c o m public void write(JsonWriter out, C value) throws IOException { JsonElement tree = delegate.toJsonTree(value); beforeWrite(value, tree); elementAdapter.write(out, tree); } @Override public C read(JsonReader in) throws IOException { JsonElement tree = elementAdapter.read(in); afterRead(tree); return delegate.fromJsonTree(tree); } }; }
From source file:jp.pay.model.ExternalAccountTypeAdapterFactory.java
License:Open Source License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!ExternalAccount.class.isAssignableFrom(type.getRawType())) { return null; // this class only serializes 'ExternalAccount' and its subtypes }//from w ww.j ava 2 s . co m final String SOURCE_OBJECT_PROP = "object"; final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); final TypeAdapter<ExternalAccount> externalAccountAdapter = gson.getDelegateAdapter(this, TypeToken.get(ExternalAccount.class)); final TypeAdapter<Card> cardAdapter = gson.getDelegateAdapter(this, TypeToken.get(Card.class)); TypeAdapter<ExternalAccount> result = new TypeAdapter<ExternalAccount>() { public void write(JsonWriter out, ExternalAccount value) throws IOException { // TODO: check instance of for correct writer externalAccountAdapter.write(out, value); } public ExternalAccount read(JsonReader in) throws IOException { JsonObject object = elementAdapter.read(in).getAsJsonObject(); String sourceObject = object.getAsJsonPrimitive(SOURCE_OBJECT_PROP).getAsString(); if (sourceObject.equals("card")) { return cardAdapter.fromJsonTree(object); } else { return externalAccountAdapter.fromJsonTree(object); } } }.nullSafe(); return (TypeAdapter<T>) result; }
From source file:me.albinmathew.celluloid.api.http.ItemTypeAdapterFactory.java
License:Apache License
@NonNull public <T> TypeAdapter<T> create(@NonNull Gson gson, final TypeToken<T> type) { final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter<T>() { public void write(JsonWriter out, T value) throws IOException { delegate.write(out, value); }/*from w w w . j a v a2 s.c o m*/ public T read(JsonReader in) throws IOException { JsonElement jsonElement = elementAdapter.read(in); return delegate.fromJsonTree(jsonElement); } }.nullSafe(); }
From source file:mx.openpay.client.serialization.OpenpayTypeAdapterFactory.java
License:Apache License
public TypeAdapter<C> getClassAdapter(final Gson gson, final TypeToken<C> type) { final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type); final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); return new TypeAdapter<C>() { @Override/*www . j a va 2s . c om*/ public void write(final JsonWriter out, final C value) throws IOException { JsonElement tree = delegate.toJsonTree(value); OpenpayTypeAdapterFactory.this.beforeWrite(value, tree); elementAdapter.write(out, tree); } @Override public C read(final JsonReader in) throws IOException { JsonElement tree = elementAdapter.read(in); OpenpayTypeAdapterFactory.this.afterRead(tree); return delegate.fromJsonTree(tree); } }; }
From source file:net.segoia.event.eventbus.config.json.EventBusJsonConfigLoader.java
License:Apache License
public static EventBusJsonConfig load(Reader reader) { GsonBuilder gb = new GsonBuilder(); gb.registerTypeAdapter(Condition.class, new JsonDeserializer<Condition>() { @Override//from w w w .jav a 2s. c om public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jo = json.getAsJsonObject(); JsonElement conditionType = jo.get("ctype"); String ctype = null; if (conditionType != null) { ctype = conditionType.getAsString().trim(); if ("".equals(ctype)) { ctype = null; } } JsonDeserializer<?> deserializerForType = jsonDeserializers.get(ctype); if (deserializerForType == null) { throw new JsonParseException("No deserializer defined for condition type " + ctype); } return (Condition) deserializerForType.deserialize(json, typeOfT, context); } }); gb.registerTypeAdapterFactory(new TypeAdapterFactory() { @Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type); TypeAdapter<T> typeAdapter = new TypeAdapter<T>() { @Override public void write(JsonWriter out, T value) throws IOException { delegateAdapter.write(out, value); } @Override public T read(JsonReader in) throws IOException { JsonElement value = Streams.parse(in); if (value.isJsonNull()) { return null; } if (!value.isJsonObject()) { return delegateAdapter.fromJsonTree(value); } // System.out.println(value+" "+value.getClass()); JsonObject jo = value.getAsJsonObject(); JsonElement cnameElem = jo.remove("className"); if (cnameElem != null) { String cname = cnameElem.getAsString(); try { // System.out.println("using clazz " + cname); return (T) gson.fromJson(value, Class.forName(cname)); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } else { return delegateAdapter.fromJsonTree(value); } } }; return typeAdapter; } }); final Gson gson = gb.create(); EventBusJsonConfig ebusConfig = gson.fromJson(reader, EventBusJsonConfig.class); return ebusConfig; }
From source file:org.arl.fjage.remote.GenericValueAdapterFactory.java
License:BSD License
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.equals(GenericValue.class)) return null; final GenericValueAdapterFactory parent = this; return new TypeAdapter<T>() { @Override//from w ww . j ava2 s .c o m public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); return; } Class type = ((GenericValue) value).getType(); if (type == null) { out.nullValue(); return; } if (Number.class.isAssignableFrom(type)) out.value((Number) ((GenericValue) value).getValue()); else if (type.equals(String.class)) out.value((String) ((GenericValue) value).getValue()); else if (type.equals(Boolean.class)) out.value((Boolean) ((GenericValue) value).getValue()); else { out.beginObject(); out.name("clazz").value(type.getName()); out.name("data"); TypeAdapter delegate = gson.getAdapter(TypeToken.get(type)); Object v = ((GenericValue) value).getValue(); delegate.write(out, v); out.endObject(); } } @Override public T read(JsonReader in) throws IOException { JsonToken tok = in.peek(); if (tok == JsonToken.NULL) { in.nextNull(); return null; } if (tok == JsonToken.NUMBER) { String s = in.nextString(); try { if (s.contains(".")) return (T) new GenericValue(Double.parseDouble(s)); else return (T) new GenericValue(Long.parseLong(s)); } catch (NumberFormatException ex) { return (T) new GenericValue(null); } } if (tok == JsonToken.STRING) return (T) new GenericValue(in.nextString()); if (tok == JsonToken.BOOLEAN) return (T) new GenericValue(in.nextBoolean()); if (tok != JsonToken.BEGIN_OBJECT) return null; TypeToken tt = null; GenericValue rv = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("clazz")) { try { Class<?> cls = Class.forName(in.nextString()); tt = TypeToken.get(cls); } catch (Exception ex) { // do nothing } } else if (name.equals("data") && tt != null) { TypeAdapter delegate = gson.getAdapter(tt); rv = new GenericValue(delegate.read(in)); } else in.skipValue(); } in.endObject(); return (T) rv; } }; }
From source file:org.arl.fjage.remote.MessageAdapterFactory.java
License:BSD License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!Message.class.isAssignableFrom(rawType)) return null; final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); final TypeAdapter<Performative> perfDelegate = gson.getAdapter(TypeToken.get(Performative.class)); final TypeAdapter<AgentID> aidDelegate = gson.getAdapter(TypeToken.get(AgentID.class)); final TypeAdapter<GenericValue> gvDelegate = gson.getAdapter(TypeToken.get(GenericValue.class)); final MessageAdapterFactory parent = this; return new TypeAdapter<T>() { @Override/*from w ww . ja v a 2 s . c o m*/ public void write(JsonWriter out, T value) throws IOException { if (value == null) out.nullValue(); else { out.beginObject(); out.name("clazz").value(value.getClass().getName()); out.name("data"); if (value instanceof GenericMessage) { GenericMessage msg = (GenericMessage) value; out.beginObject(); out.name("msgID").value(msg.getMessageID()); out.name("inReplyTo").value(msg.getInReplyTo()); out.name("perf"); perfDelegate.write(out, msg.getPerformative()); out.name("recipient"); aidDelegate.write(out, msg.getRecipient()); out.name("sender"); aidDelegate.write(out, msg.getSender()); out.name("map"); delegate.write(out, value); out.endObject(); } else delegate.write(out, value); out.endObject(); } } @Override public T read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } T rv = null; Class<?> cls = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("clazz")) { String className = in.nextString(); try { cls = classloader != null ? Class.forName(className, true, classloader) : Class.forName(className); } catch (Exception ex) { // do nothing } } else if (name.equals("data")) { if (cls == null) rv = delegate.read(in); else if (cls.equals(GenericMessage.class)) { GenericMessage msg = new GenericMessage(); in.beginObject(); while (in.hasNext()) { String fname = in.nextName(); if (fname.equals("msgID")) msg.setMessageID(in.nextString()); else if (fname.equals("inReplyTo")) msg.setInReplyTo(in.nextString()); else if (fname.equals("perf")) msg.setPerformative(perfDelegate.read(in)); else if (fname.equals("recipient")) msg.setRecipient(aidDelegate.read(in)); else if (fname.equals("sender")) msg.setSender(aidDelegate.read(in)); else if (fname.equals("map")) { in.beginObject(); while (in.hasNext()) { String key = in.nextName(); GenericValue value = gvDelegate.read(in); msg.put(key, value); } in.endObject(); } else in.skipValue(); } in.endObject(); rv = (T) msg; } else { TypeAdapter<?> delegate1 = gson.getDelegateAdapter(parent, TypeToken.get(cls)); rv = (T) delegate1.read(in); } } else in.skipValue(); } in.endObject(); return rv; } }; }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.CollectionTypeAdapter.java
License:Open Source License
@Override public void write(JsonWriter out, Collection<E> collection) throws IOException { if (collection == null) { out.nullValue();//from ww w .java 2 s. c o m return; } out.beginArray(); for (E element : collection) { if (element != null && elementType != element.getClass() && (elementType instanceof TypeVariable<?> || elementType instanceof Class<?>)) { @SuppressWarnings("unchecked") TypeAdapter<E> runtimeTypeAdapter = (TypeAdapter<E>) gson .getAdapter(TypeToken.get(element.getClass())); runtimeTypeAdapter.write(out, element); } else { elementTypeAdapter.write(out, element); } } out.endArray(); }
From source file:org.eclipse.recommenders.utils.gson.MultisetTypeAdapterFactory.java
License:Open Source License
private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<Multiset<E>>() { public void write(JsonWriter out, Multiset<E> multiset) throws IOException { if (multiset == null) { out.nullValue();/*w w w . java2s. c om*/ return; } out.beginArray(); for (Entry<E> entry : multiset.entrySet()) { out.beginObject(); out.name("id"); elementAdapter.write(out, entry.getElement()); out.name("count"); out.value(entry.getCount()); out.endObject(); } out.endArray(); } public Multiset<E> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } Multiset<E> result = LinkedHashMultiset.create(); in.beginArray(); while (in.hasNext()) { in.beginObject(); in.nextName(); // "id" E element = elementAdapter.read(in); in.nextName(); // "count" int count = in.nextInt(); result.add(element, count); in.endObject(); } in.endArray(); return result; } }; }