More specific deserializer for the Asset class

This commit is contained in:
Nelson R. Perez 2016-12-13 13:20:41 -05:00
parent f8cb55ffb5
commit e2154d8428
3 changed files with 46 additions and 3 deletions

View file

@ -1,9 +1,19 @@
package de.bitsharesmunich.graphenej; package de.bitsharesmunich.graphenej;
import com.google.gson.*;
import java.lang.reflect.Type;
/** /**
* Created by nelson on 11/9/16. * Created by nelson on 11/9/16.
*/ */
public class Asset extends GrapheneObject { 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 id;
private String symbol; private String symbol;
private int precision; private int precision;
@ -11,16 +21,48 @@ public class Asset extends GrapheneObject {
private String dynamic_asset_data_id; private String dynamic_asset_data_id;
private AssetOptions options; private AssetOptions options;
/**
* Simple constructor
* @param id
*/
public Asset(String id) { public Asset(String id) {
super(id); super(id);
this.id = 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(){ public String getSymbol(){
return this.symbol; return this.symbol;
} }
public String getId(){ public int getPrecision(){
return this.id; 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<Asset> {
@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);
}
} }
} }

View file

@ -792,7 +792,7 @@ public class Test {
System.out.println("onSuccess"); System.out.println("onSuccess");
WitnessResponse<List<Asset>> resp = response; WitnessResponse<List<Asset>> resp = response;
for(Asset asset : resp.result){ for(Asset asset : resp.result){
System.out.println("Asset: "+asset.getId()+", Symbol: "+asset.getSymbol()); System.out.println("Asset: "+asset.getObjectId()+", Symbol: "+asset.getSymbol());
} }
} }

View file

@ -47,6 +47,7 @@ public class LookupAssetSymbols extends WebSocketAdapter {
System.out.println("<<< "+response); System.out.println("<<< "+response);
GsonBuilder gsonBuilder = new GsonBuilder(); GsonBuilder gsonBuilder = new GsonBuilder();
Type LookupAssetSymbolsResponse = new TypeToken<WitnessResponse<List<Asset>>>(){}.getType(); Type LookupAssetSymbolsResponse = new TypeToken<WitnessResponse<List<Asset>>>(){}.getType();
gsonBuilder.registerTypeAdapter(Asset.class, new Asset.AssetDeserializer());
WitnessResponse<List<Asset>> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse); WitnessResponse<List<Asset>> witnessResponse = gsonBuilder.create().fromJson(response, LookupAssetSymbolsResponse);
mListener.onSuccess(witnessResponse); mListener.onSuccess(witnessResponse);
} }