List of usage examples for javax.json JsonValue getValueType
ValueType getValueType();
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BigDecimal jsonDouble(JsonValue value) throws BtcException { if ((value == null) || (value.getValueType() == JsonValue.ValueType.NULL)) { return null; }/*from w w w . j a va 2 s . c o m*/ if ((value.getValueType() == JsonValue.ValueType.NUMBER) && (value instanceof JsonNumber)) { return ((JsonNumber) value).bigDecimalValue(); } throw new BtcException(BtcException.BTC4J_ERROR_CODE, BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_INVALID_TYPE + value.getValueType()); }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public JsonObject jsonObject(JsonValue value) throws BtcException { if ((value == null) || (value.getValueType() == JsonValue.ValueType.NULL)) { return null; }/*from ww w . j a va 2 s. c o m*/ if ((value.getValueType() == JsonValue.ValueType.OBJECT) && (value instanceof JsonObject)) { return (JsonObject) value; } throw new BtcException(BtcException.BTC4J_ERROR_CODE, BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_INVALID_TYPE + value.getValueType()); }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcCoinbase jsonCoinbase(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/* w ww. j ava2 s.c o m*/ BtcCoinbase coin = new BtcCoinbase(); Map<String, String> auxiliary = new HashMap<String, String>(); JsonValue aux = object.get(BTCOBJ_COIN_AUX); if ((aux != null) && (aux.getValueType() == JsonValue.ValueType.OBJECT) && (aux instanceof JsonObject)) { JsonObject auxObject = (JsonObject) aux; for (String key : auxObject.keySet()) { auxiliary.put(key, auxObject.getString(key, "")); } } coin.setAux(auxiliary); coin.setValue(jsonDouble(object, BTCOBJ_COIN_VALUE)); return coin; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcLastBlock jsonLastBlock(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/* w w w . j av a 2s . com*/ BtcLastBlock lastBlock = new BtcLastBlock(); lastBlock.setLastBlock(object.getString(BTCOBJ_LAST_BLOCK_LAST_BLOCK, "")); List<BtcTransaction> transactions = new ArrayList<BtcTransaction>(); JsonValue txs = object.get(BTCOBJ_LAST_BLOCK_TRANSACTIONS); if ((txs != null) && (txs.getValueType() == JsonValue.ValueType.ARRAY) && (txs instanceof JsonArray)) { JsonArray txsArray = (JsonArray) txs; for (JsonValue tx : txsArray.getValuesAs(JsonValue.class)) { transactions.add(jsonTransaction(tx)); } } lastBlock.setTransactions(transactions); return lastBlock; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcScript jsonScript(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/*w w w.j a v a2 s. c o m*/ BtcScript script = new BtcScript(); script.setAsm(object.getString(BTCOBJ_SCRIPT_ASM, "")); script.setPublicKey(object.getString(BTCOBJ_SCRIPT_PUBLIC_KEY, "")); script.setRequiredSignatures(jsonLong(object, BTCOBJ_SCRIPT_REQUIRED_SIGNATURES)); script.setType(BtcScript.Type.getValue(object.getString(BTCOBJ_SCRIPT_TYPE, ""))); List<String> addresses = new ArrayList<String>(); JsonValue addrs = object.get(BTCOBJ_SCRIPT_ADDRESSES); if ((addrs) != null && (addrs.getValueType() == JsonValue.ValueType.ARRAY) && (addrs instanceof JsonArray)) { JsonArray addrsArray = (JsonArray) addrs; for (JsonValue addr : addrsArray.getValuesAs(JsonValue.class)) { addresses.add(jsonString(addr)); } } script.setAddresses(addresses); return script; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcAddedNode jsonAddedNode(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/*from w w w . j a va2 s . com*/ BtcAddedNode addedNode = new BtcAddedNode(); addedNode.setAddedNode(object.getString(BTCOBJ_NODE_ADDED_NODE, "")); addedNode.setConnected(object.getBoolean(BTCOBJ_NODE_CONNECTED, false)); List<BtcNode> nodes = new ArrayList<BtcNode>(); JsonValue addresses = object.get(BTCOBJ_NODE_ADDRESSES); if ((addresses != null) && (addresses.getValueType() == JsonValue.ValueType.ARRAY) && (addresses instanceof JsonArray)) { JsonArray addressesArray = (JsonArray) addresses; for (JsonValue address : addressesArray.getValuesAs(JsonValue.class)) { nodes.add(jsonNode(address)); } } addedNode.setAddresses(nodes); return addedNode; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public JsonValue invoke(String method, JsonArray parameters) throws BtcException { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add(JSONRPC_REALM, JSONRPC_VERSION).add(JSONRPC_METHOD, method); if (parameters != null) { builder.add(JSONRPC_PARAMS, parameters); } else {/* ww w .j ava 2s . com*/ builder.addNull(JSONRPC_PARAMS); } String guid = UUID.randomUUID().toString(); builder.add(JSONRPC_ID, guid); JsonObject request = builder.build(); JsonObject response = jsonObject(jsonValue(jsonInvoke(String.valueOf(request)))); if (response == null) { LOG.severe(BTC4J_DAEMON_DATA_NULL_JSON); throw new BtcException(BtcException.BTC4J_ERROR_CODE, BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_NULL_JSON); } if (!(guid.equals(jsonId(response)))) { LOG.severe(BTC4J_DAEMON_DATA_INVALID_ID); throw new BtcException(BtcException.BTC4J_ERROR_CODE, BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_INVALID_ID); } JsonValue error = response.get(JSONRPC_ERROR); if ((error != null) && (error.getValueType().equals(ValueType.OBJECT))) { JsonObject errorObj = (JsonObject) error; int code = errorObj.getInt(JSONRPC_CODE); String message = errorObj.getString(JSONRPC_MESSAGE); JsonObject data = (JsonObject) errorObj.get(JSONRPC_DATA); String dataStr = (data == null) ? "" : (" " + String.valueOf(data)); LOG.severe("error: " + code + " " + message + dataStr); throw new BtcException(code, message + dataStr); } return response.get(JSONRPC_RESULT); }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcInput jsonInput(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/*from w w w .ja va2 s .c o m*/ BtcInput input = new BtcInput(); input.setTransaction(object.getString(BTCOBJ_TX_INPUT_TRANSACTION, "")); input.setOutput(jsonLong(object, BTCOBJ_TX_INPUT_OUTPUT)); JsonValue script = object.get(BTCOBJ_TX_INPUT_SCRIPT_SIGNATURE); if ((script != null) && (script.getValueType() == JsonValue.ValueType.OBJECT) && (script instanceof JsonObject)) { input.setScript(jsonScript(script)); } input.setSequence(jsonLong(object, BTCOBJ_TX_INPUT_SEQUENCE)); return input; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcTransaction jsonTransaction(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }/*from w ww . j a v a2 s . co m*/ BtcTransaction transaction = new BtcTransaction(); transaction.setTransaction(object.getString(BTCOBJ_TX_TRANSACTION, "")); transaction.setAmount(jsonDouble(object, BTCOBJ_TX_AMOUNT)); transaction.setFee(jsonDouble(object, BTCOBJ_TX_FEE)); transaction.setConfirmations(jsonLong(object, BTCOBJ_TX_CONFIRMATIONS)); transaction.setTime(jsonLong(object, BTCOBJ_TX_TIME)); transaction.setTimeReceived(jsonLong(object, BTCOBJ_TX_TIME_RECEIVED)); transaction.setBlockHash(object.getString(BTCOBJ_TX_BLOCK_HASH, "")); transaction.setBlockIndex(jsonLong(object, BTCOBJ_TX_BLOCK_INDEX)); transaction.setBlockTime(jsonLong(object, BTCOBJ_TX_BLOCK_TIME)); List<BtcTransactionDetail> details = new ArrayList<BtcTransactionDetail>(); JsonValue txDetails = object.get(BTCOBJ_TX_DETAILS); if ((txDetails != null) && (txDetails.getValueType() == JsonValue.ValueType.ARRAY) && (txDetails instanceof JsonArray)) { JsonArray txDetailsArray = (JsonArray) txDetails; for (JsonValue txDetail : txDetailsArray.getValuesAs(JsonValue.class)) { details.add(jsonTransactionDetail(txDetail)); } } else { details.add(jsonTransactionDetail(value)); } transaction.setDetails(details); return transaction; }
From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java
public BtcRawTransaction jsonRawTransaction(JsonValue value) throws BtcException { JsonObject object = jsonObject(value); if (object == null) { return null; }//from ww w.j ava2 s . c o m BtcRawTransaction transaction = new BtcRawTransaction(); transaction.setHex(object.getString(BTCOBJ_TX_HEX, "")); transaction.setTransaction(object.getString(BTCOBJ_TX_TRANSACTION, "")); transaction.setVersion(jsonLong(object, BTCOBJ_TX_VERSION)); transaction.setLockTime(jsonLong(object, BTCOBJ_TX_LOCK_TIME)); List<BtcInput> inputTransactions = new ArrayList<BtcInput>(); JsonValue inputs = object.get(BTCOBJ_TX_INPUTS); if ((inputs != null) && (inputs.getValueType() == JsonValue.ValueType.ARRAY) && (inputs instanceof JsonArray)) { JsonArray inputsArray = (JsonArray) inputs; for (JsonValue input : inputsArray.getValuesAs(JsonValue.class)) { inputTransactions.add(jsonInput(input)); } } transaction.setInputs(inputTransactions); List<BtcOutput> outputTransactions = new ArrayList<BtcOutput>(); JsonValue outputs = object.get(BTCOBJ_TX_OUTPUTS); if ((outputs != null) && (outputs.getValueType() == JsonValue.ValueType.ARRAY) && (outputs instanceof JsonArray)) { JsonArray outputsArray = (JsonArray) outputs; for (JsonValue output : outputsArray.getValuesAs(JsonValue.class)) { outputTransactions.add(jsonOutput(output)); } } transaction.setOutputs(outputTransactions); transaction.setBlockHash(object.getString(BTCOBJ_TX_BLOCK_HASH, "")); transaction.setConfirmations(jsonLong(object, BTCOBJ_TX_CONFIRMATIONS)); transaction.setTime(jsonLong(object, BTCOBJ_TX_TIME)); transaction.setBlockTime(jsonLong(object, BTCOBJ_TX_BLOCK_TIME)); transaction.setComplete(object.getBoolean(BTCOBJ_TX_COMPLETE, true)); return transaction; }