Example usage for com.google.gson JsonPrimitive JsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive(Character c) 

Source Link

Document

Create a primitive containing a character.

Usage

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 {//  w w w  . jav a2 s. co 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  w w w  .ja  va 2  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);
}

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);// w  w w  .  j ava 2  s  . com
        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 {//  www.ja v  a 2 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("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.PopularDealsTypeAdapter.java

@Override
public JsonElement serialize(ProWithRate pwr, Type type, JsonSerializationContext jsc) {
    String path = "";
    try {//from w w  w  .j a v a 2  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();
    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 {/*from  w  w w.j a  va 2s . c om*/
        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()));
        }//ww w  . ja  v  a 2s . co  m
        obj.add("headers", headers);
    }

    return gson.toJson(obj);
}

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

License:Apache License

/**
 * At a minimum, this method is responsible for taking a raw source string
 * value and converting it to a {@link JsonElement}. The default
 * implementation makes an effort to represent numeric and boolean values as
 * appropriate {@link JsonPrimitive json primitives}. All other kinds of
 * values are represented as strings, which is the correct format for the
 * server to handle masquerading types (i.e. resolvable links, links, tags,
 * forced doubles, etc).//from  www . ja  v a  2s  .  c  om
 * <p>
 * The default behaviour is appropriate in most cases, but this method can
 * be used by subclasses to define dynamic intermediary transformations to
 * data to better prepare it for import.
 * </p>
 * <h1>Examples</h1> <h2>Specifying Link Resolution</h2>
 * <p>
 * The server will convert raw data of the form
 * <code>@&lt;key&gt;@value@&lt;key&gt;@</code> into a Link to all the
 * records where key equals value in Concourse. For this purpose, the
 * subclass can convert the raw value to this form using the
 * {@link Convert#stringToResolvableLinkSpecification(String, String)}
 * method.
 * </p>
 * <p>
 * <h2>Normalizing Data</h2>
 * It may be desirable to normalize the raw data before input. For example,
 * the subclass may wish to convert all strings to a specific case, or
 * sanitize inputs, etc.
 * </p>
 * <p>
 * <h2>Compacting Representation</h2>
 * If a column in a file contains a enumerated set of string values, it may
 * be desirable to transform the values to a string representation of a
 * number so that, when converted, the data is more compact and takes up
 * less space.
 * </p>
 * 
 * @param key
 * @param value
 * @return the transformed values in a JsonArray
 */
protected JsonElement transformValue(String key, String value) {
    JsonPrimitive element;
    Object parsed;
    if ((parsed = Strings.tryParseNumberStrict(value)) != null) {
        element = new JsonPrimitive((Number) parsed);
    } else if ((parsed = Strings.tryParseBoolean(value)) != null) {
        element = new JsonPrimitive((Boolean) parsed);
    } else {
        element = new JsonPrimitive(value);
        element.toString();
    }
    return element;
}

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

License:Open Source License

protected static JsonArray convertToJsonArray(ArrayList<String> theList) {
    JsonArray theArray = new JsonArray();

    for (String s : theList) {
        theArray.add(new JsonPrimitive(s));
    }/*from  www.  ja  v a 2s  . c  o  m*/

    return theArray;
}

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  ww w. ja v a  2  s .  co  m

    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;
}