Example usage for javax.json JsonObjectBuilder addNull

List of usage examples for javax.json JsonObjectBuilder addNull

Introduction

In this page you can find the example usage for javax.json JsonObjectBuilder addNull.

Prototype

JsonObjectBuilder addNull(String name);

Source Link

Document

Adds a name/ JsonValue#NULL pair to the JSON object associated with this object builder where the value is null .

Usage

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 {/*from w  w  w.  ja v  a  2 s .  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:be.fedict.dcat.datagovbe.Drupal.java

/**
 * Add a dataset to Drupal form/* w  w w .  java2s .  c om*/
 * 
 * @param builder
 * @param dataset
 * @param lang
 * @throws RepositoryException
 */
private void addDataset(JsonObjectBuilder builder, Map<IRI, ListMultimap<String, String>> dataset, String lang)
        throws RepositoryException {
    String id = getOne(dataset, DCTERMS.IDENTIFIER, "");
    String title = stripTags(getOne(dataset, DCTERMS.TITLE, lang));

    // Just copy the title if description is empty
    String desc = getOne(dataset, DCTERMS.DESCRIPTION, lang);
    desc = (desc.isEmpty()) ? title : stripTags(desc);

    // Max size for Drupal title
    if (title.length() > Drupal.LEN_TITLE) {
        logger.warn("Title {} too long", title);
        title = ellipsis(title, Drupal.LEN_TITLE);
    }

    Date modif = getModif(dataset);

    String keywords = getKeywords(dataset, lang);
    // Max size for Drupal keywords
    if (keywords.length() > Drupal.LEN_KEYWORDS) {
        logger.warn("Keywords {} too long", keywords);
        keywords = ellipsis(keywords, Drupal.LEN_KEYWORDS);
    }

    Map<IRI, ListMultimap<String, String>> publ = getPublisher(dataset);
    JsonArrayBuilder emails = fieldArrayJson(getDatasetMails(dataset));
    JsonArrayBuilder orgs = fieldArrayJson(getDatasetOrgs(dataset, lang));

    builder.add(Drupal.TYPE, Drupal.TYPE_DATA).add(Drupal.LANGUAGE, lang)
            .add(Drupal.AUTHOR, Json.createObjectBuilder().add(Drupal.ID, userid)).add(Drupal.TITLE, title)
            .add(Drupal.BODY,
                    Json.createObjectBuilder().add(Drupal.VALUE, desc).add(Drupal.SUMMARY, "")
                            .add(Drupal.FORMAT, Drupal.FORMAT_HTML))
            .add(Drupal.FLD_UPSTAMP, modif.getTime() / 1000L)
            .add(Drupal.FLD_FREQ, arrayTermsJson(dataset, DATAGOVBE.FREQ))
            .add(Drupal.FLD_CAT, arrayTermsJson(dataset, DATAGOVBE.THEME))
            .add(Drupal.FLD_GEO, arrayTermsJson(dataset, DATAGOVBE.SPATIAL))
            .add(Drupal.FLD_PUBLISHER, arrayTermsJson(publ, DATAGOVBE.ORG)).add(Drupal.FLD_ORG, orgs)
            .add(Drupal.FLD_MAIL, emails).add(Drupal.FLD_KEYWORDS, keywords).add(Drupal.FLD_ID, id);

    String fromtill = getOne(dataset, DCTERMS.TEMPORAL, "");
    if (fromtill.isEmpty()) {
        builder.addNull(Drupal.FLD_TIME);
    } else {
        builder.add(Drupal.FLD_TIME, fromtill);
    }
}