Fixed a remaining issue with the deserialization of the BitAssetData 'get_object' response

This commit is contained in:
Nelson R. Perez 2018-02-22 17:38:20 -05:00
parent 857d861e4b
commit d0f9ddbbb9
8 changed files with 111 additions and 29 deletions

2
.gitignore vendored
View file

@ -103,3 +103,5 @@ release.properties
graphenej/build
local.properties
sample

View file

@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=0.4.7-alpha1
VERSION_CODE=10
VERSION_NAME=0.4.7-alpha3
VERSION_CODE=12
GROUP=com.github.bilthon
POM_DESCRIPTION=A Java library for mobile app Developers; Graphene/Bitshares blockchain.

View file

@ -1,5 +1,5 @@
group 'cy.agorise'
version '0.4.6'
version '0.4.7-alpha2'
apply plugin: 'com.android.library'
apply from: 'maven-push.gradle'
@ -21,8 +21,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 24
versionCode 10
versionName "0.4.7-alpha1"
versionCode 12
versionName "0.4.7-alpha3"
vectorDrawables.useSupportLibrary = true
}
buildTypes {

View file

@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cy.agorise.graphenej"
android:versionCode="10"
android:versionName="0.4.7-alpha1" >
package="cy.agorise.graphenej">
<uses-sdk android:minSdkVersion="1" />
<application/>
</manifest>

View file

@ -5,12 +5,10 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketFrame;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -93,6 +91,7 @@ public class GetObjects extends BaseGrapheneHandler {
String response = frame.getPayloadText();
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(BitAssetData.class, new BitAssetData.BitAssetDataDeserializer());
gsonBuilder.registerTypeAdapter(AssetFeed.class, new AssetFeed.AssetFeedDeserializer());
gsonBuilder.registerTypeAdapter(ReportedAssetFeed.class, new ReportedAssetFeed.ReportedAssetFeedDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
@ -106,7 +105,8 @@ public class GetObjects extends BaseGrapheneHandler {
JsonParser parser = new JsonParser();
JsonArray resultArray = parser.parse(response).getAsJsonObject().get(WitnessResponse.KEY_RESULT).getAsJsonArray();
for(JsonElement element : resultArray){
for(int i = 0; i < resultArray.size(); i++){
JsonElement element = resultArray.get(i);
String id = element.getAsJsonObject().get(GrapheneObject.KEY_ID).getAsString();
GrapheneObject grapheneObject = new GrapheneObject(id);
switch (grapheneObject.getObjectType()){
@ -119,11 +119,9 @@ public class GetObjects extends BaseGrapheneHandler {
parsedResult.add(account);
break;
case ASSET_BITASSET_DATA:
Type BitAssetDataType = new TypeToken<WitnessResponse<List<BitAssetData>>>(){}.getType();
WitnessResponse<List<BitAssetData>> witnessResponse = gsonBuilder.create().fromJson(response, BitAssetDataType);
for(BitAssetData bitAssetData : witnessResponse.result){
parsedResult.add(bitAssetData);
}
BitAssetData bitAssetData = gson.fromJson(element, BitAssetData.class);
parsedResult.add(bitAssetData);
break;
}
}

View file

@ -1,7 +1,21 @@
package cy.agorise.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.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import cy.agorise.graphenej.GrapheneObject;
import cy.agorise.graphenej.Price;
import cy.agorise.graphenej.Util;
/**
* This is the representation of the response from the 'get_objects' call with
@ -9,16 +23,23 @@ import cy.agorise.graphenej.Price;
*
*/
public class BitAssetData extends GrapheneObject {
public static final String KEY_FEEDS = "feeds";
public static final String KEY_CURRENT_FEED = "current_feed";
public static final String KEY_CURRENT_FEED_PUBLICATION_TIME = "current_feed_publication_time";
public static final String KEY_OPERATIONS = "operations";
public static final String KEY_FORCE_SETTLED_VOLUME = "force_settled_volume";
public static final String KEY_IS_PREDICTION_MARKET = "is_prediction_market";
public static final String KEY_SETTLEMENT_PRICE = "settlement_price";
public static final String KEY_SETTLEMENT_FUND = "settlement_fund";
public ReportedAssetFeed[] feeds;
public AssetFeed current_feed;
public String current_feed_publication_time;
public Object options;
public long force_settled_volume;
public boolean is_prediction_market;
public Price settlement_price;
public long settlement_fund;
private ReportedAssetFeed[] feeds;
private AssetFeed current_feed;
private Date current_feed_publication_time;
private Options options;
private long force_settled_volume;
private boolean is_prediction_market;
private Price settlement_price;
private long settlement_fund;
public BitAssetData(String id) {
super(id);
@ -40,19 +61,24 @@ public class BitAssetData extends GrapheneObject {
this.current_feed = current_feed;
}
public String getCurrentFeedPublicationTime() {
public Date getCurrentFeedPublicationTime() {
return current_feed_publication_time;
}
public void setCurrentFeedPublicationTime(String current_feed_publication_time) {
this.current_feed_publication_time = current_feed_publication_time;
public void setCurrentFeedPublicationTime(String currentFeedPublicationTime) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Util.TIME_DATE_FORMAT);
try {
this.current_feed_publication_time = simpleDateFormat.parse(currentFeedPublicationTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
public Object getOptions() {
public Options getOptions() {
return options;
}
public void setOptions(Object options) {
public void setOptions(Options options) {
this.options = options;
}
@ -87,4 +113,46 @@ public class BitAssetData extends GrapheneObject {
public void setSettlementFund(long settlementFund) {
this.settlement_fund = settlementFund;
}
/**
* Custom deserializer used to instantiate the BitAssetData class from the response of the
* 'get_objects' API call.
*/
public static class BitAssetDataDeserializer implements JsonDeserializer<BitAssetData> {
@Override
public BitAssetData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String id = jsonObject.get(GrapheneObject.KEY_ID).getAsString();
BitAssetData bitAssetData = new BitAssetData(id);
ArrayList<ReportedAssetFeed> reportedAssetFeeds = new ArrayList<>();
JsonArray jsonAssetFeeds = jsonObject.get(KEY_FEEDS).getAsJsonArray();
for(JsonElement jsonFeed : jsonAssetFeeds){
ReportedAssetFeed reportedAssetFeed = context.deserialize(jsonFeed, ReportedAssetFeed.class);
reportedAssetFeeds.add(reportedAssetFeed);
}
// Deserializing attributes
JsonElement jsonCurrentFeed = jsonObject.get(KEY_CURRENT_FEED).getAsJsonObject();
AssetFeed assetFeed = context.deserialize(jsonCurrentFeed, AssetFeed.class);
String publicationTime = jsonObject.get(KEY_CURRENT_FEED_PUBLICATION_TIME).getAsString();
Options options = context.deserialize(jsonObject.get(KEY_OPERATIONS), Options.class);
long forceSettledVolume = jsonObject.get(KEY_FORCE_SETTLED_VOLUME).getAsLong();
boolean isPredictionMarket = jsonObject.get(KEY_IS_PREDICTION_MARKET).getAsBoolean();
Price settlementPrice = context.deserialize(jsonObject.get(KEY_SETTLEMENT_PRICE), Price.class);
long settlementFund = jsonObject.get(KEY_SETTLEMENT_FUND).getAsLong();
// Setting attributes
bitAssetData.setFeeds(reportedAssetFeeds.toArray(new ReportedAssetFeed[reportedAssetFeeds.size()]));
bitAssetData.setCurrentFeed(assetFeed);
bitAssetData.setCurrentFeedPublicationTime(publicationTime);
bitAssetData.setOptions(options);
bitAssetData.setForceSettledVolume(forceSettledVolume);
bitAssetData.setIsPredictionMarket(isPredictionMarket);
bitAssetData.setSettlementPrice(settlementPrice);
bitAssetData.setSettlementFund(settlementFund);
return bitAssetData;
}
}
}

View file

@ -0,0 +1,15 @@
package cy.agorise.graphenej.models;
/**
* Class used to represent the 'options' object returned inside the response obtained after
* querying for an object of type 'asset_bitasset_data' (2.4.x)
*/
public class Options {
private long feed_lifetime_sec;
private long minimum_feeds;
private long force_settlement_delay_sec;
private long force_settlement_offset_percent;
private long maximum_force_settlement_volume;
private String short_backing_asset;
}

View file

@ -122,6 +122,7 @@ public class GetObjectsTest extends BaseApiTest{
public void onSuccess(WitnessResponse response) {
System.out.println("onSuccess");
List<BitAssetData> list = (List<BitAssetData>) response.result;
System.out.println("Response array length: "+list.size());
BitAssetData bitAssetData1 = list.get(0);
BitAssetData bitAssetData2 = list.get(1);