From 5a62f40d93a22f2156b7a3417521b96d6fa1405d Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Wed, 18 Jan 2017 22:36:23 -0500 Subject: [PATCH] Introducing the SubscriptionResponse class with basic support for AccountBalanceUpdate and DynamicGlobalProperties objects deserialization --- .../models/SubscriptionResponse.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/de/bitsharesmunich/graphenej/models/SubscriptionResponse.java diff --git a/src/main/java/de/bitsharesmunich/graphenej/models/SubscriptionResponse.java b/src/main/java/de/bitsharesmunich/graphenej/models/SubscriptionResponse.java new file mode 100644 index 0000000..4118963 --- /dev/null +++ b/src/main/java/de/bitsharesmunich/graphenej/models/SubscriptionResponse.java @@ -0,0 +1,70 @@ +package de.bitsharesmunich.graphenej.models; + +import com.google.gson.JsonArray; +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 java.io.Serializable; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import de.bitsharesmunich.graphenej.GrapheneObject; +import de.bitsharesmunich.graphenej.ObjectType; + +/** + * Created by nelson on 1/12/17. + */ +public class SubscriptionResponse { + private static final String TAG = "SubscriptionResponse"; + public static final String KEY_ID = "id"; + public static final String KEY_METHOD = "method"; + public static final String KEY_PARAMS = "params"; + + public String method; + public List params; + + public static class SubscriptionResponseDeserializer implements JsonDeserializer { + + @Override + public SubscriptionResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + SubscriptionResponse response = new SubscriptionResponse(); + JsonObject responseObject = json.getAsJsonObject(); + response.method = responseObject.get(KEY_METHOD).getAsString(); + + JsonArray paramsArray = responseObject.get(KEY_PARAMS).getAsJsonArray(); + response.params = new ArrayList<>(); + response.params.add(paramsArray.get(0).getAsInt()); + ArrayList secondArgument = new ArrayList<>(); + response.params.add(secondArgument); + + JsonArray subArray = paramsArray.get(1).getAsJsonArray().get(0).getAsJsonArray(); + for(JsonElement object : subArray){ + if(object.isJsonObject()){ + GrapheneObject grapheneObject = new GrapheneObject(object.getAsJsonObject().get(KEY_ID).getAsString()); + JsonObject jsonObject = object.getAsJsonObject(); + if(grapheneObject.getObjectType() == ObjectType.ACCOUNT_BALANCE_OBJECT){ + AccountBalanceUpdate balanceObject = new AccountBalanceUpdate(grapheneObject.getObjectId()); + balanceObject.owner = jsonObject.get(AccountBalanceUpdate.KEY_OWNER).getAsString(); + balanceObject.asset_type = jsonObject.get(AccountBalanceUpdate.KEY_ASSET_TYPE).getAsString(); + balanceObject.balance = jsonObject.get(AccountBalanceUpdate.KEY_BALANCE).getAsLong(); + secondArgument.add(balanceObject); + }else if(grapheneObject.getObjectType() == ObjectType.DYNAMIC_GLOBAL_PROPERTY_OBJECT){ + DynamicGlobalProperties dynamicGlobal = new DynamicGlobalProperties(grapheneObject.getObjectId()); + dynamicGlobal.head_block_number = jsonObject.get(DynamicGlobalProperties.KEY_HEAD_BLOCK_NUMBER).getAsLong(); + dynamicGlobal.head_block_id = jsonObject.get(DynamicGlobalProperties.KEY_HEAD_BLOCK_ID).getAsString(); + dynamicGlobal.time = jsonObject.get(DynamicGlobalProperties.KEY_TIME).getAsString(); + //TODO: Deserialize all other attributes + secondArgument.add(dynamicGlobal); + } + }else{ + secondArgument.add(object.getAsString()); + } + } + return response; + } + } +}