Android Open Source - Buddy-Android-SDK Json Envelope Deserializer






From Project

Back to project page Buddy-Android-SDK.

License

The source code is released under:

Apache License

If you think the Android project Buddy-Android-SDK listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.buddy.sdk;
/*  w  w  w  .  j a va  2 s  .  co  m*/
import android.location.Location;

import com.buddy.sdk.models.ModelBase;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;

import java.lang.reflect.Type;
import java.util.Date;

/**
 * Created by shawn on 7/6/14.
 */
public class JsonEnvelopeDeserializer<T> implements JsonDeserializer<JsonEnvelope<Object>>
{
    private Class<T> clazz;

    private Gson gson;

    public JsonEnvelopeDeserializer(Class<T> clazz) {
        this.clazz = clazz;
        gson = JsonEnvelopeDeserializer.makeGsonDeserializer();
    }

    public static Gson makeGsonDeserializer(){
        return new GsonBuilder()
                .registerTypeAdapter(Location.class, new BuddyLocationDeserializer())
                .registerTypeAdapter(Date.class, new BuddyDateDeserializer())
                .create();
    }

    @Override
    public JsonEnvelope<Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException
    {
        JsonObject jsonObj = json.getAsJsonObject();
        Object result = null;

        JsonElement element = jsonObj.get("result");

        if (element != null) {
            if (element.isJsonObject()) {

                if (clazz != null && !JsonObject.class.isAssignableFrom(clazz)) {
                    result = gson.fromJson(element.getAsJsonObject(), clazz);
                } else {
                    result = element.getAsJsonObject();
                }

                if (result instanceof ModelBase) {
                    ((ModelBase) result).setJsonObject(element.getAsJsonObject());
                }
            } else if (element.isJsonPrimitive()) {
                JsonPrimitive primitive = element.getAsJsonPrimitive();

                if (primitive.isString()) {
                    result = primitive.getAsString();
                } else if (primitive.isBoolean()) {
                    result = primitive.getAsBoolean();
                } else if (primitive.isNumber()) {
                    result = primitive.getAsLong();
                }
            } else {
                throw new JsonParseException("Can't deal with JSON: " + element.toString());
            }

        }

        JsonEnvelope<Object> env = new JsonEnvelope<Object>(jsonObj, result);

        return env;
    }
}




Java Source Code List

chat.sample.buddy.com.buddychat.ApplicationTest.java
chat.sample.buddy.com.buddychat.BuddyChatApplication.java
chat.sample.buddy.com.buddychat.Chat.java
chat.sample.buddy.com.buddychat.GcmBroadcastReceiver.java
chat.sample.buddy.com.buddychat.GcmIntentService.java
chat.sample.buddy.com.buddychat.GetCurrentUserCallback.java
chat.sample.buddy.com.buddychat.Loading.java
chat.sample.buddy.com.buddychat.Login.java
chat.sample.buddy.com.buddychat.MainScreen.java
chat.sample.buddy.com.buddychat.Signup.java
chat.sample.buddy.com.buddychat.SimpleAdapter.java
com.buddy.sample.buddychat.LoginActivity.java
com.buddy.sdk.AccessTokenCallback.java
com.buddy.sdk.BuddyCallback.java
com.buddy.sdk.BuddyClientImpl.java
com.buddy.sdk.BuddyClientOptions.java
com.buddy.sdk.BuddyClient.java
com.buddy.sdk.BuddyDateDeserializer.java
com.buddy.sdk.BuddyFile.java
com.buddy.sdk.BuddyFutureCallback.java
com.buddy.sdk.BuddyFuture.java
com.buddy.sdk.BuddyLocationDeserializer.java
com.buddy.sdk.BuddyLocationRangeSerializer.java
com.buddy.sdk.BuddyLocationSerializer.java
com.buddy.sdk.BuddyResultBase.java
com.buddy.sdk.BuddyResult.java
com.buddy.sdk.BuddyServiceClient.java
com.buddy.sdk.Buddy.java
com.buddy.sdk.DateRangeSerializer.java
com.buddy.sdk.DateRange.java
com.buddy.sdk.JsonEnvelopeBase.java
com.buddy.sdk.JsonEnvelopeDeserializer.java
com.buddy.sdk.JsonEnvelope.java
com.buddy.sdk.UserAuthenticationRequiredCallback.java
com.buddy.sdk.models.AlbumItem.java
com.buddy.sdk.models.Album.java
com.buddy.sdk.models.BinaryModelBase.java
com.buddy.sdk.models.Blob.java
com.buddy.sdk.models.BuddyLocation.java
com.buddy.sdk.models.Checkin.java
com.buddy.sdk.models.Identity.java
com.buddy.sdk.models.LocationRange.java
com.buddy.sdk.models.Message.java
com.buddy.sdk.models.MetadataItem.java
com.buddy.sdk.models.ModelBase.java
com.buddy.sdk.models.NotificationResult.java
com.buddy.sdk.models.PagedResult.java
com.buddy.sdk.models.Picture.java
com.buddy.sdk.models.SocialNetworkUser.java
com.buddy.sdk.models.TimedMetric.java
com.buddy.sdk.models.UserListItem.java
com.buddy.sdk.models.UserList.java
com.buddy.sdk.models.User.java
com.buddy.sdk.models.Video.java