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:com.caris.oscarexchange4j.util.serializers.ThemeSerializer.java

License:Apache License

@Override
public JsonElement serialize(Theme src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject obj = new JsonObject();
    obj.add("id", context.serialize(src.getId()));
    if (src.getName() != null)
        obj.add("name", context.serialize(src.getName()));
    if (src.getCovers() != null)
        obj.add("covers", context.serialize(src.getCovers()));
    if (src.getLayers() != null)
        obj.add("layers", context.serialize(src.getLayers()));
    obj.add("displayOrder", context.serialize(src.getDisplayOrder()));
    if (src.getSRS() != null)
        obj.add("srs", context.serialize(src.getSRS()));
    if (src.getBackgroundColor() != null)
        obj.add("backgroundColor", context.serialize(src.getBackgroundColor()));
    if (src.getServices() != null)
        obj.add("services", context.serialize(src.getServices()));
    try {// w ww  . j a  va2s.  com
        Method method = Theme.class.getMethod("getParameters", (Class<?>[]) null);
        Type returnType = method.getGenericReturnType();
        obj.add("parameters", context.serialize(src.getParameters(), returnType));
    } catch (SecurityException e) {
        logger.error(e.getMessage(), e);
    } catch (NoSuchMethodException e) {
        logger.error(e.getMessage(), e);
    }
    return obj;
}

From source file:com.ccreanga.bitbucket.rest.client.http.dto.RepositorySshKeyRequest.java

License:Apache License

public JsonObject toJson() {
    JsonObject key = new JsonObject();
    key.addProperty("id", 0);
    key.addProperty("label", label);
    key.addProperty("text", publicKey);

    JsonObject keyPayload = new JsonObject();
    keyPayload.addProperty("project", projectKey);
    keyPayload.addProperty("repository", repositorySlug);
    keyPayload.addProperty("permission", permission);
    keyPayload.add("key", key);
    return keyPayload;
}

From source file:com.certus.controllers.ChartExpenseAdapter.java

@Override
public JsonElement serialize(ChartExpenses exp, Type type, JsonSerializationContext jsc) {
    JsonObject jo = new JsonObject();
    jo.add("day", new JsonPrimitive(exp.getDay()));
    jo.add("sale", new JsonPrimitive(exp.getAmountSale()));
    jo.add("expense", new JsonPrimitive(exp.getAmountExpense()));
    return jo;/*  ww w .j a v  a2s  .com*/
}

From source file:com.certus.controllers.ChartsCriteriaAdapter.java

@Override
public JsonElement serialize(ChartsCriteria sale, Type type, JsonSerializationContext jsc) {
    JsonObject obj = new JsonObject();
    obj.add("day", new JsonPrimitive(sale.getDay()));
    obj.add("amount", new JsonPrimitive(sale.getAmount()));
    return obj;/*  w w  w.j  a v a  2 s. c  om*/
}

From source file:com.certus.controllers.ProductFilterTypeAdapter.java

@Override
public JsonElement serialize(ProductHasSize phs, Type type, JsonSerializationContext jsc) {
    i++;/*from  w w w  .  j ava2  s .  c om*/
    String productsPath1 = null;
    try {
        Context env1 = (Context) new InitialContext().lookup("java:comp/env");
        productsPath1 = (String) env1.lookup("uploadpathproducts");
    } catch (NamingException ex) {
        Logger.getLogger(ProductFilterTypeAdapter.class.getName()).log(Level.SEVERE, null, ex);
    }
    JsonObject jo = new JsonObject();
    jo.add("text", new JsonPrimitive(phs.getProduct().getName()));
    jo.add("value", new JsonPrimitive(i));
    jo.add("selected", i == 1 ? new JsonPrimitive(Boolean.TRUE) : new JsonPrimitive(Boolean.FALSE));
    jo.add("description", new JsonPrimitive(phs.getProduct().getBrand().getBrandName()));
    jo.add("imageSrc", new JsonPrimitive(productsPath1 + phs.getProduct().getImageMain()));
    return jo;
}

From source file:com.certus.controllers.UserTypeAdapter.java

@Override
public JsonElement serialize(User u, Type type, JsonSerializationContext jsc) {
    JsonObject object = new JsonObject();
    try {//from www . j a  v  a  2  s  . c o  m
        object.add("name", new JsonPrimitive(u.getFName() + " " + u.getLName()));
        object.add("email", new JsonPrimitive(u.getEmail()));
        object.add("tel", new JsonPrimitive(u.getTelephone()));
        object.add("address", new JsonPrimitive(u.getAddress()));

    } catch (Exception e) {
        e.printStackTrace();
    }
    return object;
}

From source file:com.certus.mobile.actions.getAllProductsAction.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session s = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    if (request.getParameter("cartList") != null) {
        String jsonList = request.getParameter("cartList");
        Type type = new TypeToken<List<CartItem>>() {
        }.getType();/*from  ww  w  .  ja v  a2  s  .  com*/
        List<CartItem> conList = new Gson().fromJson(jsonList, type);
        List<ProductHasSize> myList = getCompleteProductCart(conList, s);

        String element = new Gson().toJson(myList, new TypeToken<List<ProductHasSize>>() {
        }.getType());
        response.getWriter().write(element);

    } else {
        JsonArray array = new JsonArray();
        JsonObject jo = new JsonObject();
        jo.add("error", new JsonPrimitive("Error response"));
        array.add(jo);
        String element = new Gson().toJson(array);
        response.getWriter().write(element);
    }

}

From source file:com.certus.mobile.actions.mobileFindSubCategoriesAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session s = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    String catName = request.getParameter("catName");
    if (catName != null) {
        Category category = (Category) s.createCriteria(Category.class, "cat")
                .add(Restrictions.eq("cat.catName", catName)).uniqueResult();

        JsonObject jo = new JsonObject();
        JsonArray subCatAry = new JsonArray();
        for (SubCategory sub : category.getSubCategories()) {
            JsonObject subJo = new JsonObject();
            subJo.add("sub_id", new JsonPrimitive(sub.getId()));
            subJo.add("sub_name", new JsonPrimitive(sub.getSubCategoryName()));
            subCatAry.add(subJo);//from  w w  w  . ja  va  2 s  . com
        }
        jo.add("cat_id", new JsonPrimitive(category.getId()));
        jo.add("cat_name", new JsonPrimitive(category.getCatName()));
        jo.add("sub_categories", subCatAry);
        String element = new Gson().toJson(jo);
        response.getWriter().write(element);
    }
}

From source file:com.certus.mobile.actions.mobileRecentItemsAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session sess = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    FilterRecentItems im = new FilterRecentItems();
    String path = "";
    try {/*from ww  w .ja va2 s . c o m*/
        Context env1 = (Context) new InitialContext().lookup("java:comp/env");
        path += (String) env1.lookup("uploadpathproducts");
    } catch (Exception e) {
        e.printStackTrace();
    }
    JsonArray ary = new JsonArray();
    for (Integer i : im.getRecentItems()) {
        Product pr = (Product) sess.load(Product.class, i);
        if (pr.isAvailability()) {
            JsonObject jo = new JsonObject();
            jo.add("pid", new JsonPrimitive(pr.getId()));
            jo.add("p_name", new JsonPrimitive(pr.getName()));
            jo.add("cat_id", new JsonPrimitive(pr.getSubCategory().getCategory().getId()));
            jo.add("subcat_id", new JsonPrimitive(pr.getSubCategory().getId()));
            jo.add("disc_Per", new JsonPrimitive(pr.getDiscountPrice()));
            jo.add("brand", new JsonPrimitive(pr.getBrand().getBrandName()));
            jo.add("image", new JsonPrimitive(path + pr.getImageMain()));
            ary.add(jo);
            //  Product pl = new Gson().fromJson(jo, Product.class);
        }
    }
    String element = new Gson().toJson(ary);
    sess.close();
    response.getWriter().write(element);

}

From source file:com.certus.mobile.actions.mobileSliderImagesAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String sliderImgPath = "";
    try {//from   www.  ja v a2  s  .  c  o  m
        Context envSlide = (Context) new InitialContext().lookup("java:comp/env");
        sliderImgPath = (String) envSlide.lookup("sliderImgs");
    } catch (NamingException ex) {
        Logger.getLogger(mobileSliderImagesAction.class.getName()).log(Level.SEVERE, null, ex);
    }

    Session sez = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    List<SliderFacts> sf = sez.createCriteria(SliderFacts.class).list();
    JsonArray array = new JsonArray();
    for (SliderFacts s : sf) {
        JsonObject jo = new JsonObject();
        jo.add("img", new JsonPrimitive(sliderImgPath + s.getImage()));
        array.add(jo);
    }
    String element = new Gson().toJson(array);
    response.getWriter().write(element);
}