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.certus.mobile.controllers.CartModelAdapter.java

@Override
public JsonElement serialize(CartModel model, Type type, JsonSerializationContext jsc) {
    List<CartItemModel> cartItemModels = new ArrayList<>();
    List<Double> tot = new ArrayList<>();

    JsonObject jo = new JsonObject();

    for (CartItem item : cartItems) {

        CartItemModel itemModel = new CartItemModel(item.getProduct_id(),
                cart.getNameofProduct(item.getProduct_id()),
                cart.getPriceofProduct(item.getProduct_id(), item.getSize()), item.getSize(),
                path + cart.getImageofProduct(item.getProduct_id()), item.getQnty());
        cartItemModels.add(itemModel);//from  ww w.ja v  a  2 s.  c  o m
        tot.add(cart.getPriceofProduct(item.getProduct_id(), item.getSize()) * item.getQnty());

    }
    JsonArray array = (JsonArray) new Gson().toJsonTree(cartItemModels, new TypeToken<List<CartItemModel>>() {
    }.getType());
    jo.add("cart_total", new JsonPrimitive(cart.grandTotal(tot)));
    jo.add("total_items", new JsonPrimitive(cart.getTotalItemsOfTheCart()));
    jo.add("items", array);
    //        model.setCart_total(cart.grandTotal(tot));
    //        model.setTotal_items(cart.getTotalItemsOfTheCart());
    //        model.setItems(cartItemModels);
    return jo;
}

From source file:com.certus.mobile.controllers.FilterProductsAdapter.java

@Override
public JsonElement serialize(ProductHasSize phs, Type type, JsonSerializationContext jsc) {

    String path = "";
    try {// w  w w . j  a  v  a2 s  . com
        Context env1 = (Context) new InitialContext().lookup("java:comp/env");
        path += (String) env1.lookup("uploadpathproducts");
    } catch (Exception e) {
        e.printStackTrace();
    }
    JsonObject jo = new JsonObject();
    jo.add("pid", new JsonPrimitive(phs.getProduct().getId()));
    jo.add("name", new JsonPrimitive(phs.getProduct().getName()));
    jo.add("brand", new JsonPrimitive(phs.getProduct().getBrand().getBrandName()));
    jo.add("price", new JsonPrimitive(phs.getPrice()));
    jo.add("dic_per", new JsonPrimitive(phs.getProduct().getDiscountPrice()));
    jo.add("size", new JsonPrimitive(phs.getSize().getSizeName()));
    jo.add("img", new JsonPrimitive(path + phs.getProduct().getImageMain()));
    return jo;
}

From source file:com.certus.mobile.controllers.MobileProductListAdapter.java

@Override
public JsonElement serialize(ProductHasSize phs, Type type, JsonSerializationContext jsc) {
    JsonObject jo = new JsonObject();
    jo.add("pid", jo);
    return jo;/* www .j  av a 2s  .  co m*/
}

From source file:com.certus.mobile.controllers.PopularDealsTypeAdapter.java

@Override
public JsonElement serialize(ProWithRate pwr, Type type, JsonSerializationContext jsc) {
    String path = "";
    try {/* w w w.  j ava 2s. com*/
        Context env1 = (Context) new InitialContext().lookup("java:comp/env");
        path += (String) env1.lookup("uploadpathproducts");
    } catch (Exception e) {
        e.printStackTrace();
    }
    JsonObject jo = new JsonObject();
    if (pwr.getProduct().isAvailability()) {

        jo.add("pid", new JsonPrimitive(pwr.getProduct().getId()));
        jo.add("cat_id", new JsonPrimitive(pwr.getProduct().getSubCategory().getCategory().getId()));
        jo.add("subcat_id", new JsonPrimitive(pwr.getProduct().getSubCategory().getId()));
        jo.add("disc_per", new JsonPrimitive(pwr.getProduct().getDiscountPrice()));
        jo.add("image", new JsonPrimitive(path + pwr.getProduct().getImageMain()));
        jo.add("price", new JsonPrimitive(pwr.getPrice()));
        jo.add("p_name", new JsonPrimitive(pwr.getProduct().getName()));
        jo.add("brand", new JsonPrimitive(pwr.getProduct().getBrand().getBrandName()));

    }
    return jo;
}

From source file:com.certus.mobile.controllers.SingleItemTypeAdapter.java

@Override
public JsonElement serialize(ProductHasSize phs, Type type, JsonSerializationContext jsc) {
    String path = "";
    try {//  ww  w .ja  v  a2  s.c o  m
        Context env1 = (Context) new InitialContext().lookup("java:comp/env");
        path += (String) env1.lookup("uploadpathproducts");
    } catch (Exception e) {
        e.printStackTrace();
    }

    JsonObject jo = new JsonObject();
    jo.add("pid", new JsonPrimitive(phs.getProduct().getId()));
    jo.add("name", new JsonPrimitive(phs.getProduct().getName()));
    jo.add("price", new JsonPrimitive(phs.getPrice()));
    jo.add("disc_per", new JsonPrimitive(phs.getProduct().getDiscountPrice()));
    jo.add("brand", new JsonPrimitive(phs.getProduct().getBrand().getBrandName()));
    jo.add("desc", new JsonPrimitive(phs.getProduct().getDescription()));
    jo.add("img", new JsonPrimitive(path + phs.getProduct().getImageMain()));
    JsonArray sizesAry = new JsonArray();
    JsonArray pricesAry = new JsonArray();
    JsonArray qntyAry = new JsonArray();
    for (ProductHasSize pp : phs.getProduct().getProductHasSizes()) {
        sizesAry.add(new JsonPrimitive(pp.getSize().getSizeName()));
        pricesAry.add(new JsonPrimitive(pp.getPrice()));
        qntyAry.add(new JsonPrimitive(pp.getQnty()));
    }
    jo.add("sizes", sizesAry);
    jo.add("prices", pricesAry);
    jo.add("avl_qnty", qntyAry);
    JsonArray reviewsAry = new JsonArray();
    for (Review r : phs.getProduct().getReviews()) {
        JsonObject reviews = new JsonObject();
        reviews.add("comment", new JsonPrimitive(r.getComment()));
        reviews.add("date", new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd").format(r.getDateComnt())));
        reviews.add("user", new JsonPrimitive(r.getUser().getFName() + " " + r.getUser().getLName()));
        reviewsAry.add(reviews);
    }
    jo.add("reviews", reviewsAry);
    return jo;
}

From source file:com.chiwanpark.flume.plugins.handler.JSONHandler.java

License:Apache License

@Override
public String getString(Event event) throws Exception {
    JsonPrimitive body = new JsonPrimitive(new String(event.getBody(), charset));
    JsonObject obj = new JsonObject();

    obj.add("body", body);
    if (!event.getHeaders().isEmpty()) {
        JsonObject headers = new JsonObject();
        for (Map.Entry<String, String> header : event.getHeaders().entrySet()) {
            headers.add(header.getKey(), new JsonPrimitive(header.getValue()));
        }/*  w ww  . j a va  2 s .c  o  m*/
        obj.add("headers", headers);
    }

    return gson.toJson(obj);
}

From source file:com.cinchapi.concourse.importer.LineBasedImporter.java

License:Apache License

/**
 * Parse the data from {@code line} into a {@link JsonObject} that is
 * appropriate for import. The subclass can customize the behaviour of this
 * process by overriding the {@link #header()} and
 * {@link #transformValue(String, String)} methods.
 * // w w  w.j  ava  2s.com
 * @param line
 * @param keys
 * @return the line data encoded as a JsonObject
 */
private final JsonObject parseLine(String line, String... keys) {
    line = line.trim();
    JsonObject json = new JsonObject();
    String[] toks = null;
    if (useOptimizedSplitPath) {
        QuoteAwareStringSplitter it = new QuoteAwareStringSplitter(line, delimiter().charAt(0));
        List<String> toksList = Lists.newArrayList();
        while (it.hasNext()) {
            toksList.add(it.next());
        }
        toks = TLists.toArrayCasted(toksList, String.class);
    } else {
        toks = Strings.splitStringByDelimiterButRespectQuotes(line, delimiter());
    }
    for (int i = 0; i < Math.min(keys.length, toks.length); ++i) {
        if (StringUtils.isBlank(toks[i])) {
            continue;
        }
        JsonElement value = transformValue(keys[i], toks[i]);
        json.add(keys[i], value);
    }
    return json;
}

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

License:Open Source License

@Override
public JsonElement serialize(XapiActivityDefinition arg0, Type arg1, JsonSerializationContext arg2) {
    if (arg0.isEmpty()) {
        return null;
    }//from  w  ww  .  j  a va 2 s.c  om

    JsonObject result = new JsonObject();

    if (arg0.hasName()) {
        result.add("name", arg2.serialize(arg0.getName(), XapiLanguageMap.class));
    }

    if (arg0.hasDescription()) {
        result.add("description", arg2.serialize(arg0.getDescription(), XapiLanguageMap.class));
    }

    if (arg0.hasType()) {
        result.addProperty("type", arg0.getType().toString());
    }

    if (arg0.hasInteractionProperties()) {
        XapiInteraction theInteraction = arg0.getInteractionProperties();
        if (theInteraction.hasChoices()) {
            result.add("choices",
                    JsonUtility.jsonArrayFromXapiInteractionComponent(theInteraction.getChoices(), arg2));
        }
        if (theInteraction.hasScale()) {
            result.add("scale",
                    JsonUtility.jsonArrayFromXapiInteractionComponent(theInteraction.getScale(), arg2));
        }
        if (theInteraction.hasTarget()) {
            result.add("target",
                    JsonUtility.jsonArrayFromXapiInteractionComponent(theInteraction.getTarget(), arg2));
        }
        if (theInteraction.hasSource()) {
            result.add("source",
                    JsonUtility.jsonArrayFromXapiInteractionComponent(theInteraction.getSource(), arg2));
        }
        if (theInteraction.hasSteps()) {
            result.add("steps",
                    JsonUtility.jsonArrayFromXapiInteractionComponent(theInteraction.getSteps(), arg2));
        }
        if (theInteraction.hasCorrectReponse()) {
            ArrayList<String> theCorrectResponses = theInteraction.getCorrectResponse();
            JsonArray theCorrectResponseArray = new JsonArray();
            for (String correctResponse : theCorrectResponses) {
                theCorrectResponseArray.add(new JsonPrimitive(correctResponse));
            }
            result.add("correctResponsesPattern", theCorrectResponseArray);
        }
        if (theInteraction.hasType()) {
            result.addProperty("interactionType", theInteraction.getType());
        }
    }

    if (arg0.hasExtension()) {
        XapiExtension theExt = arg0.getExtension();
        JsonObject theExtJson = new JsonObject();
        for (String key : theExt.getKeys()) {
            theExtJson.addProperty(key, theExt.getValueOf(key));
        }

        result.add("extensions", theExtJson);
    }

    return result;
}

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

License:Open Source License

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

    result.addProperty("id", arg0.getId().toString());
    result.addProperty("objectType", arg0.getObjectType());

    JsonElement theDefinition = arg2.serialize(arg0.getDefinition(), XapiActivityDefinition.class);
    if (theDefinition != null) {
        result.add("definition", theDefinition);
    }/* www  .  j  av  a 2 s. com*/

    return result;
}

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

License:Open Source License

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

    if (arg0.hasName()) {
        result.addProperty("name", arg0.getName());
    }/*  w w w.  ja v a 2s. c  om*/

    result.addProperty("objectType", arg0.getObjectType());

    XapiInverseFunctionalIdentifier theIdentifier = arg0.getInverseFuncId();

    if (theIdentifier.hasMbox()) {
        result.addProperty("mbox", theIdentifier.getMbox().toString());
    }

    if (theIdentifier.hasMboxSha1Sum()) {
        result.addProperty("mbox_sha1sum", theIdentifier.getMboxSha1Sum());
    }

    if (theIdentifier.hasOpenId()) {
        result.addProperty("openid", theIdentifier.getOpenId().toString());
    }

    if (theIdentifier.hasAccount()) {
        result.add("account", arg2.serialize(theIdentifier.getAccount()));
    }

    return result;
}