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:cashPR.GetCashPaymentHeader.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w w  w. j  av a 2  s . co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection dataConnection = null;

    final JsonObject jResultObj = new JsonObject();
    final String from_date = request.getParameter("from_date");
    final String to_date = request.getParameter("to_date");
    final String v_type = request.getParameter("v_type");
    final String branch_cd = request.getParameter("branch_cd");
    if (dataConnection == null) {
        dataConnection = helper.getConnMpAdmin();
    }

    if (dataConnection != null) {
        try {
            String sql = "select c.AC_CD,c.REF_NO,VDATE,a.FNAME,c1.BAL,c1.REMARK,c.branch_cd from CPRHD c left join CPRDT c1 on c.REF_NO=c1.REF_NO"
                    + " left join ACNTMST a on c.AC_CD=a.AC_CD where VDATE>=? and VDATE<=? and CTYPE=? ";
            if (!branch_cd.equalsIgnoreCase("0")) {
                sql += " and branch_cd=" + branch_cd;
            }
            sql += " order by VDATE,ref_no";
            PreparedStatement pstLocal = dataConnection.prepareStatement(sql);
            pstLocal.setString(1, from_date);
            pstLocal.setString(2, to_date);
            pstLocal.setString(3, v_type);
            ResultSet rsLocal = pstLocal.executeQuery();
            JsonArray array = new JsonArray();
            while (rsLocal.next()) {
                JsonObject object = new JsonObject();
                object.addProperty("REF_NO", rsLocal.getString("REF_NO"));
                object.addProperty("VDATE", rsLocal.getString("VDATE"));
                object.addProperty("FNAME", rsLocal.getString("FNAME"));
                object.addProperty("BAL", rsLocal.getString("BAL"));
                object.addProperty("REMARK", rsLocal.getString("REMARK"));
                object.addProperty("AC_CD", rsLocal.getString("AC_CD"));
                object.addProperty("BRANCH_CD", rsLocal.getString("BRANCH_CD"));
                array.add(object);
            }
            //                response.getWriter().print(array.toString());
            jResultObj.addProperty("result", 1);
            jResultObj.addProperty("Cause", "success");
            jResultObj.add("data", array);
        } catch (SQLNonTransientConnectionException ex1) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", "Server is down");
        } catch (SQLException ex) {
            jResultObj.addProperty("result", -1);
            jResultObj.addProperty("Cause", ex.getMessage());
        }
    }
    response.getWriter().print(jResultObj);
}

From source file:catalog.CloudantUtil.java

License:Apache License

/**
 * Read bulk documents from the cloudant database
 *
 * @throws UnsupportedEncodingException//from   w w  w  . j  a v a2s.  c o  m
 */
public static JsonArray bulkDocuments(Credential credential, JsonArray bulkDocs)
        throws UnsupportedEncodingException {
    JsonObject docs = new JsonObject();
    docs.add("docs", bulkDocs);
    // use GET to get the document
    String dbname = credential.dbname;
    Response response = given().port(443).baseUri(cloudantAccount(credential.user)).auth()
            .basic(credential.user, credential.password).contentType("application/json").body(docs)
            .post("/" + credential.dbname + "/_bulk_docs?include_docs=true");
    String responseStr = response.asString();
    if (responseStr.length() > 500)
        responseStr = responseStr.substring(0, 500);
    System.out.format("Response of get document from database %s: %s\n", dbname, responseStr);
    return (JsonArray) new JsonParser().parse(response.asString());
}

From source file:cc.kave.commons.utils.json.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//from w  ww  .  j av a 2  s .c  o  m

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }

    return new TypeAdapter<R>() {
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            // (kave adaptation) was: ".remove(typeFiledName)"
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);
            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            String label = labelJsonElement.getAsString();
            @SuppressWarnings("unchecked") // registration requires that
            // subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
            if (delegate == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
                        + "; did you forget to register a subtype?");
            }
            return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            if (value == null) {
                Streams.write(null, out);
                return;
            }
            Class<?> srcType = value.getClass();
            String label = subtypeToLabel.get(srcType);
            @SuppressWarnings("unchecked") // registration requires that
            // subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException(
                        "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
            // (kave adaptation) disabled check
            // if (jsonObject.has(typeFieldName)) {
            // throw new JsonParseException("cannot serialize " +
            // srcType.getName()
            // + " because it already defines a field named " +
            // typeFieldName);
            // }
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    };
}

From source file:ccm.pay2spawn.configurator.Configurator.java

License:Open Source License

@Override
public void callback(Object... data) {
    int rewardID = (int) data[0];
    String type = (String) data[1];
    JsonObject newData = (JsonObject) data[2];

    if (rewardID == -1) {
        JsonObject object = new JsonObject();
        object.addProperty("type", type);
        object.add("data", newData);
        rewardData.add(object);//ww w.ja  v a 2  s  . c  om
        rewards.updateUI();
    } else {
        rewardData.get(rewardID).getAsJsonObject().add("data", newData);
        rewards.updateUI();
    }
}

From source file:ccm.pay2spawn.permissions.Group.java

License:Open Source License

public JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    root.addProperty("parent", getParent());

    JsonArray nodes = new JsonArray();
    for (Node node : this.nodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("nodes", nodes);

    return root;//from  w w  w.j  a  v  a  2  s  .  c om
}

From source file:ccm.pay2spawn.permissions.PermissionsDB.java

License:Open Source License

public void save() {
    try {/*from   www  .  ja v a  2 s  .c o m*/
        File file = getFile();
        if (!file.exists()) //noinspection ResultOfMethodCallIgnored
            file.createNewFile();
        JsonObject rootObject = new JsonObject();

        JsonArray players = new JsonArray();
        for (Player player : playerDB.values())
            players.add(player.toJson());
        rootObject.add("players", players);

        JsonArray groups = new JsonArray();
        for (Group group : groupDB.values())
            groups.add(group.toJson());
        rootObject.add("groups", groups);

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(GSON.toJson(rootObject));
        bw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:ccm.pay2spawn.permissions.PermissionsDB.java

License:Open Source License

public void load() throws IOException {
    File file = getFile();// ww  w  .  jav a  2  s  . c  o m
    if (file.exists()) {
        JsonObject rootObject = JSON_PARSER.parse(new FileReader(file)).getAsJsonObject();

        for (JsonElement element : rootObject.getAsJsonArray("players")) {
            Player player = new Player(element.getAsJsonObject());
            playerDB.put(player.getName(), player);
        }

        for (JsonElement element : rootObject.getAsJsonArray("groups")) {
            Group group = new Group(element.getAsJsonObject());
            groupDB.put(group.getName(), group);
        }
    } else {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();
        JsonObject rootObject = new JsonObject();
        rootObject.add("players", new JsonArray());
        rootObject.add("groups", new JsonArray());
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(GSON.toJson(rootObject));
        bw.close();
    }
}

From source file:ccm.pay2spawn.permissions.Player.java

License:Open Source License

public JsonElement toJson() {
    JsonObject root = new JsonObject();
    root.addProperty("name", getName());
    JsonArray groups = new JsonArray();
    for (String group : this.getGroups())
        groups.add(new JsonPrimitive(group));
    root.add("groups", groups);

    JsonArray nodes = new JsonArray();
    for (Node node : this.overrideNodes)
        nodes.add(new JsonPrimitive(node.toString()));
    root.add("overrides", nodes);

    return root;//from   w  w w  . j  av  a2  s . c o m
}

From source file:ccm.pay2spawn.types.guis.HelperGuiBase.java

License:Open Source License

public void storeValue(String key, JsonObject jsonObject, Object value) {
    if (key == null || jsonObject == null)
        return;/*from  w w  w .  j  a  v a 2 s . c o m*/
    if (value == null) {
        jsonObject.add(key, JsonNull.INSTANCE);
        return;
    }
    if (Strings.isNullOrEmpty(value.toString()))
        jsonObject.remove(key);
    else
        jsonObject.addProperty(key,
                typeMap != null && typeMap.containsKey(key) ? typeMap.get(key) + ":" + value.toString()
                        : value.toString());
}

From source file:ccm.pay2spawn.util.JsonNBTHelper.java

License:Open Source License

public static JsonObject parseNBT(NBTTagCompound compound) {
    JsonObject jsonObject = new JsonObject();
    for (Object object : compound.func_150296_c()) {
        jsonObject.add(object.toString(), parseNBT(compound.getTag(object.toString())));
    }/*from w  w  w . j av  a2  s.  c  om*/
    return jsonObject;
}