Example usage for com.google.gson TypeAdapter write

List of usage examples for com.google.gson TypeAdapter write

Introduction

In this page you can find the example usage for com.google.gson TypeAdapter write.

Prototype

public abstract void write(JsonWriter out, T value) throws IOException;

Source Link

Document

Writes one JSON value (an array, object, string, number, boolean or null) for value .

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  ww. j  a v  a2 s .c  om
                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:br.cefetmg.radar.util.typeadapter.HibernateProxyTypeAdapter.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from   w  ww. j  a va 2s.  c o  m*/
public void write(JsonWriter out, HibernateProxy value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }
    // Retrieve the original (not proxy) class
    Class<?> baseType = Hibernate.getClass(value);
    // Get the TypeAdapter of the original class, to delegate the serialization
    TypeAdapter delegate = context.getAdapter(TypeToken.get(baseType));
    // Get a filled instance of the original class
    Object unproxiedValue = ((HibernateProxy) value).getHibernateLazyInitializer().getImplementation();
    // Serialize the value
    delegate.write(out, unproxiedValue);
}

From source file:cn.ieclipse.af.demo.sample.volley.adapter.TypeAdapterRuntimeTypeWrapper.java

License:Apache License

public void write(JsonWriter out, T value) throws IOException {
    TypeAdapter chosen = this.delegate;
    Type runtimeType = this.getRuntimeTypeIfMoreSpecific(this.type, value);
    if (runtimeType != this.type) {
        TypeAdapter runtimeTypeAdapter = this.context.getAdapter(TypeToken.get(runtimeType));
        if (!(runtimeTypeAdapter instanceof ReflectiveTypeAdapterFactory.Adapter)) {
            chosen = runtimeTypeAdapter;
        } else if (!(this.delegate instanceof ReflectiveTypeAdapterFactory.Adapter)) {
            chosen = this.delegate;
        } else {//from www.  j  a  v  a2 s  . c o m
            chosen = runtimeTypeAdapter;
        }
    }

    chosen.write(out, value);
}

From source file:cn.lambdalib.vis.model.renderer.ItemModelRenderer.java

License:MIT License

protected void writeToJson(JsonWriter out) throws IOException {
    TypeAdapter cta = GsonAdapters.compTransformAdapter;
    out.name("t_std");
    cta.write(out, stdTransform);
    out.name("t_fp");
    cta.write(out, fpTransform);/*from  w  w  w  .  jav  a  2 s  . co m*/
    out.name("t_tp");
    cta.write(out, tpTransform);
    //        if(value.texture != null) {
    //            out.name("texture");
    //            cta.write(out, value.texture.toString());
    //        }
}

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/*w w  w . j  a va 2  s .  co  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.aelitis.azureus.util.ObjectTypeAdapterLong.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*  w  ww . j a  v a2 s.  c  o m*/
public void write(JsonWriter out, Object value) throws IOException {
    if (value == null) {
        out.nullValue();
        return;
    }

    TypeAdapter<Object> typeAdapter = (TypeAdapter<Object>) gson.getAdapter(value.getClass());
    if (typeAdapter instanceof ObjectTypeAdapterLong) {
        out.beginObject();
        out.endObject();
        return;
    }

    typeAdapter.write(out, 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 w  w.j  av  a  2s.  co  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 w  w w .j av a  2 s .  co  m

    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.datastore_android_sdk.serialization.TypeAdapterRuntimeTypeWrapper.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//from   w w w .  j  a va2  s. co  m
public void write(JsonWriter out, T value) throws IOException {
    // Retrieves the type adapter in the order of preference
    TypeAdapter chosen = getRuntimeTypeAdapter(value);
    chosen.write(out, value);
}

From source file:com.drakeet.rebase.api.tool.ResponseTypeAdapterFactory.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type);
    final TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);

    return new TypeAdapter<T>() {
        @Override/*from   w w w  .  ja v  a  2s .c  o  m*/
        public void write(JsonWriter out, T value) throws IOException {
            delegateAdapter.write(out, value);
        }

        @Override
        @SuppressWarnings("PointlessBooleanExpression")
        public T read(JsonReader in) throws IOException {
            JsonElement jsonElement = jsonElementAdapter.read(in);
            if (jsonElement.isJsonObject()) {
                JsonObject jsonObject = jsonElement.getAsJsonObject();
                if (jsonObject.has(ERROR) && jsonObject.get(ERROR).getAsBoolean() == false) {
                    if (jsonObject.has(DATA) && isJson(jsonObject, DATA)) {
                        jsonElement = jsonObject.get(DATA);
                    }
                }
            }
            return delegateAdapter.fromJsonTree(jsonElement);
        }
    }.nullSafe();
}