Example usage for javax.json JsonObject get

List of usage examples for javax.json JsonObject get

Introduction

In this page you can find the example usage for javax.json JsonObject get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Iterable<JsonValue> getNodeChildrenByName(JsonValue node, XSElementDeclaration childElementDeclaration)
        throws SAXException {
    String name = childElementDeclaration.getName();
    if (DEBUG)//from  ww  w . ja v  a  2  s. c o m
        log.debug("getChildrenByName() childname [" + name + "] isParentOfSingleMultipleOccurringChildElement ["
                + isParentOfSingleMultipleOccurringChildElement() + "] isMultipleOccuringChildElement ["
                + isMultipleOccurringChildElement(name) + "] node [" + node + "]");
    try {
        if (!(node instanceof JsonObject)) {
            if (DEBUG)
                log.debug("getChildrenByName() parent node is not a JsonObject, but a ["
                        + node.getClass().getName() + "]");
            return null;
        }
        JsonObject o = (JsonObject) node;
        if (!o.containsKey(name)) {
            if (DEBUG)
                log.debug("getChildrenByName() no children named [" + name + "] node [" + node + "]");
            return null;
        }
        JsonValue child = o.get(name);
        List<JsonValue> result = new LinkedList<JsonValue>();
        if (child instanceof JsonArray) {
            if (DEBUG)
                log.debug("getChildrenByName() child named [" + name
                        + "] is a JsonArray, current node insertElementContainerElements ["
                        + insertElementContainerElements + "]");
            // if it could be necessary to insert elementContainers, we cannot return them as a list of individual elements now, because then the containing element would be duplicated
            // we also cannot use the isSingleMultipleOccurringChildElement, because it is not valid yet
            if (!isMultipleOccurringChildElement(name)) {
                if (insertElementContainerElements || !strictSyntax) {
                    result.add(child);
                    if (DEBUG)
                        log.debug("getChildrenByName() singleMultipleOccurringChildElement [" + name
                                + "] returning array node (insertElementContainerElements=true)");
                } else {
                    throw new SAXException(MSG_EXPECTED_SINGLE_ELEMENT + " [" + name + "]");
                }
            } else {
                if (DEBUG)
                    log.debug("getChildrenByName() childname [" + name
                            + "] returning elements of array node (insertElementContainerElements=false or not singleMultipleOccurringChildElement)");
                result.addAll((JsonArray) child);
            }
            return result;
        }
        result.add(child);
        if (DEBUG)
            log.debug("getChildrenByName() name [" + name + "] returning [" + child + "]");
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:org.erstudio.guper.websocket.GuperWSEndpoint.java

@OnMessage
public void onWebSocketMessage(JsonObject json, Session session)
        throws WebSocketMessageException, MessageException, LocationException, IOException, EncodeException {
    System.out.println("onWebSocketMessage " + session.getId());

    if (!json.containsKey("type")) {
        throw new WebSocketMessageException("Type is undefined");
    }//  w ww .j  ava 2  s .  c  o m

    String type = json.getString("type");
    if (StringUtils.isBlank(type)) {
        throw new WebSocketMessageException("Empty message type");
    }

    if (!json.containsKey("message")) {
        throw new WebSocketMessageException("Message is undefined");
    }

    String message = json.get("message").toString();
    if (StringUtils.isBlank(message)) {
        throw new WebSocketMessageException("Empty message");
    }

    switch (type) {
    case "SEND_MESSAGE": {
        try {
            Message m = mapper.readValue(message, Message.class);
            onSendMessage(m, session);
        } catch (IOException ex) {
            throw new WebSocketMessageException("Incorrect Message format: " + message);
        }
    }
        break;

    case "CHANGE_LOCATION": {
        try {
            Location l = mapper.readValue(message, Location.class);
            onChangeLocation(l, session);
        } catch (IOException ex) {
            throw new WebSocketMessageException("Incorrect Location format: " + message);
        }
    }
        break;

    default:
        throw new WebSocketMessageException("Incorrect message type: " + type);
    }

}

From source file:org.ocelotds.integration.AbstractOcelotTest.java

/**
 * Becareful result is not unmarshalled/*from w w  w  . ja v a  2 s  . com*/
 *
 * @param json
 * @return
 */
protected static MessageToClient mtcFromJson(String json) {
    try (JsonReader reader = Json.createReader(new StringReader(json))) {
        JsonObject root = reader.readObject();
        MessageToClient message = new MessageToClient();
        message.setId(root.getString(Constants.Message.ID));
        message.setTime(root.getInt(Constants.Message.TIME));
        message.setType(MessageType.valueOf(root.getString(Constants.Message.TYPE)));
        message.setDeadline(root.getInt(Constants.Message.DEADLINE));
        if (null != message.getType()) {
            switch (message.getType()) {
            case FAULT:
                JsonObject faultJs = root.getJsonObject(Constants.Message.RESPONSE);
                Fault f = Fault.createFromJson(faultJs.toString());
                message.setFault(f);
                break;
            case MESSAGE:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                message.setType(MessageType.MESSAGE);
                break;
            case CONSTRAINT:
                JsonArray result = root.getJsonArray(Constants.Message.RESPONSE);
                List<ConstraintViolation> list = new ArrayList<>();
                for (JsonValue jsonValue : result) {
                    list.add(getJava(ConstraintViolation.class, ((JsonObject) jsonValue).toString()));
                }
                message.setConstraints(list.toArray(new ConstraintViolation[] {}));
                break;
            default:
                message.setResult("" + root.get(Constants.Message.RESPONSE));
                break;
            }
        }
        return message;
    }
}

From source file:edu.harvard.hms.dbmi.bd2k.irct.ri.exac.EXACResourceImplementation.java

private String getValue(JsonObject obj, String field) {
    if (field.contains(".")) {
        String thisField = field.split("\\.")[0];
        String remaining = field.replaceFirst(thisField + ".", "");
        return getValue(obj.getJsonObject(thisField), remaining);
    }/*ww w.  ja v a2 s .  com*/
    if (obj.containsKey(field)) {
        ValueType vt = obj.get(field).getValueType();
        if (vt == ValueType.NUMBER) {
            return obj.getJsonNumber(field).toString();
        } else if (vt == ValueType.TRUE) {
            return "TRUE";
        } else if (vt == ValueType.FALSE) {
            return "FALSE";
        }
        return obj.getString(field);
    }
    return "";
}

From source file:nl.nn.adapterframework.align.Json2Xml.java

@Override
public Map<String, String> getAttributes(XSElementDeclaration elementDeclaration, JsonValue node)
        throws SAXException {
    if (!readAttributes) {
        return null;
    }//from w  ww  .  j a  v a 2s  .  c om
    if (!(node instanceof JsonObject)) {
        if (DEBUG)
            log.debug("getAttributes() parent node is not a JsonObject, but a [" + node.getClass().getName()
                    + "] isParentOfSingleMultipleOccurringChildElement ["
                    + isParentOfSingleMultipleOccurringChildElement() + "]  value [" + node
                    + "], returning null");
        return null;
    }
    JsonObject o = (JsonObject) node;
    if (o.isEmpty()) {
        if (DEBUG)
            log.debug("getAttributes() no children");
        return null;
    }
    try {
        Map<String, String> result = new HashMap<String, String>();
        for (String key : o.keySet()) {
            if (key.startsWith(attributePrefix)) {
                String attributeName = key.substring(attributePrefix.length());
                String value = getText(elementDeclaration, o.get(key));
                if (DEBUG)
                    log.debug("getAttributes() attribute [" + attributeName + "] = [" + value + "]");
                result.put(attributeName, value);
            }
        }
        return result;
    } catch (JsonException e) {
        throw new SAXException(e);
    }
}

From source file:com.floreantpos.ui.views.payment.SettleTicketDialog.java

public void submitMyKalaDiscount() {
    if (ticket.hasProperty(LOYALTY_ID)) {
        POSMessageDialog.showError(Application.getPosWindow(), Messages.getString("SettleTicketDialog.18")); //$NON-NLS-1$
        return;/*from w  w w . j  a  va  2 s  . c  om*/
    }

    try {
        String loyaltyid = JOptionPane.showInputDialog(Messages.getString("SettleTicketDialog.19")); //$NON-NLS-1$

        if (StringUtils.isEmpty(loyaltyid)) {
            return;
        }

        ticket.addProperty(LOYALTY_ID, loyaltyid);

        String transactionURL = buildLoyaltyApiURL(ticket, loyaltyid);

        String string = IOUtils.toString(new URL(transactionURL).openStream());

        JsonReader reader = Json.createReader(new StringReader(string));
        JsonObject object = reader.readObject();
        JsonArray jsonArray = (JsonArray) object.get("discounts"); //$NON-NLS-1$
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = (JsonObject) jsonArray.get(i);
            addCoupon(ticket, jsonObject);
        }

        updateModel();

        OrderController.saveOrder(ticket);

        POSMessageDialog.showMessage(Application.getPosWindow(), Messages.getString("SettleTicketDialog.21")); //$NON-NLS-1$
        paymentView.updateView();
    } catch (Exception e) {
        POSMessageDialog.showError(Application.getPosWindow(), Messages.getString("SettleTicketDialog.22"), e); //$NON-NLS-1$
    }
}

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 w w  .  j  a  v  a2s  .  c om*/
    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 BtcInput jsonInput(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }/*w  w w  .ja v  a 2 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 BtcLastBlock jsonLastBlock(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }//from  w ww .j a va 2s  . c  om
    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 BtcAddedNode jsonAddedNode(JsonValue value) throws BtcException {
    JsonObject object = jsonObject(value);
    if (object == null) {
        return null;
    }//from w w w  . j av a  2s . co m
    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;
}