Example usage for com.google.gson JsonArray add

List of usage examples for com.google.gson JsonArray add

Introduction

In this page you can find the example usage for com.google.gson JsonArray add.

Prototype

public void add(JsonElement element) 

Source Link

Document

Adds the specified element to self.

Usage

From source file:com.claresco.tinman.json.XapiPersonJson.java

License:Open Source License

@Override
public JsonElement serialize(XapiPerson arg0, Type arg1, JsonSerializationContext arg2) {
    JsonObject theResult = new JsonObject();

    theResult.addProperty("objectType", "Person");

    if (arg0.hasNames()) {
        JsonArray theNamesJson = JsonUtility.convertToJsonArray(arg0.getNames());

        if (theNamesJson.size() > 0) {
            theResult.add("name", theNamesJson);
        }//from  w ww  .j a  v  a  2 s.com
    }

    if (arg0.hasMboxes()) {
        JsonArray theMboxesJson = JsonUtility.convertToJsonArrayFromIRIList(arg0.getMboxes());

        if (theMboxesJson.size() > 0) {
            theResult.add("mbox", theMboxesJson);
        }

    }

    if (arg0.hasMboxSha1sums()) {
        JsonArray theMboxSha1sumsJson = JsonUtility.convertToJsonArray(arg0.getMboxSha1sums());

        if (theMboxSha1sumsJson.size() > 0) {
            theResult.add("mbox_sha1sum", theMboxSha1sumsJson);
        }
    }

    if (arg0.hasOpendIDs()) {
        JsonArray theOpenIDsJson = JsonUtility.convertToJsonArray(arg0.getOpenIDs());

        if (theOpenIDsJson.size() > 0) {
            theResult.add("openid", theOpenIDsJson);
        }
    }

    if (arg0.hasAccounts()) {
        ArrayList<XapiAccount> theAccounts = arg0.getAccounts();

        JsonArray theAccountsJson = new JsonArray();

        for (XapiAccount a : theAccounts) {
            theAccountsJson.add(arg2.serialize(a, XapiAccount.class));
        }

        if (theAccountsJson.size() > 0) {
            theResult.add("account", theAccountsJson);
        }
    }

    return theResult;
}

From source file:com.claresco.tinman.json.XapiStatementBatchJson.java

License:Open Source License

@Override
public JsonElement serialize(XapiStatementBatch arg0, Type arg1, JsonSerializationContext arg2) {
    JsonArray theStatementArray = new JsonArray();

    if (arg0.size() == 1) {
        JsonElement theStatement = arg2.serialize(arg0.getStatementAtIndex(0), XapiStatement.class);
        theStatementArray.add(theStatement);

        return theStatementArray;
    } else {//from  w  ww  .j a va2 s . c o m
        for (XapiStatement s : arg0) {
            theStatementArray.add(arg2.serialize(s, XapiStatement.class));
        }
        return theStatementArray;
    }
}

From source file:com.claresco.tinman.servlet.XapiServletUtility.java

License:Open Source License

protected static String createJsonArray(ArrayList<String> theData) {
    JsonArray theArray = new JsonArray();

    for (String s : theData) {
        theArray.add(new JsonPrimitive(s));
    }//from   ww  w .  j  a  va2  s  .co m

    return theArray.toString();
}

From source file:com.claresco.tinman.servlet.XapiServletUtility.java

License:Open Source License

protected static String createJsonArray(Set<String> theData) {
    JsonArray theArray = new JsonArray();

    for (String s : theData) {
        theArray.add(new JsonPrimitive(s));
    }/*  w w w .j av  a2s  .c  om*/

    return theArray.toString();
}

From source file:com.cloud.agent.transport.ArrayTypeAdaptor.java

License:Apache License

@Override
public JsonElement serialize(T[] src, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray array = new JsonArray();
    for (T cmd : src) {
        JsonObject obj = new JsonObject();
        obj.add(cmd.getClass().getName().substring(s_pkg.length()), _gson.toJsonTree(cmd));
        array.add(obj);
    }//from   w ww  .ja  v  a2s . c o m

    return array;
}

From source file:com.cloudant.client.api.Database.java

License:Open Source License

/**
 * Set permissions for a user/apiKey on this database.
 * <p>/*from w  w w  . j a  v a 2s.  com*/
 * Note this method is only applicable to databases that support the
 * <a href="http://docs.cloudant.com/authorization.html">
 * Cloudant authorization API
 * </a> such as Cloudant DBaaS. For unsupported databases consider using the /db/_security
 * endpoint.
 * </p>
 *
 * @param userNameorApikey
 * @param permissions      permissions to grant
 * @throws UnsupportedOperationException if called on a database that does not provide the
 *                                       Cloudant authorization API
 * @see <a href="http://docs.cloudant.com/authorization.html#roles">Roles</a>
 * @see <a href="http://docs.cloudant.com/authorization.html#modifying-permissions">Modifying
 * permissions</a>
 */
public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) {
    assertNotEmpty(userNameorApikey, "userNameorApikey");
    assertNotEmpty(permissions, "permissions");
    final JsonArray jsonPermissions = new JsonArray();
    for (Permissions s : permissions) {
        final JsonPrimitive permission = new JsonPrimitive(s.toString());
        jsonPermissions.add(permission);
    }

    // get existing permissions
    JsonObject perms = getPermissionsObject();

    // now set back
    JsonElement elem = perms.get("cloudant");
    if (elem == null) {
        perms.addProperty("_id", "_security");
        elem = new JsonObject();
        perms.add("cloudant", elem);
    }
    elem.getAsJsonObject().add(userNameorApikey, jsonPermissions);

    HttpResponse response = null;
    HttpPut put = new HttpPut(apiV2DBSecurityURI);
    setEntity(put, client.getGson().toJson(perms), "application/json");
    try {
        response = executeRequest(put);
        String ok = getAsString(response, "ok");
        if (!ok.equalsIgnoreCase("true")) {
            //raise exception
        }
    } finally {
        close(response);
    }
}

From source file:com.cloudant.client.api.DatabaseImpl.java

License:Open Source License

@Override
public void setPermissions(String userNameorApikey, EnumSet<Permissions> permissions) {
    assertNotEmpty(userNameorApikey, "userNameorApikey");
    assertNotEmpty(permissions, "permissions");
    final JsonArray jsonPermissions = new JsonArray();
    for (Permissions s : permissions) {
        final JsonPrimitive permission = new JsonPrimitive(s.toString());
        jsonPermissions.add(permission);
    }// w  ww .  ja  v a2s . c o  m

    // get existing permissions
    JsonObject perms = getPermissionsObject();

    // now set back
    JsonElement elem = perms.get("cloudant");
    if (elem == null) {
        perms.addProperty("_id", "_security");
        elem = new JsonObject();
        perms.add("cloudant", elem);
    }
    elem.getAsJsonObject().add(userNameorApikey, jsonPermissions);

    InputStream response = null;
    HttpConnection put = Http.PUT(apiV2DBSecurityURI, "application/json");
    put.setRequestBody(client.getGson().toJson(perms));
    try {
        response = client.couchDbClient.executeToInputStream(put);
        String ok = getAsString(response, "ok");
        if (!ok.equalsIgnoreCase("true")) {
            //raise exception
        }
    } finally {
        close(response);
    }
}

From source file:com.cloudant.client.api.DatabaseImpl.java

License:Open Source License

/**
 * Form a create index json from parameters
 *//*from ww  w. jav  a  2 s. co  m*/
private JsonObject getIndexDefinition(String indexName, String designDocName, String indexType,
        IndexField[] fields) {
    assertNotEmpty(fields, "index fields");
    JsonObject indexObject = new JsonObject();
    if (!(indexName == null || indexName.isEmpty())) {
        indexObject.addProperty("name", indexName);
    }
    if (!(designDocName == null || designDocName.isEmpty())) {
        indexObject.addProperty("ddoc", designDocName);
    }
    if (!(indexType == null || indexType.isEmpty())) {

        indexObject.addProperty("type", indexType);
    }

    JsonArray fieldsArray = new JsonArray();
    for (int i = 0; i < fields.length; i++) {
        JsonObject fieldObject = new JsonObject();
        fieldObject.addProperty(fields[i].getName(), fields[i].getOrder().toString());
        fieldsArray.add(fieldObject);
    }
    JsonObject arrayOfFields = new JsonObject();
    arrayOfFields.add("fields", fieldsArray);
    indexObject.add("index", arrayOfFields);

    return indexObject;
}

From source file:com.cloudant.client.api.DatabaseImpl.java

License:Open Source License

private JsonObject getFindByIndexBody(String selectorJson, FindByIndexOptions options) {

    JsonArray fieldsArray = new JsonArray();
    if (options.getFields().size() > 0) {
        for (String field : options.getFields()) {
            JsonPrimitive jsonField = client.getGson().fromJson(field, JsonPrimitive.class);
            fieldsArray.add(jsonField);
        }//w w w .  j  a v a 2  s.  c o  m
    }

    JsonArray sortArray = new JsonArray();
    if (options.getSort().size() > 0) {

        for (IndexField sort : options.getSort()) {
            JsonObject sortObject = new JsonObject();
            sortObject.addProperty(sort.getName(), sort.getOrder().toString());
            sortArray.add(sortObject);
        }
    }

    JsonObject indexObject = new JsonObject();

    //parse and find if valid json issue #28
    JsonObject selectorObject = null;
    boolean isObject = true;
    try {
        selectorObject = getGson().fromJson(selectorJson, JsonObject.class);
    } catch (JsonParseException e) {
        isObject = false;
    }

    if (!isObject) {
        if (selectorJson.startsWith("\"selector\"")) {
            selectorJson = selectorJson.substring(selectorJson.indexOf(":") + 1, selectorJson.length()).trim();
            selectorObject = getGson().fromJson(selectorJson, JsonObject.class);
        } else {
            throw new JsonParseException("selectorJson should be valid json or like " + "\"selector\": {...} ");
        }
    }

    if (selectorObject.has("selector")) {
        indexObject.add("selector", selectorObject.get("selector"));
    } else {
        indexObject.add("selector", selectorObject);
    }

    if (fieldsArray.size() > 0) {
        indexObject.add("fields", fieldsArray);
    }
    if (sortArray.size() > 0) {
        indexObject.add("sort", sortArray);
    }
    if (options.getLimit() != null) {
        indexObject.addProperty("limit", options.getLimit());
    }
    if (options.getSkip() != null) {
        indexObject.addProperty("skip", options.getSkip());
    }
    if (options.getReadQuorum() != null) {
        indexObject.addProperty("r", options.getReadQuorum());
    }
    if (options.getUseIndex() != null) {
        indexObject.add("use_index", getGson().fromJson(options.getUseIndex(), JsonArray.class));
    }

    return indexObject;
}

From source file:com.cloudant.client.api.model.FindByIndexOptions.java

License:Open Source License

/**
 * Specify a specific index to run the query against
 *
 * @param designDocument set the design document to use
 * @param indexName set the index name to use
 * @return this to set additional options
 *//*from w ww.jav  a 2 s .  c o  m*/
public FindByIndexOptions useIndex(String designDocument, String indexName) {
    assertNotNull(designDocument, "designDocument");
    assertNotNull(indexName, "indexName");
    JsonArray index = new JsonArray();
    index.add(new JsonPrimitive(designDocument));
    index.add(new JsonPrimitive(indexName));
    this.useIndex = index;
    return this;
}