Example usage for com.google.gson JsonObject add

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

Introduction

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

Prototype

public void add(String property, JsonElement value) 

Source Link

Document

Adds a member, which is a name-value pair, to self.

Usage

From source file:aopdomotics.storage.GroceryBill.java

/**
 * Return JSON for all food items on bill
 * @return // w  w  w  .j ava2s.  c o  m
 */
public JsonObject getJson() {
    //Convert food items to json
    JsonObject storage = new JsonObject();

    JsonObject foodItems = new JsonObject();
    for (Food item : foods) {
        JsonElement jsonItem = new JsonObject();
        foodItems.addProperty(item.getClass().getSimpleName().toLowerCase(), item.buyQuantity());
    }

    storage.add("Bill", foodItems);
    return storage;
}

From source file:aopdomotics.storage.recipe.Recipe.java

public JsonObject getJson() {
    JsonObject storage = new JsonObject();

    JsonObject foodItems = new JsonObject();

    foodItems.addProperty(component1.getClass().getSimpleName().toLowerCase(), component1.getQuantity());
    foodItems.addProperty(component2.getClass().getSimpleName().toLowerCase(), component2.getQuantity());
    foodItems.addProperty(component3.getClass().getSimpleName().toLowerCase(), component3.getQuantity());

    storage.add("Recipe", foodItems);
    return storage;
}

From source file:app.abhijit.iter.data.source.StudentDataSource.java

License:Open Source License

private void processStudentSubjects(String response) {
    mFetchStudentSubjectsAsyncTask = null;

    JsonObject student = mJsonParser.parse(mLocalStore.getString(mSelectedRegistrationNumber, null))
            .getAsJsonObject();/*from   w  ww.  j  a v  a  2  s  .c o  m*/
    JsonObject subjects = new JsonObject();

    try {
        JsonArray studentSubjects = mJsonParser.parse(response).getAsJsonArray();
        for (int i = 0; i < studentSubjects.size(); i++) {
            if (!studentSubjects.get(i).getAsJsonObject().has("subjectcode")
                    || !studentSubjects.get(i).getAsJsonObject().has("subject")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalpresentclass")
                    || !studentSubjects.get(i).getAsJsonObject().has("totalclasses")) {
                continue;
            }
            String code = studentSubjects.get(i).getAsJsonObject().get("subjectcode").getAsString();
            String name = studentSubjects.get(i).getAsJsonObject().get("subject").getAsString();
            int presentClasses = studentSubjects.get(i).getAsJsonObject().get("totalpresentclass").getAsInt();
            int totalClasses = studentSubjects.get(i).getAsJsonObject().get("totalclasses").getAsInt();
            if (totalClasses <= 0 || presentClasses < 0 || presentClasses > totalClasses)
                continue;
            long lastUpdated = new Date().getTime();
            int oldPresentClasses = presentClasses;
            int oldTotalClasses = totalClasses;
            if (student.has(STUDENT_SUBJECTS_KEY)
                    && student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().has(code)) {
                oldPresentClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_PRESENT_CLASSES_KEY).getAsInt();
                oldTotalClasses = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                        .getAsJsonObject().get(STUDENT_SUBJECT_TOTAL_CLASSES_KEY).getAsInt();
                if (presentClasses == oldPresentClasses && totalClasses == oldTotalClasses) {
                    lastUpdated = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject().get(code)
                            .getAsJsonObject().get(STUDENT_SUBJECT_LAST_UPDATED_KEY).getAsLong();
                }
            }
            JsonObject subject = new JsonObject();
            subject.addProperty(STUDENT_SUBJECT_CODE_KEY, code);
            subject.addProperty(STUDENT_SUBJECT_NAME_KEY, name);
            subject.addProperty(STUDENT_SUBJECT_PRESENT_CLASSES_KEY, presentClasses);
            subject.addProperty(STUDENT_SUBJECT_TOTAL_CLASSES_KEY, totalClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_PRESENT_CLASSES_KEY, oldPresentClasses);
            subject.addProperty(STUDENT_SUBJECT_OLD_TOTAL_CLASSES_KEY, oldTotalClasses);
            subject.addProperty(STUDENT_SUBJECT_LAST_UPDATED_KEY, lastUpdated);
            subjects.add(code, subject);
        }
    } catch (Exception e) {
        if (student.has(STUDENT_SUBJECTS_KEY)) {
            subjects = student.get(STUDENT_SUBJECTS_KEY).getAsJsonObject();
        } else {
            subjects = new JsonObject();
        }
    }

    student.add(STUDENT_SUBJECTS_KEY, subjects);
    student.addProperty(STUDENT_TIMESTAMP_KEY, new Date().getTime());
    mLocalStore.edit().putString(mSelectedRegistrationNumber, student.toString()).apply();

    fetch();
}

From source file:ar.com.ws.djnextension.config.GsonBuilderConfiguratorForTesting.java

License:Open Source License

/**
 * @param parent/*w  w  w.j  a v  a 2 s . c o m*/
 * @param elementName
 * @param value
 */
private static void setIntValue(JsonObject parent, String elementName, int value) {
    parent.add(elementName, new JsonPrimitive(Integer.valueOf(value)));
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

@Override
protected void defaults() {
    super.defaults();

    JsonObject subscribe = new JsonObject();
    subscribe.add("port", new JsonPrimitive(9000));
    subscribe.add("scheme", new JsonPrimitive("ws"));
    subscribe.add("path", new JsonPrimitive("/sparql"));
    parameters.add("subscribe", subscribe);

    JsonObject securesubscribe = new JsonObject();
    securesubscribe.add("port", new JsonPrimitive(9443));
    securesubscribe.add("scheme", new JsonPrimitive("wss"));
    securesubscribe.add("path", new JsonPrimitive("/secure/sparql"));
    parameters.add("securesubscribe", securesubscribe);

    JsonObject secureUpdate = new JsonObject();
    secureUpdate.add("port", new JsonPrimitive(8443));
    secureUpdate.add("scheme", new JsonPrimitive("https"));
    parameters.add("secureupdate", secureUpdate);

    JsonObject secureQuery = new JsonObject();
    secureQuery.add("port", new JsonPrimitive(8443));
    secureQuery.add("scheme", new JsonPrimitive("https"));
    parameters.add("securequery", secureQuery);

    JsonObject register = new JsonObject();
    register.add("register", new JsonPrimitive("/oauth/register"));
    register.add("requesttoken", new JsonPrimitive("/oauth/token"));
    register.add("port", new JsonPrimitive(8443));
    register.add("scheme", new JsonPrimitive("https"));
    parameters.add("authorizationserver", register);
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

/**
 * Sets the credentials./*from   ww  w.  j a  va2  s.  com*/
 *
 * @param id the username
 * @param secret the password
 * @throws IOException 
 */
public void setCredentials(String id, String secret) throws IOException {
    logger.debug("Set credentials Id: " + id + " Secret:" + secret);

    this.id = id;
    this.secret = secret;

    try {
        authorization = new BASE64Encoder().encode((id + ":" + secret).getBytes("UTF-8"));

        //TODO need a "\n", why?
        authorization = authorization.replace("\n", "");
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage());
    }

    //Save on file the encrypted version   
    if (parameters.get("security") == null) {
        JsonObject credentials = new JsonObject();
        credentials.add("client_id", new JsonPrimitive(SEPAEncryption.encrypt(id)));
        credentials.add("client_secret", new JsonPrimitive(SEPAEncryption.encrypt(secret)));
        parameters.add("security", credentials);
    } else {
        parameters.get("security").getAsJsonObject().add("client_id",
                new JsonPrimitive(SEPAEncryption.encrypt(id)));
        parameters.get("security").getAsJsonObject().add("client_secret",
                new JsonPrimitive(SEPAEncryption.encrypt(secret)));
    }

    storeProperties(propertiesFile);
}

From source file:arces.unibo.SEPA.client.api.SPARQL11SEProperties.java

License:Open Source License

/**
 * Sets the JWT./*from  w w w  .jav  a 2s  . c o  m*/
 *
 * @param jwt the JSON Web Token
 * @param expires the date when the token will expire
 * @param type the token type (e.g., bearer)
 * @throws IOException 
 */
public void setJWT(String jwt, Date expires, String type) throws IOException {

    this.jwt = jwt;
    this.expires = expires.getTime();
    this.tokenType = type;

    //Save on file the encrypted version
    if (parameters.get("security") == null) {
        JsonObject credentials = new JsonObject();
        credentials.add("jwt", new JsonPrimitive(SEPAEncryption.encrypt(jwt)));
        credentials.add("expires",
                new JsonPrimitive(SEPAEncryption.encrypt(String.format("%d", expires.getTime()))));
        credentials.add("type", new JsonPrimitive(SEPAEncryption.encrypt(type)));
        parameters.add("security", credentials);
    } else {
        parameters.get("security").getAsJsonObject().add("jwt", new JsonPrimitive(SEPAEncryption.encrypt(jwt)));
        parameters.get("security").getAsJsonObject().add("expires",
                new JsonPrimitive(SEPAEncryption.encrypt(String.format("%d", expires.getTime()))));
        parameters.get("security").getAsJsonObject().add("type",
                new JsonPrimitive(SEPAEncryption.encrypt(type)));
    }

    storeProperties(propertiesFile);
}

From source file:arces.unibo.SEPA.client.api.WebsocketClientEndpoint.java

License:Open Source License

private void sendSubscribeRequest() {
    if (wsClientSession == null) {
        logger.error("Session is null");
        return;/*from  w w w  .  ja v a 2s.c om*/
    }

    try {
        JsonObject request = new JsonObject();
        request.add("subscribe", new JsonPrimitive(sparql));
        if (alias != null)
            request.add("alias", new JsonPrimitive(alias));
        else
            logger.debug("Alias is null");
        if (jwt != null)
            request.add("authorization", new JsonPrimitive(jwt));
        else
            logger.debug("Authorization is null");
        logger.debug(request.toString());

        wsClientSession.getBasicRemote().sendText(request.toString());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:arces.unibo.SEPA.client.api.WebsocketClientEndpoint.java

License:Open Source License

public void unsubscribe(String spuid, String jwt) throws IOException, URISyntaxException {
    logger.debug("unsubscribe");

    if (!isConnected())
        try {/*from w ww. ja  va2 s .  co  m*/
            connect();
        } catch (DeploymentException e) {
            throw new IOException(e.getMessage());
        }

    JsonObject request = new JsonObject();
    if (spuid != null)
        request.add("unsubscribe", new JsonPrimitive(spuid));
    if (jwt != null)
        request.add("authorization", new JsonPrimitive(jwt));

    wsClientSession.getBasicRemote().sendText(request.toString());
}

From source file:arces.unibo.SEPA.commons.protocol.SPARQL11Properties.java

License:Open Source License

protected void defaults() {
    parameters.add("host", new JsonPrimitive("localhost"));
    parameters.add("port", new JsonPrimitive(9999));
    parameters.add("scheme", new JsonPrimitive("http"));
    parameters.add("path", new JsonPrimitive("/blazegraph/namespace/kb/sparql"));

    JsonObject query = new JsonObject();
    query.add("method", new JsonPrimitive("POST"));
    query.add("format", new JsonPrimitive("JSON"));
    parameters.add("query", query);

    JsonObject update = new JsonObject();
    update.add("method", new JsonPrimitive("URL_ENCODED_POST"));
    update.add("format", new JsonPrimitive("HTML"));
    parameters.add("update", update);

    doc.add("parameters", parameters);
}