List of usage examples for com.google.gson TypeAdapter TypeAdapter
TypeAdapter
From source file:org.lanternpowered.server.script.json.ObjectTypeAdapterFactory.java
License:MIT License
@Nullable @Override/*from www . j a va 2s .co m*/ 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.metadata.FailureCodeTypeAdapterFactory.java
License:Open Source License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { Class<T> rawType = (Class<T>) type.getRawType(); if (rawType.equals(FailureCode.class)) return (TypeAdapter<T>) new TypeAdapter<FailureCode>() { @Override/*from w w w. j a va2 s . c o m*/ public void write(JsonWriter out, FailureCode value) throws IOException { out.value(value.getName()); } @Override public FailureCode read(JsonReader in) throws IOException { return new FailureCode(in.nextString()); } }; return null; }
From source file:org.objectpocket.gson.CustomTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); // @Entity// w w w. j a v a 2s . c o m if (type.getRawType().getAnnotation(Entity.class) != null) { return new TypeAdapter<T>() { // SERIALIZE public void write(JsonWriter out, T obj) throws IOException { if (obj != null) { String id = objectPocket.getIdForObject(obj); // normalize if (!objectPocket.isSerializeAsRoot(obj)) { gson.toJson(new ProxyOut(obj.getClass().getTypeName(), id), ProxyOut.class, out); return; } else { objectPocket.setSerializeAsRoot(obj, false); } } // default serialization delegate.write(out, obj); }; // DESERIALIZE @SuppressWarnings("unchecked") @Override public T read(JsonReader in) throws IOException { if (in.getPath().length() > 2) { in.beginObject(); in.nextName(); StringBuilder sb = new StringBuilder(in.nextString()); String id = sb.substring(0, sb.indexOf("@")); in.endObject(); T obj = null; try { obj = (T) ReflectionUtil.instantiateDefaultConstructor(type.getRawType()); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { throw new IOException("Could not instantiate class " + type.getRawType().getName() + "\n" + "Might be that the class has no default constructor!", e); } objectPocket.addIdFromReadObject(obj, id); return obj; } else { T obj = delegate.read(in); return obj; } } }; } // All other else { return delegate; } }
From source file:org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory.java
License:Open Source License
/** * Handle {@link BaseChannelConfiguration} * * @param gson/*from w ww . j a v a 2 s . com*/ * @param type * @return */ private <T> TypeAdapter<T> createHAConfig(Gson gson, TypeToken<T> type) { /* The delegate is the 'default' adapter */ final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override public T read(@Nullable JsonReader in) throws IOException { if (in == null) { return null; } /* read the object using the default adapter, but translate the names in the reader */ T result = delegate.read(MappingJsonReader.getConfigMapper(in)); /* do the '~' expansion afterwards */ expandTidleInTopics(BaseChannelConfiguration.class.cast(result)); return result; } @Override public void write(@Nullable JsonWriter out, T value) throws IOException { delegate.write(out, value); } }; }
From source file:org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory.java
License:Open Source License
private <T> TypeAdapter<T> createHADevice(Gson gson, TypeToken<T> type) { /* The delegate is the 'default' adapter */ final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override/* www . j a v a2 s . co m*/ public T read(@Nullable JsonReader in) throws IOException { if (in == null) { return null; } /* read the object using the default adapter, but translate the names in the reader */ T result = delegate.read(MappingJsonReader.getDeviceMapper(in)); return result; } @Override public void write(@Nullable JsonWriter out, T value) throws IOException { delegate.write(out, value); } }; }
From source file:org.smartparam.manager.json.vendor.gson.LevelKeySerializer.java
License:Apache License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (LevelKey.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter) new TypeAdapter<LevelKey>() { @Override/*from ww w. jav a2 s . c om*/ public void write(JsonWriter out, LevelKey value) throws IOException { out.value(value.value()); } @Override public LevelKey read(JsonReader in) throws IOException { return new SimpleLevelKey(in.nextString()); } }; } return null; }
From source file:org.smartparam.manager.json.vendor.gson.ParameterDiffSerializer.java
License:Apache License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (ParameterDiff.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter) new TypeAdapter<ParameterDiff>() { @Override/*from w w w.ja v a 2 s . c o m*/ public void write(JsonWriter out, ParameterDiff value) throws IOException { out.beginObject().name("previous").value(gson.toJson(value.previous())).name("current") .value(gson.toJson(value.current())).endObject(); } @Override public ParameterDiff read(JsonReader in) throws IOException { in.beginObject(); in.nextName(); SimpleParameter previous = gson.fromJson(in, SimpleParameter.class); in.nextName(); SimpleParameter current = gson.fromJson(in, SimpleParameter.class); in.endObject(); return new ParameterDiff(previous, current); } }; } return null; }
From source file:org.smartparam.manager.json.vendor.gson.ParameterEntryDiffSerializer.java
License:Apache License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { if (ParameterEntryDiff.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter) new TypeAdapter<ParameterEntryDiff>() { @Override/* ww w . j a v a 2 s. c o m*/ public void write(JsonWriter out, ParameterEntryDiff value) throws IOException { out.beginObject().name("previous").value(gson.toJson(value.previous())).name("current") .value(gson.toJson(value.current())).endObject(); } @Override public ParameterEntryDiff read(JsonReader in) throws IOException { in.beginObject(); in.nextName(); SimpleParameterEntry previous = gson.fromJson(in, SimpleParameterEntry.class); in.nextName(); SimpleParameterEntry current = gson.fromJson(in, SimpleParameterEntry.class); in.endObject(); return new ParameterEntryDiff(previous, current); } }; } return null; }
From source file:org.smartparam.manager.json.vendor.gson.ParameterEntryKeySerializer.java
License:Apache License
@Override @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (ParameterEntryKey.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter) new TypeAdapter<ParameterEntryKey>() { @Override// www .j a va 2s . c om public void write(JsonWriter out, ParameterEntryKey value) throws IOException { out.value(value.value()); } @Override public ParameterEntryKey read(JsonReader in) throws IOException { return new SimpleParameterEntryKey(in.nextString()); } }; } return null; }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.imported.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w w w .j a va 2 s . co m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> 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 in) throws IOException { JsonElement jsonElement = RuntimeTypeAdapterFactory.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @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?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } RuntimeTypeAdapterFactory.write(clone, out); } }.nullSafe(); }