Example usage for com.google.gson Gson getDelegateAdapter

List of usage examples for com.google.gson Gson getDelegateAdapter

Introduction

In this page you can find the example usage for com.google.gson Gson getDelegateAdapter.

Prototype

public <T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) 

Source Link

Document

This method is used to get an alternate type adapter for the specified type.

Usage

From source file:RestrictSerializationToBaseClassFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
    Class incoming = type.getRawType();
    if (subClasses != null) {
        boolean matched = false;
        for (Class<? extends B> subClass : subClasses) {
            if (incoming == subClass) {
                matched = true;/*from   w  w w.j a va 2s  .  c  o m*/
                break;
            }
        }
        if (!matched)
            return null;
    } else { // restricting all sub-classes since none specified
        // don't match the baseClass since we want to use the default factory for it
        if (incoming == baseClass || !baseClass.isAssignableFrom(incoming))
            return null;
    }
    TypeAdapter<T> adapter = new TypeAdapter<T>() {
        @Override
        public T read(JsonReader reader) throws IOException {
            final TypeAdapter subAdapter = gson.getDelegateAdapter(RestrictSerializationToBaseClassFactory.this,
                    type);
            return (T) subAdapter.read(reader);
        }

        @Override
        public void write(JsonWriter writer, T value) throws IOException {
            final TypeAdapter<B> baseAdapter = gson.getAdapter(baseClass);
            baseAdapter.write(writer, (B) value);
        }
    };
    return adapter;
}

From source file:ca.ualberta.cs.team1travelexpenseapp.gsonUtils.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  av a 2  s .com*/

    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 = Streams.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());
            }
            Streams.write(clone, out);
        }
    };
}

From source file:cc.kave.commons.utils.json.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }/*from   w  ww.  ja va 2 s.c o 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 = Streams.parse(in);
            // (kave adaptation) was: ".remove(typeFiledName)"
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(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 {
            if (value == null) {
                Streams.write(null, out);
                return;
            }
            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();
            // (kave adaptation) disabled check
            // 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());
            }
            Streams.write(clone, out);
        }
    };
}

From source file:co.aurasphere.botmill.kik.util.json.LowerCaseTypeAdapterFactory.java

License:Open Source License

public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    return new TypeAdapter<T>() {

        @Override//from w w w . j  a  v  a  2 s . c  o  m
        public T read(JsonReader in) throws IOException {
            JsonElement tree = elementAdapter.read(in);
            afterRead(tree);
            return delegate.fromJsonTree(tree);
        }

        @Override
        public void write(JsonWriter out, T value) throws IOException {
            JsonElement tree = delegate.toJsonTree(value);
            beforeWrite(value, tree);
            elementAdapter.write(out, tree);
        }

        protected void beforeWrite(T source, JsonElement toSerialize) {
        }

        protected void afterRead(JsonElement deserialized) {
            if (deserialized instanceof JsonObject) {
                JsonObject jsonObject = ((JsonObject) deserialized);
                Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
                for (Map.Entry<String, JsonElement> entry : entrySet) {
                    if (entry.getValue() instanceof JsonElement) {
                        if (entry.getKey().equalsIgnoreCase("type")) {
                            String val = jsonObject.get(entry.getKey()).toString();
                            jsonObject.addProperty(entry.getKey(), val.toLowerCase());
                        }
                    } else {
                        afterRead(entry.getValue());
                    }
                }
            }
        }
    };
}

From source file:com.chalup.gsonadapters.JsonRootAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    JsonRootName rootAnnotation = typeToken.getRawType().getAnnotation(JsonRootName.class);
    if (rootAnnotation == null) {
        return null;
    }/*from w  w w . j a  v a 2s. c  om*/

    TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, typeToken);
    return new Adapter<T>(delegateAdapter, rootAnnotation.value());
}

From source file:com.codefororlando.streettrees.api.deserializer.ResponseSanitizer.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
    return new TypeAdapter<T>() {
        public void write(JsonWriter out, T value) throws IOException {
            delegate.write(out, value);//from   w ww.  jav a 2  s.  c  o m
        }

        public T read(JsonReader in) throws IOException {
            T parsedObject = delegate.read(in);

            // NOTE(jpr): Tree names come in with errant whitespace, and its easiest to fix it here
            // rather than fighting retrofit during deserialization
            if (parsedObject instanceof Tree) {
                Tree tree = (Tree) parsedObject;
                tree.setTreeName(tree.getTreeName().trim());
            }

            return parsedObject;
        }
    };
}

From source file:com.cyanogenmod.account.gcm.model.MessageTypeAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (!Message.class.isAssignableFrom(type.getRawType())) {
        return null;
    }//from   ww w.  jav  a  2s  .com

    final TypeAdapter<Message> rootAdapter = gson.getDelegateAdapter(this, TypeToken.get(Message.class));
    final TypeAdapter<PublicKeyMessage> keyExchangeAdapter = gson.getDelegateAdapter(this,
            TypeToken.get(PublicKeyMessage.class));
    final TypeAdapter<SymmetricKeyMessage> symmetricKeyAdapter = gson.getDelegateAdapter(this,
            TypeToken.get(SymmetricKeyMessage.class));
    final TypeAdapter<EncryptedMessage> secureMessageAdapter = gson.getDelegateAdapter(this,
            TypeToken.get(EncryptedMessage.class));
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

    TypeAdapter<Message> result = new TypeAdapter<Message>() {
        @Override
        public void write(JsonWriter out, Message value) throws IOException {
            if (value instanceof PublicKeyMessage) {
                keyExchangeAdapter.write(out, (PublicKeyMessage) value);
            } else if (value instanceof SymmetricKeyMessage) {
                symmetricKeyAdapter.write(out, (SymmetricKeyMessage) value);
            } else if (value instanceof EncryptedMessage) {
                secureMessageAdapter.write(out, (EncryptedMessage) value);
            } else {
                JsonObject object = rootAdapter.toJsonTree(value).getAsJsonObject();
                elementAdapter.write(out, object);
            }
        }

        @Override
        public Message read(JsonReader in) throws IOException {
            JsonObject object = elementAdapter.read(in).getAsJsonObject();
            if (object.has("public_key")) {
                return keyExchangeAdapter.fromJsonTree(object);
            } else if (object.has("symmetric_key")) {
                return symmetricKeyAdapter.fromJsonTree(object);
            } else if (object.has("ciphertext")) {
                return secureMessageAdapter.fromJsonTree(object);
            } else {
                return rootAdapter.fromJsonTree(object);
            }
        }
    }.nullSafe();

    return (TypeAdapter<T>) result;
}

From source file:com.dampcake.gson.immutable.ImmutableAdapterFactory.java

License:Apache License

private <T> TypeAdapter getDelegate(Gson gson, TypeToken<T> type) {
    Class<?> iface = type.getRawType();

    if (!iface.isInterface())
        iface = interfaceMap.get(iface);

    checkState(iface != null, "Non-mappable type found");
    return gson.getDelegateAdapter(this, TypeToken.get(iface));
}

From source file:com.defano.wyldcard.runtime.serializer.PostConstructAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    for (Class<?> t = type.getRawType(); (t != Object.class)
            && (t.getSuperclass() != null); t = t.getSuperclass()) {
        for (Method m : t.getDeclaredMethods()) {
            if (m.isAnnotationPresent(PostConstruct.class)) {
                m.setAccessible(true);/*from w w  w.  j  a va2  s. co m*/
                TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
                return new PostConstructAdapter<>(delegate, m);
            }
        }
    }
    return null;
}

From source file:com.devicehive.json.adapters.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }/*from   w  ww.  jav a  2  s  .c  o  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 = Streams.parse(in);
            //                JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(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());
            //                }
            //                Streams.write(clone, out);
            Streams.write(jsonObject, out);
        }
    }.nullSafe();
}