diff --git a/src/main/java/de/bitsharesmunich/graphenej/Asset.java b/src/main/java/de/bitsharesmunich/graphenej/Asset.java index 261f4e4..59c4a41 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Asset.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Asset.java @@ -1,9 +1,19 @@ package de.bitsharesmunich.graphenej; +import com.google.gson.*; + +import java.lang.reflect.Type; + /** * Created by nelson on 11/9/16. */ public class Asset extends GrapheneObject { + public static final String KEY_ID = "id"; + public static final String KEY_SYMBOL = "symbol"; + public static final String KEY_PRECISION = "precision"; + public static final String KEY_ISSUER = "issuer"; + public static final String KEY_DYNAMIC_ASSET_DATA_ID = "dynamic_asset_data_id"; + private String id; private String symbol; private int precision; @@ -11,16 +21,48 @@ public class Asset extends GrapheneObject { private String dynamic_asset_data_id; private AssetOptions options; + /** + * Simple constructor + * @param id + */ public Asset(String id) { super(id); this.id = id; } + /** + * Constructor + * @param id: The graphene object id. + * @param symbol: The asset symbol. + * @param precision: The asset precision. + */ + public Asset(String id, String symbol, int precision){ + super(id); + this.symbol = symbol; + this.precision = precision; + } + public String getSymbol(){ return this.symbol; } - public String getId(){ - return this.id; + public int getPrecision(){ + return this.precision; + } + + /** + * Custom deserializer used to instantiate a simple version of the Asset class from the response of the + * 'lookup_asset_symbols' API call. + */ + public static class AssetDeserializer implements JsonDeserializer { + + @Override + public Asset deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + JsonObject object = json.getAsJsonObject(); + String id = object.get(KEY_ID).getAsString(); + String symbol = object.get(KEY_SYMBOL).getAsString(); + int precision = object.get(KEY_PRECISION).getAsInt(); + return new Asset(id, symbol, precision); + } } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/Test.java b/src/main/java/de/bitsharesmunich/graphenej/Test.java index b7e3a9a..9bcd9e9 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/Test.java +++ b/src/main/java/de/bitsharesmunich/graphenej/Test.java @@ -792,7 +792,7 @@ public class Test { System.out.println("onSuccess"); WitnessResponse> resp = response; for(Asset asset : resp.result){ - System.out.println("Asset: "+asset.getId()+", Symbol: "+asset.getSymbol()); + System.out.println("Asset: "+asset.getObjectId()+", Symbol: "+asset.getSymbol()); } } diff --git a/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java b/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java index 489e403..ab62e50 100644 --- a/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java +++ b/src/main/java/de/bitsharesmunich/graphenej/api/LookupAssetSymbols.java @@ -47,6 +47,7 @@ public class LookupAssetSymbols extends WebSocketAdapter { System.out.println("<<< "+response); GsonBuilder gsonBuilder = new GsonBuilder(); Type LookupAssetSymbolsResponse = new TypeToken>>(){}.getType(); + gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer()); WitnessResponse> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse); mListener.onSuccess(witnessResponse); }