Avoid unchecked casts when deserializing JSON using Gson

This commit is contained in:
Steven Barclay 2019-11-15 22:52:06 +00:00
parent 09a964ff27
commit 77a8c20ce9
No known key found for this signature in database
GPG Key ID: 9FED6BF1176D500B
2 changed files with 9 additions and 10 deletions

View File

@ -47,16 +47,15 @@ public class FeeProvider extends HttpClientProvider {
public Tuple2<Map<String, Long>, Map<String, Long>> getFees() throws IOException {
String json = httpClient.requestWithGET("getFees", "User-Agent", "bisq/" + Version.VERSION + ", uid:" + httpClient.getUid());
LinkedTreeMap<String, Object> linkedTreeMap = new Gson().<LinkedTreeMap<String, Object>>fromJson(json, LinkedTreeMap.class);
LinkedTreeMap<?, ?> linkedTreeMap = new Gson().fromJson(json, LinkedTreeMap.class);
Map<String, Long> tsMap = new HashMap<>();
tsMap.put("bitcoinFeesTs", ((Double) linkedTreeMap.get("bitcoinFeesTs")).longValue());
Map<String, Long> map = new HashMap<>();
try {
//noinspection unchecked
LinkedTreeMap<String, Double> dataMap = (LinkedTreeMap<String, Double>) linkedTreeMap.get("dataMap");
Long btcTxFee = dataMap.get("btcTxFee").longValue();
LinkedTreeMap<?, ?> dataMap = (LinkedTreeMap<?, ?>) linkedTreeMap.get("dataMap");
Long btcTxFee = ((Double) dataMap.get("btcTxFee")).longValue();
map.put("BTC", btcTxFee);
} catch (Throwable t) {

View File

@ -50,20 +50,20 @@ public class PriceProvider extends HttpClientProvider {
String json = httpClient.requestWithGET("getAllMarketPrices", "User-Agent", "bisq/"
+ Version.VERSION + ", uid:" + httpClient.getUid());
LinkedTreeMap<String, Object> map = new Gson().<LinkedTreeMap<String, Object>>fromJson(json, LinkedTreeMap.class);
LinkedTreeMap<?, ?> map = new Gson().fromJson(json, LinkedTreeMap.class);
Map<String, Long> tsMap = new HashMap<>();
tsMap.put("btcAverageTs", ((Double) map.get("btcAverageTs")).longValue());
tsMap.put("poloniexTs", ((Double) map.get("poloniexTs")).longValue());
tsMap.put("coinmarketcapTs", ((Double) map.get("coinmarketcapTs")).longValue());
//noinspection unchecked
List<LinkedTreeMap<String, Object>> list = (ArrayList<LinkedTreeMap<String, Object>>) map.get("data");
list.forEach(treeMap -> {
List<?> list = (ArrayList<?>) map.get("data");
list.forEach(obj -> {
try {
LinkedTreeMap<?, ?> treeMap = (LinkedTreeMap<?, ?>) obj;
final String currencyCode = (String) treeMap.get("currencyCode");
final double price = (double) treeMap.get("price");
final double price = (Double) treeMap.get("price");
// json uses double for our timestampSec long value...
final long timestampSec = MathUtils.doubleToLong((double) treeMap.get("timestampSec"));
final long timestampSec = MathUtils.doubleToLong((Double) treeMap.get("timestampSec"));
marketPriceMap.put(currencyCode, new MarketPrice(currencyCode, price, timestampSec, true));
} catch (Throwable t) {
log.error(t.toString());