List of usage examples for com.google.gson JsonObject isJsonObject
public boolean isJsonObject()
From source file:main.summonerinfo.SummonerInfo.java
License:Apache License
/** * Opens connection to the Riot API, gets data in JSON, decodes it and assigns it to a variable. * @throws IOException//from ww w.ja v a 2 s . co m */ private void getAccountInfo() throws IOException { JsonObject json; URL request; request = new URL("https://" + region + ".api.pvp.net/api/lol/" + region + "/v1.4/summoner/by-name/" + formatNameForWeb(summonerName) + "?api_key=" + getAPIKey()); HttpURLConnection connect = (HttpURLConnection) request.openConnection(); setResponseCode(connect.getResponseCode()); if (connect.getResponseCode() == 401) { return; } if (connect.getResponseCode() == 404) { return; } BufferedReader in = new BufferedReader(new InputStreamReader(connect.getInputStream(), "UTF-8")); JsonParser parser = new JsonParser(); JsonElement element = parser.parse(in); json = element.getAsJsonObject(); if (json.isJsonObject()) { accountInfo = json.getAsJsonObject(formatNameForJson(summonerName)); } in.close(); }
From source file:org.coinspark.wallet.CSBalanceDatabase.java
License:Open Source License
private void processBalanceUpdateList(List<CSBalanceDatabase.CSBalanceUpdate> balanceUpdates, Map<String, Integer> TxDepthMap) { if (assetDB == null) { return;//from w w w . j a va2 s. co m } if (balanceUpdates.isEmpty()) { return; } List<Integer> assetIDs = new ArrayList<Integer>(); List<CSAsset> assets = new ArrayList<CSAsset>(); List<CSTransactionOutput> txOuts = new ArrayList<CSTransactionOutput>(); List<String> servers = new ArrayList<String>(); for (CSBalanceUpdate bu : balanceUpdates) { if (assetIDs.indexOf(bu.assetID) < 0) { assetIDs.add(bu.assetID); } } for (Integer assetID1 : assetIDs) { int assetID = assetID1; CSAsset asset = assetDB.getAsset(assetID); boolean takeit = false; if (asset != null) { asset.selectCoinsparkTrackerUrl(); if (asset.getCoinsparkTrackerUrl() != null) { if (servers.indexOf(asset.getCoinsparkTrackerUrl()) < 0) { servers.add(asset.getCoinsparkTrackerUrl()); } takeit = true; } } if (takeit) { assets.add(asset); } } if (assets.isEmpty()) { return; } assetIDs.clear(); for (CSAsset asset : assets) { assetIDs.add(asset.getAssetID()); } int[] idMap = new int[assets.size()]; for (int s = 0; s < servers.size(); s++) { CSBalanceDatabase.JRequest request = new CSBalanceDatabase.JRequest(); request.id = (int) (new Date().getTime() / 1000); request.jsonrpc = "2.0"; request.method = "coinspark_assets_get_qty"; request.params = new CSBalanceDatabase.JRequestParams(); request.params.assets = new String[assets.size()]; int c = 0; for (int i = 0; i < assets.size(); i++) { idMap[i] = -1; if (servers.get(s).equals(assets.get(i).getCoinsparkTrackerUrl())) { CSAsset asset = assets.get(i); request.params.assets[c] = asset.getGenTxID(); idMap[i] = c; c++; } } CSTransactionOutput lastTxOut = null; txOuts.clear(); for (int i = 0; i < balanceUpdates.size(); i++) { CSBalanceDatabase.CSBalanceUpdate bu = balanceUpdates.get(i); bu.assetIDInRequest = assetIDs.indexOf(bu.assetID); if (bu.assetIDInRequest >= 0) { if (idMap[bu.assetIDInRequest] >= 0) { bu.oldBalance = new CSBalanceEntry(bu.balance); bu.serverIDInRequest = s; balanceUpdates.set(i, bu); if (!bu.txOut.equals(lastTxOut)) { txOuts.add(bu.txOut); lastTxOut = bu.txOut; } } } } if (!txOuts.isEmpty()) { request.params.txouts = new CSBalanceDatabase.JRequestTxOut[txOuts.size()]; for (int i = 0; i < txOuts.size(); i++) { CSTransactionOutput txout = txOuts.get(i); request.params.txouts[i] = new CSBalanceDatabase.JRequestTxOut(txout.getTxID().toString(), txout.getIndex()); } JsonObject jresult = null; try { // Use Google GSON library for Java Object <--> JSON conversions Gson gson = new Gson(); // convert java object to JSON format, String json = gson.toJson(request); try { JsonElement jelement; JsonObject jobject = null; Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "application/json"); CSUtils.CSDownloadedURL downloaded = CSUtils.postURL(servers.get(s), 15, null, json, headers); if (downloaded.error != null) { log.error(downloaded.error); } else { jelement = new JsonParser().parse(downloaded.contents); jobject = jelement.getAsJsonObject(); } if (jobject != null) { if ((jobject.get("id") == null) || jobject.get("id").getAsInt() != request.id) { log.error("Tracker: id doesn't match " + request.id); } else { if ((jobject.get("error") != null)) { if (jobject.get("error").isJsonObject()) { JsonObject jerror = jobject.get("error").getAsJsonObject(); String errorMessage = "Query error: "; if (jerror.get("code") != null) { errorMessage += jerror.get("code").getAsInt() + " - "; } if (jerror.get("message") != null) { errorMessage += jerror.get("message").getAsString(); } log.error("Tracker: " + errorMessage); } else { log.error("Tracker: Query error"); } } else { jresult = jobject.getAsJsonObject("result"); if (jresult != null) { if (!jresult.isJsonObject()) { log.error("Tracker: result object is not array"); jresult = null; } } } } } } catch (JsonSyntaxException ex) { log.error("Tracker, JSON syntax " + ex.getClass().getName() + " " + ex.getMessage()); } catch (Exception ex) { log.error("Tracker, exception " + ex.getClass().getName() + " " + ex.getMessage()); } } catch (Exception ex) { log.error("Tracker: " + ex.getClass().getName() + " " + ex.getMessage()); } if (jresult == null) { for (int i = 0; i < balanceUpdates.size(); i++) { CSBalanceDatabase.CSBalanceUpdate bu = balanceUpdates.get(i); if (bu.serverIDInRequest == s) { bu.oldBalance = null; bu.serverIDInRequest = -1; balanceUpdates.set(i, bu); } } } else { for (int i = 0; i < balanceUpdates.size(); i++) { CSBalanceDatabase.CSBalanceUpdate bu = balanceUpdates.get(i); if (bu.serverIDInRequest == s) { try { if (bu.qtyBTC == null) { if ((jresult.get("BTC") != null) && (jresult.get("BTC").isJsonArray())) { JsonArray jarray = jresult.getAsJsonArray("BTC"); for (int j = 0; j < jarray.size(); j++) { JsonObject jentry = jarray.get(j).getAsJsonObject(); if (jentry.get("txid") != null) { String txID = jentry.get("txid").toString(); txID = txID.substring(1, 65); if ((jentry.get("vout") != null) && bu.txOut.equals(txID, jentry.get("vout").getAsInt())) { if ((jentry.get("error") == null) && (jentry.get("qty") != null)) { bu.qtyBTC = new BigInteger(jentry.get("qty").getAsString()); } } } } } } String genTxID = assets.get(bu.assetIDInRequest).getGenTxID(); if ((jresult.get(genTxID) != null) && (jresult.get(genTxID).isJsonArray())) { JsonArray jarray = jresult.getAsJsonArray(genTxID); for (int j = 0; j < jarray.size(); j++) { JsonObject jentry = jarray.get(j).getAsJsonObject(); if (jentry.get("txid") != null) { String txID = jentry.get("txid").toString(); txID = txID.substring(1, 65); if ((jentry.get("vout") != null) && bu.txOut.equals(txID, jentry.get("vout").getAsInt())) { if ((jentry.get("error") != null) || (jentry.get("qty") == null)) { if ((bu.balance.balanceState != CSBalance.CSBalanceState.SELF) && (bu.balance.balanceState != CSBalance.CSBalanceState.CALCULATED)) { bu.balance.setQty(bu.balance.qty, CSBalance.CSBalanceState.UNKNOWN); } else { bu.balance.setQty(bu.balance.qty, bu.balance.balanceState); } if (TxDepthMap != null) { if (TxDepthMap.containsKey(txID)) { if (TxDepthMap .get(txID) > BALANCE_DB_MAXIMAL_UNKNOWN_DEPTH) { bu.balance.setQty(BigInteger.ZERO, CSBalance.CSBalanceState.ZERO); csLog.info( "Balance DB: Tx is too deep in block chain (depth " + TxDepthMap.get(txID) + ": " + bu.txOut.getTxID().toString() + "-" + bu.txOut.getIndex() + "-" + bu.assetID + " - setting UNKNOWN state to ZERO"); } } } } else { if ((jentry.get("spent") != null) && (jentry.get("spent").getAsInt() > 0)) { bu.balance.setQty( new BigInteger(jentry.get("qty").getAsString()), CSBalance.CSBalanceState.SPENT); } else { if (jentry.get("qty").getAsLong() > 0) { bu.balance.setQty( new BigInteger(jentry.get("qty").getAsString()), CSBalance.CSBalanceState.VALID); CSEventBus.INSTANCE.postAsyncEvent( CSEventType.BALANCE_VALID, new CSBalance(bu.txOut, bu.assetID, bu.balance.qty, bu.balance.qtyChecked, bu.balance.qtyFailures, bu.balance.balanceState)); } else { bu.balance.setQty( new BigInteger(jentry.get("qty").getAsString()), CSBalance.CSBalanceState.ZERO); } } } } } } } csLog.info("Balance DB: Update: " + bu.txOut.getTxID().toString() + "-" + bu.txOut.getIndex() + "-" + bu.assetID + "-" + bu.balance.qty + "-" + bu.balance.qtyChecked + "-" + bu.balance.qtyFailures + "-" + bu.balance.balanceState); } catch (Exception ex) { log.error("Tracker: " + ex.getClass().getName() + " " + ex.getMessage()); bu.oldBalance = null; } balanceUpdates.set(i, bu); } } } } } }
From source file:org.eclipse.smarthome.binding.digitalstrom.internal.lib.manager.impl.DeviceStatusManagerImpl.java
License:Open Source License
private void setInizialStateWithLastCallScenes() { if (sceneMan == null) { return;//from w w w .java2s .c o m } JsonObject response = connMan.getDigitalSTROMAPI().query2(connMan.getSessionToken(), LAST_CALL_SCENE_QUERY); if (!response.isJsonObject()) { return; } for (Entry<String, JsonElement> entry : response.entrySet()) { if (!entry.getValue().isJsonObject()) { continue; } JsonObject zone = entry.getValue().getAsJsonObject(); int zoneID = -1; short groupID = -1; short sceneID = -1; if (zone.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()) != null) { zoneID = zone.get(JSONApiResponseKeysEnum.ZONE_ID.getKey()).getAsInt(); } for (Entry<String, JsonElement> groupEntry : zone.entrySet()) { if (groupEntry.getKey().startsWith("group") && groupEntry.getValue().isJsonObject()) { JsonObject group = groupEntry.getValue().getAsJsonObject(); if (group.get(JSONApiResponseKeysEnum.DEVICES.getKey()) != null) { if (group.get(JSONApiResponseKeysEnum.GROUP.getKey()) != null) { groupID = group.get(JSONApiResponseKeysEnum.GROUP.getKey()).getAsShort(); } if (group.get(JSONApiResponseKeysEnum.LAST_CALL_SCENE.getKey()) != null) { sceneID = group.get(JSONApiResponseKeysEnum.LAST_CALL_SCENE.getKey()).getAsShort(); } if (zoneID > -1 && groupID > -1 && sceneID > -1) { logger.debug("initial state, call scene {}-{}-{}", zoneID, groupID, sceneID); sceneMan.callInternalSceneWithoutDiscovery(zoneID, groupID, sceneID); } } } } } }
From source file:org.openscore.content.json.actions.GetValueFromObject.java
License:Open Source License
private JsonElement getObject(JsonObject jsonObject, String[] keys, int startIndex) throws Exception { if (startIndex >= keys.length) { return jsonObject; }/* w ww . j a va 2s . c o m*/ String aKey = keys[startIndex]; if (jsonObject.isJsonObject()) { //base case if (keys.length == 0) return getValue(jsonObject, aKey); //recursive call else { final JsonObject newJsonObject; final JsonElement valueFromKey = getValue(jsonObject, aKey); if (valueFromKey instanceof JsonObject) { newJsonObject = (JsonObject) valueFromKey; } else { return valueFromKey; } return getObject(newJsonObject, keys, ++startIndex); } } else { throw new Exception("The key does not exist in JavaScript object!"); } }
From source file:org.wso2.carbon.appfactory.s4.integration.StratosRestService.java
License:Open Source License
private JsonObject getSubscribedCartridge(String cartridgeAlias) throws AppFactoryException { HttpClient httpClient = getNewHttpClient(); JsonObject catridgeJson = null;/* w ww .j a v a 2 s .co m*/ try { String serviceEndPoint = stratosManagerURL + LIST_DETAILS_OF_SUBSCRIBED_CARTRIDGE + cartridgeAlias; DomainMappingResponse response = doGet(httpClient, serviceEndPoint); if (HttpStatus.SC_OK == response.getStatusCode()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved the subscription info"); } GsonBuilder gsonBuilder = new GsonBuilder(); JsonParser jsonParser = new JsonParser(); JsonObject subscriptionInfo = jsonParser.parse(response.getResponse()).getAsJsonObject(); if (subscriptionInfo != null && subscriptionInfo.isJsonObject()) { JsonElement catridge = subscriptionInfo.get("cartridge"); if (catridge.isJsonObject()) { catridgeJson = catridge.getAsJsonObject(); } } } } catch (Exception e) { handleException("Error occurred while getting subscription info", e); } return catridgeJson; }