/** * Produce a JSONObject by combining a JSONArray of names with the values of * this JSONArray. * * @param names * A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.isEmpty() || this.isEmpty()) { return null; } JSONObject jo = new JSONObject(names.length()); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * * @param names * A JSONArray containing a list of key strings. This determines * the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException * If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.isEmpty()) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** * Produce a JSONObject by combining a JSONArray of names with the values of * this JSONArray. * * @param names * A JSONArray containing a list of key strings. These will be * paired with the values. * @return A JSONObject, or null if there are no names or if this JSONArray * has no values. * @throws JSONException * If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.isEmpty() || this.isEmpty()) { return null; } JSONObject jo = new JSONObject(names.length()); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/** * Produce a JSONArray containing the values of the members of this * JSONObject. * * @param names * A JSONArray containing a list of key strings. This determines * the sequence of the values in the result. * @return A JSONArray of values. * @throws JSONException * If any of the values are non-finite numbers. */ public JSONArray toJSONArray(JSONArray names) throws JSONException { if (names == null || names.isEmpty()) { return null; } JSONArray ja = new JSONArray(); for (int i = 0; i < names.length(); i += 1) { ja.put(this.opt(names.getString(i))); } return ja; }
/** * {@inheritDoc} */ @Override public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException { logger.debug("Got notification callback {}", payload.toString()); if (payload.isEmpty()) { return; } // Test for order error callback // [0,"n",[null,"on-req",null,null,[null,null,1513970684865000,"tBTCUSD",null,null,0.001,0.001,"EXCHANGE MARKET",null,null,null,null,null,null,null,12940,null,null,null,null,null,null,0,null,null],null,"ERROR","Invalid order: minimum size for BTC/USD is 0.002"]] if ("on-req".equals(payload.getString(1))) { final String state = payload.optString(6); if ("ERROR".equals(state)) { BitfinexSubmittedOrder exchangeOrder = jsonToBitfinexSubmittedOrder(payload); submittedOrderConsumer.accept(symbol, exchangeOrder); } } }
/** * {@inheritDoc} */ @Override public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException { logger.info("Got order callback {}", payload.toString()); // No orders active if (payload.isEmpty()) { eventConsumer.accept(symbol, Lists.newArrayList()); return; } // Snapshot or update List<BitfinexSubmittedOrder> orders = Lists.newArrayList(); if (payload.get(0) instanceof JSONArray) { for (int orderPos = 0; orderPos < payload.length(); orderPos++) { final JSONArray orderArray = payload.getJSONArray(orderPos); BitfinexSubmittedOrder exchangeOrder = jsonToBitfinexSubmittedOrder(orderArray); orders.add(exchangeOrder); } } else { BitfinexSubmittedOrder exchangeOrder = jsonToBitfinexSubmittedOrder(payload); orders.add(exchangeOrder); } eventConsumer.accept(symbol, orders); }
/** * {@inheritDoc} */ @Override public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException { logger.info("Got position callback {}", payload.toString()); ArrayList<BitfinexPosition> positions = Lists.newArrayList(); // No positions active if (payload.isEmpty()) { positionConsumer.accept(symbol, positions); return; } if (payload.get(0) instanceof JSONArray) { // snapshot for (int orderPos = 0; orderPos < payload.length(); orderPos++) { final JSONArray orderArray = payload.getJSONArray(orderPos); BitfinexPosition position = jsonArrayToPosition(orderArray); positions.add(position); } } else { // update BitfinexPosition position = jsonArrayToPosition(payload); positions.add(position); } positionConsumer.accept(symbol, positions); }
/** * {@inheritDoc} */ @Override public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException { if (payload.isEmpty()) { return; } // channel symbol trade:1m:tLTCUSD final Set<BitfinexCandle> candlestickList = new TreeSet<>(Comparator.comparing(BitfinexCandle::getTimestamp)); // Snapshots contain multiple Bars, Updates only one if (payload.get(0) instanceof JSONArray) { for (int pos = 0; pos < payload.length(); pos++) { final JSONArray parts = payload.getJSONArray(pos); BitfinexCandle candlestick = jsonToCandlestick(parts); candlestickList.add(candlestick); } } else { BitfinexCandle candlestick = jsonToCandlestick(payload); candlestickList.add(candlestick); } candlesConsumer.accept(symbol, candlestickList); }
/** * {@inheritDoc} */ @Override public void handleChannelData(final String action, final JSONArray payload) throws BitfinexClientException { List<BitfinexWallet> wallets = Lists.newArrayList(); if (payload.isEmpty()) { walletConsumer.accept(symbol, wallets); return; } if (payload.get(0) instanceof JSONArray) { // snapshot for (int walletPos = 0; walletPos < payload.length(); walletPos++) { final JSONArray walletArray = payload.getJSONArray(walletPos); BitfinexWallet wallet = jsonArrayToWallet(walletArray); wallets.add(wallet); } } else { // update BitfinexWallet wallet = jsonArrayToWallet(payload); wallets.add(wallet); } walletConsumer.accept(symbol, wallets); }
@Override protected Map<String, Number> parseStream(InputStream stream) { Configuration c = find(JsonpProvider.class).getConfiguration(); DocumentContext ctx = JsonPath.parse(stream, c); setUptime(findUptime(ctx)); Map<String, Number> vars = new HashMap<>(collectKeys.size()); for (Map.Entry<JsonPath, String> e: collectKeys.entrySet()) { try { JSONArray v = protectedRead(ctx, e.getKey()); if (v.isEmpty()) { log(Level.ERROR, "Failed to collect data '%s': not found", e.getValue()); break; } vars.put(e.getValue(), valueToNum(v.get(0))); } catch (PathNotFoundException ex) { log(Level.ERROR, "Failed to collect data '%s': not found", e.getValue()); } catch (JSONException ex) { log(Level.ERROR, "Failed to collect data '%s': %s", e.getValue(), ex.getMessage()); } } return vars; }
} else if (object instanceof JSONArray) { JSONArray array = (JSONArray) object; if (array.isEmpty()) { String path = parentPath.stream().collect(Collectors.joining(".")); leafNodesByPath.put(path, new FlattenedDocumentLeafNode(documentKey, path, FragmentType.EMPTY_ARRAY, null, fragmentCapacity));