Example usage for com.google.gson JsonObject JsonObject

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

Introduction

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

Prototype

JsonObject

Source Link

Usage

From source file:adams.data.report.ReportJsonUtils.java

License:Open Source License

/**
 * Turns the report into a json structure.
 *
 * @param report   the report to convert
 * @return      the json data structure//w  w  w . j  a  v  a  2  s.  co m
 */
public static JsonObject toJson(Report report) {
    JsonObject result;

    result = new JsonObject();

    // report
    for (AbstractField field : report.getFields()) {
        switch (field.getDataType()) {
        case NUMERIC:
            result.addProperty(field.getName(), report.getDoubleValue(field));
            break;
        case BOOLEAN:
            result.addProperty(field.getName(), report.getBooleanValue(field));
            break;
        case STRING:
        case UNKNOWN:
            result.addProperty(field.getName(), report.getStringValue(field));
            break;
        default:
            throw new IllegalStateException("Unhandled data type: " + field.getDataType());
        }
    }

    return result;
}

From source file:adams.scripting.processor.JsonProcessor.java

License:Open Source License

/**
 * Turns the command properties and payload into a single string to send.
 *
 * @param header   the header data//from www. j  a v  a 2s  .c o  m
 * @param payload   the payload
 * @return      the assembled string
 */
@Override
public String format(Properties header, byte[] payload) {
    JsonObject result;
    String data;
    JsonObject objHeader;
    Gson gson;

    if (payload.length == 0)
        data = "";
    else
        data = Base64.encodeBase64String(payload);

    objHeader = new JsonObject();
    for (String key : header.keySetAll())
        objHeader.addProperty(key, header.getProperty(key));

    result = new JsonObject();
    result.add("header", objHeader);
    result.addProperty("payload", data);

    if (m_PrettyPrinting) {
        gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(result);
    } else {
        return result.toString();
    }
}

From source file:adminservlets_ads.Apr_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* w w w.  j av a2  s .c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    String alert = null;
    AdminClass_Links al = new AdminClass_Links();

    String reciever = ar.getUserEmail(request.getParameter("to"));
    String subject = "Approval of your ad \"" + request.getParameter("subject") + "\"";
    String view_link = "<a href='" + al.getAdView()
            + "' target='blank'><p>Click here to view your advertisement</p></a>";

    String content = "Your ad \"" + request.getParameter("subject") + "\", successfully posted on Superb.lk.\n"
            + "\n" + "To view your ad, please click the following link. \n"
            + "If you have any questions, feel free to reply to the email and we will get back to you.\n";

    String inbox_content = "Hello,\n" + "\n" + "Your ad \"" + request.getParameter("subject")
            + "\", successfully posted on Superb.lk.\n" + "\n" + "Regards,\n"
            + "The support team at Superb.lk\n" + "\n";

    int inbox_result = am.sendMessage(inbox_content, request.getParameter("to"));

    String action = request.getParameter("action");
    String item = request.getParameter("item");
    int result;
    if ("Approve".equals(action) && item != null) {

        result = ar.approveAd(item); //Approving advertiesment
        int mail_result = as.mailClass(reciever, subject, content, view_link);//sending mail to the user

        if (result == 1) {
            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Approved!</strong>" + " Advertiesment number "
                    + request.getParameter("item") + "";

        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the the failure
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong>" + " Advertiesment number "
                    + request.getParameter("item") + " Try again.";

        }

    }

    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_ads.Load_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from  w  ww .  j  av  a  2s .c o m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReviewAds ar = new AdminClass_ReviewAds();
    ArrayList reviewAds = ar.reviewAds(); //loading ad reviews
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    JsonElement x = gson.toJsonTree(reviewAds);
    myObj.add("reviewAds", x);

    out.write(myObj.toString());
    out.close();

}

From source file:adminservlets_ads.Mod_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from  w  w  w  . j a v a  2 s. c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    AdminClass_Links al = new AdminClass_Links();
    String alert = null;

    if (request.getParameter("subject") != null) {

        String reciever = ar.getUserEmail(request.getParameter("to"));
        String subject = request.getParameter("subject");
        String edit_link = "<a href='" + al.getAdEdit()
                + "' target='blank'><p>Click here to update your advertisement</p></a>";

        String content = request.getParameter("content_header") + "<br/><br/>"
                + request.getParameter("content_body");
        String itemId = request.getParameter("itemname");
        String reason = "Modified due to- " + request.getParameter("reason");

        String inbox_content = request.getParameter("content_header") + request.getParameter("content_body")
                + "\n\nPlease update your ad" + "\n" + "Regards,\n" + "The support team at Superb.lk\n" + "\n";

        int inbox_result = am.sendMessage(inbox_content, request.getParameter("to"));

        int result = ar.modifyAds(itemId, reason); //updating advertiesment status
        int result2 = as.mailClass(reciever, subject, content, edit_link);//sending mail to the user

        if (result == 1 && result2 == 1) {
            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">" + "</i></button><br>Email is sent to "
                    + "<strong> " + reciever + " !</strong>";

        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><strong> Failed !</strong>";

        }
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();

}

From source file:adminservlets_ads.Rmv_ReviewAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   w  w w.j  av  a 2 s .  co  m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_ReviewAds ar = new AdminClass_ReviewAds();
    AdminClass_SendMail as = new AdminClass_SendMail();
    AdminClass_Message am = new AdminClass_Message();
    String alert = null;

    String reciever = ar.getUserEmail(request.getParameter("to"));
    String subject = "Refusal of your Ad \"" + request.getParameter("subject") + "\"";

    String content = "Your ad \"" + request.getParameter("subject") + "\", cannot be posted on Superb.lk.\n"
            + "\n" + "The reason why we could not approve your ad:\n" + "\n"
            + "<br/><br/>- Illegal item or service\n"
            + "<br/>Your ad features an item or service that is illegal or not suitable, which we cannot allow on our site. Please post a new ad with suitable content.\n"
            + "<br/><br/>we do not allow any content that Superb.lk considers in its complete and unfettered discretion to be- \n"
            + "<br/>(i) offensive and/or inappropriate (including, without limitation, defamatory, threatening, hateful, or pornographic content);\n"
            + "\n" + "<br/>(ii) false, fraudulent, misleading or deceptive, or incorrect;\n" + "\n"
            + "<br/>(iv) Content that discloses anothers personal, confidential or proprietary information (including content that identifies a person, or that is capable of being used to identify a person without that person's consent)."
            + "\n"
            + "<br/>If you have any questions, feel free to reply to the email and we will get back to you.\n";

    String inbox_content = "Hello,\n" + "\n" + "Your ad \"" + request.getParameter("subject")
            + "\", cannot be posted on Superb.lk.\n" + "\n" + "The reason why we could not approve your ad:\n"
            + "\n" + "- Illegal item or service\n"
            + "Your ad features an item or service that is illegal or not suitable, which we cannot allow on our site. Please post a new ad with suitable content.\n"
            + "\n" + "Regards,\n" + "The support team at Superb.lk\n" + "\n";

    int inbox_result = am.sendMessage(inbox_content, request.getParameter("to"));

    String action = request.getParameter("action");
    String item = request.getParameter("item");

    int result;

    if (("Remove".equals(action) && item != null)) {
        result = ar.removeAd(item); //removing advertiesment
        int mail_result = as.mailClass(reciever, subject, content, "");//sending mail to the user

        if (result == 1) {
            alert = "<button class=\"btn btn-green\">" //returning notification of the success 
                    + "<i  class=\"glyphicon glyphicon-ok-sign\">"
                    + "</i></button><br><strong>Removed!</strong>" + " Advertiesment number "
                    + request.getParameter("item") + "";
        } else {
            alert = "<button class=\"btn btn-red\">" //returning notification of the failure 
                    + "<i  class=\"glyphicon glyphicon-remove-circle\">"
                    + "</i></button><br><strong>Failed!</strong> Advertiesment number "
                    + request.getParameter("item") + " Try again.";
        }

    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", alert);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.CheckPassJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from   www.  j a  va 2s .  c om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    AdminClass_LoginMethods lm = new AdminClass_LoginMethods();

    boolean result = lm.checkPass(request.getParameter("username"), request.getParameter("password"));
    if (result == true) {
        HttpSession session = request.getSession();
        session.setAttribute("login_change", "true");
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", result);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.CustomDataAdsJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from w  w  w .jav a2 s .  c om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.adsCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Ads : " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Ads: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", result);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.CustomDataJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from  www .  j  a  v  a 2  s . c  om*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.pagevisitCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Site visit: " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Site visit: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", result);

    out.write(myObj.toString());
    out.close();
}

From source file:adminservlets_Json.CustomDataUserJson.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//  w ww  . j  a  v  a  2s.c o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String fd = request.getParameter("fd");
    String sd = request.getParameter("sd");
    String result;
    AdminClass_Overviewstats ao = new AdminClass_Overviewstats();
    boolean checkFD = ao.isValidDate(fd);
    boolean checkSD = ao.isValidDate(sd);

    if (checkFD == true && checkSD == true) {
        ArrayList al = ao.usersCustom(fd, sd);
        DecimalFormat twoDForm = new DecimalFormat("#.#");
        if (al.get(1).equals("0")) {
            result = "Users : " + (String) al.get(1) + " Percentage: 0%";
        } else {
            result = "Users: " + (String) al.get(1) + " Percentage: "
                    + twoDForm.format(
                            Float.parseFloat((String) al.get(1)) / Float.parseFloat((String) al.get(0)) * 100)
                    + "%";
        }

    } else {
        result = "Incorrect entry";
    }
    PrintWriter out = response.getWriter();

    Gson gson = new Gson();
    JsonObject myObj = new JsonObject();

    myObj.addProperty("result", result);

    out.write(myObj.toString());
    out.close();
}