Fixed a problem with the deserialization of the get_objects api call that ocurred when the requested objects were of type impl_asset_bitasset_data_type

This commit is contained in:
Nelson R. Perez 2018-02-20 23:38:47 -05:00
parent dd87ea1df8
commit 857d861e4b
6 changed files with 257 additions and 14 deletions

View file

@ -18,4 +18,13 @@ package cy.agorise.graphenej;
public class Price { public class Price {
public AssetAmount base; public AssetAmount base;
public AssetAmount quote; public AssetAmount quote;
@Override
public String toString() {
return String.format("base:[%s, %s], quote:[%s, %s]",
base.getAsset().getObjectId(),
base.getAmount().toString(),
quote.getAsset().getObjectId(),
quote.getAmount().toString());
}
} }

View file

@ -24,7 +24,9 @@ import cy.agorise.graphenej.RPC;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.interfaces.WitnessResponseListener; import cy.agorise.graphenej.interfaces.WitnessResponseListener;
import cy.agorise.graphenej.models.ApiCall; import cy.agorise.graphenej.models.ApiCall;
import cy.agorise.graphenej.models.AssetFeed;
import cy.agorise.graphenej.models.BitAssetData; import cy.agorise.graphenej.models.BitAssetData;
import cy.agorise.graphenej.models.ReportedAssetFeed;
import cy.agorise.graphenej.models.WitnessResponse; import cy.agorise.graphenej.models.WitnessResponse;
/** /**
@ -91,6 +93,8 @@ public class GetObjects extends BaseGrapheneHandler {
String response = frame.getPayloadText(); String response = frame.getPayloadText();
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AssetFeed.class, new AssetFeed.AssetFeedDeserializer());
gsonBuilder.registerTypeAdapter(ReportedAssetFeed.class, new ReportedAssetFeed.ReportedAssetFeedDeserializer());
gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer()); gsonBuilder.registerTypeAdapter(AssetAmount.class, new AssetAmount.AssetAmountDeserializer());
gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer()); gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer());
gsonBuilder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountFullDeserializer()); gsonBuilder.registerTypeAdapter(UserAccount.class, new UserAccount.UserAccountFullDeserializer());
@ -117,8 +121,9 @@ public class GetObjects extends BaseGrapheneHandler {
case ASSET_BITASSET_DATA: case ASSET_BITASSET_DATA:
Type BitAssetDataType = new TypeToken<WitnessResponse<List<BitAssetData>>>(){}.getType(); Type BitAssetDataType = new TypeToken<WitnessResponse<List<BitAssetData>>>(){}.getType();
WitnessResponse<List<BitAssetData>> witnessResponse = gsonBuilder.create().fromJson(response, BitAssetDataType); WitnessResponse<List<BitAssetData>> witnessResponse = gsonBuilder.create().fromJson(response, BitAssetDataType);
BitAssetData bitAssetData = witnessResponse.result.get(0); for(BitAssetData bitAssetData : witnessResponse.result){
parsedResult.add(bitAssetData); parsedResult.add(bitAssetData);
}
} }
} }

View file

@ -1,13 +1,80 @@
package cy.agorise.graphenej.models; package cy.agorise.graphenej.models;
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 cy.agorise.graphenej.Price; import cy.agorise.graphenej.Price;
/** /**
* Created by nelson on 1/9/17. * Price feed of a given asset.
*/ */
public class AssetFeed { public class AssetFeed {
public Price settlement_price; public static final String KEY_SETTLEMENT_PRICE = "settlement_price";
public long maintenance_collateral_ratio; public static final String KEY_MAINTENANCE_COLLATERAL_RATIO = "maintenance_collateral_ratio";
public long maximum_short_squeeze_ratio; public static final String KEY_MAXIMUM_SHORT_SQUEEZE_RATIO = "maximum_short_squeeze_ratio";
public Price core_exchange_rate; public static final String KEY_CORE_EXCHANGE_RATE = "core_exchange_rate";
private Price settlement_price;
private long maintenance_collateral_ratio;
private long maximum_short_squeeze_ratio;
private Price core_exchange_rate;
public AssetFeed(Price settlementPrice, long maintenanceCollateralRatio, long maximumShortSqueezeRatio, Price coreExchangeRate){
this.settlement_price = settlementPrice;
this.maintenance_collateral_ratio = maintenanceCollateralRatio;
this.maximum_short_squeeze_ratio = maximumShortSqueezeRatio;
this.core_exchange_rate = coreExchangeRate;
}
public Price getSettlementPrice() {
return settlement_price;
}
public void setSettlementPrice(Price settlement_price) {
this.settlement_price = settlement_price;
}
public long getMaintenanceCollateralRatio() {
return maintenance_collateral_ratio;
}
public void setMaintenanceCollateralRatio(long maintenance_collateral_ratio) {
this.maintenance_collateral_ratio = maintenance_collateral_ratio;
}
public long getMaximumShortSqueezeRatio() {
return maximum_short_squeeze_ratio;
}
public void setMaximumShortSqueezeRatio(long maximum_short_squeeze_ratio) {
this.maximum_short_squeeze_ratio = maximum_short_squeeze_ratio;
}
public Price getCoreExchangeRate() {
return core_exchange_rate;
}
public void setCoreExchangeRate(Price core_exchange_rate) {
this.core_exchange_rate = core_exchange_rate;
}
public static class AssetFeedDeserializer implements JsonDeserializer<AssetFeed> {
@Override
public AssetFeed deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
Price settlementPrice = context.deserialize(jsonObject.get(KEY_SETTLEMENT_PRICE).getAsJsonObject(), Price.class);
long collateralRatio = jsonObject.get(KEY_MAINTENANCE_COLLATERAL_RATIO).getAsLong();
long maximumShortSqueezeRatio = jsonObject.get(KEY_MAXIMUM_SHORT_SQUEEZE_RATIO).getAsLong();
Price coreExchangeRate = context.deserialize(jsonObject.get(KEY_CORE_EXCHANGE_RATE), Price.class);
AssetFeed assetFeed = new AssetFeed(settlementPrice, collateralRatio, maximumShortSqueezeRatio, coreExchangeRate);
return assetFeed;
}
}
} }

View file

@ -7,10 +7,11 @@ import cy.agorise.graphenej.Price;
* This is the representation of the response from the 'get_objects' call with * This is the representation of the response from the 'get_objects' call with
* a 2.4.x id, which will retrieve a 'impl_asset_bitasset_data_type'. * a 2.4.x id, which will retrieve a 'impl_asset_bitasset_data_type'.
* *
* Created by nelson on 1/8/17.
*/ */
public class BitAssetData extends GrapheneObject { public class BitAssetData extends GrapheneObject {
public Object[] feeds; public static final String KEY_CURRENT_FEED = "current_feed";
public ReportedAssetFeed[] feeds;
public AssetFeed current_feed; public AssetFeed current_feed;
public String current_feed_publication_time; public String current_feed_publication_time;
public Object options; public Object options;
@ -22,4 +23,68 @@ public class BitAssetData extends GrapheneObject {
public BitAssetData(String id) { public BitAssetData(String id) {
super(id); super(id);
} }
public ReportedAssetFeed[] getFeeds() {
return feeds;
}
public void setFeeds(ReportedAssetFeed[] feeds) {
this.feeds = feeds;
}
public AssetFeed getCurrentFeed() {
return current_feed;
}
public void setCurrentFeed(AssetFeed current_feed) {
this.current_feed = current_feed;
}
public String getCurrentFeedPublicationTime() {
return current_feed_publication_time;
}
public void setCurrentFeedPublicationTime(String current_feed_publication_time) {
this.current_feed_publication_time = current_feed_publication_time;
}
public Object getOptions() {
return options;
}
public void setOptions(Object options) {
this.options = options;
}
public long getForceSettledVolume() {
return force_settled_volume;
}
public void setForceSettledVolume(long force_settled_volume) {
this.force_settled_volume = force_settled_volume;
}
public boolean isIsPredictionMarket() {
return is_prediction_market;
}
public void setIsPredictionMarket(boolean is_prediction_market) {
this.is_prediction_market = is_prediction_market;
}
public Price getSettlementPrice() {
return settlement_price;
}
public void setSettlementPrice(Price settlementPrice) {
this.settlement_price = settlementPrice;
}
public long getSettlementFund() {
return settlement_fund;
}
public void setSettlementFund(long settlementFund) {
this.settlement_fund = settlementFund;
}
} }

View file

@ -0,0 +1,78 @@
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.JsonParseException;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.Util;
/**
* Witness-provided asset price feed
*/
public class ReportedAssetFeed {
private UserAccount reporter;
private AssetFeed assetFeed;
private Date reportedDate;
public ReportedAssetFeed(UserAccount userAccount, Date date, AssetFeed assetFeed){
this.reporter = userAccount;
this.reportedDate = date;
this.assetFeed = assetFeed;
}
public UserAccount getReporter() {
return reporter;
}
public void setReporter(UserAccount reporter) {
this.reporter = reporter;
}
public AssetFeed getAssetFeed() {
return assetFeed;
}
public void setAssetFeed(AssetFeed assetFeed) {
this.assetFeed = assetFeed;
}
public Date getReportedDate() {
return reportedDate;
}
public void setReportedDate(Date reportedDate) {
this.reportedDate = reportedDate;
}
public static class ReportedAssetFeedDeserializer implements JsonDeserializer<ReportedAssetFeed> {
@Override
public ReportedAssetFeed deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
String userId = array.get(0).getAsString();
JsonArray subArray = (JsonArray) array.get(1);
String dateString = subArray.get(0).getAsString();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Util.TIME_DATE_FORMAT);
Date reportDate = null;
try {
reportDate = simpleDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
AssetFeed assetFeed = context.deserialize(subArray.get(1), AssetFeed.class);
UserAccount userAccount = new UserAccount(userId);
return new ReportedAssetFeed(userAccount, reportDate, assetFeed);
}
}
}

View file

@ -11,6 +11,7 @@ import java.util.List;
import cy.agorise.graphenej.Asset; import cy.agorise.graphenej.Asset;
import cy.agorise.graphenej.GrapheneObject; import cy.agorise.graphenej.GrapheneObject;
import cy.agorise.graphenej.Price;
import cy.agorise.graphenej.UserAccount; import cy.agorise.graphenej.UserAccount;
import cy.agorise.graphenej.interfaces.WitnessResponseListener; import cy.agorise.graphenej.interfaces.WitnessResponseListener;
import cy.agorise.graphenej.models.BaseResponse; import cy.agorise.graphenej.models.BaseResponse;
@ -26,7 +27,7 @@ public class GetObjectsTest extends BaseApiTest{
private final Asset asset = new Asset("1.3.0", "BTS", 5); private final Asset asset = new Asset("1.3.0", "BTS", 5);
private final UserAccount account = new UserAccount("1.2.116354"); private final UserAccount account = new UserAccount("1.2.116354");
private final UserAccount bilthon_25 = new UserAccount("1.2.151069"); private final UserAccount bilthon_25 = new UserAccount("1.2.151069");
private final String bitAssetId = "2.4.13"; private final String[] bitAssetIds = new String[]{"2.4.21", "2.4.83"};
@Test @Test
public void testGetAsset(){ public void testGetAsset(){
@ -112,20 +113,38 @@ public class GetObjectsTest extends BaseApiTest{
public void testBitAssetData(){ public void testBitAssetData(){
try{ try{
ArrayList<String> ids = new ArrayList<>(); ArrayList<String> ids = new ArrayList<>();
ids.add(bitAssetId); for(String bitAssetId : bitAssetIds){
ids.add(bitAssetId);
}
mWebSocket.addListener(new GetObjects(ids, new WitnessResponseListener() { mWebSocket.addListener(new GetObjects(ids, new WitnessResponseListener() {
@Override @Override
public void onSuccess(WitnessResponse response) { public void onSuccess(WitnessResponse response) {
System.out.println("onSuccess"); System.out.println("onSuccess");
List<GrapheneObject> list = (List<GrapheneObject>) response.result; List<BitAssetData> list = (List<BitAssetData>) response.result;
BitAssetData bitAssetData = (BitAssetData) list.get(0); BitAssetData bitAssetData1 = list.get(0);
System.out.println("feed time: " + bitAssetData.current_feed_publication_time); BitAssetData bitAssetData2 = list.get(1);
Price price1 = bitAssetData1.getCurrentFeed().getSettlementPrice();
Price price2 = bitAssetData2.getCurrentFeed().getSettlementPrice();
System.out.println("Bitasset data 1");
System.out.println("Price 1: "+price1.toString());
System.out.println("Bitasset data 2");
System.out.println("Price 1: "+price2.toString());
synchronized (GetObjectsTest.this){
GetObjectsTest.this.notifyAll();
}
} }
@Override @Override
public void onError(BaseResponse.Error error) { public void onError(BaseResponse.Error error) {
System.out.println("onError"); System.out.println("onError");
synchronized (GetObjectsTest.this){
GetObjectsTest.this.notifyAll();
}
} }
})); }));