List of usage examples for com.google.gson JsonPrimitive JsonPrimitive
public JsonPrimitive(Character c)
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Sets the string at the specified jpath. * * @param source the source/*from w w w .j a v a 2 s . c om*/ * @param jpath the fully qualified json path to the field required. eg set({'a': {'b': {'c': [1, * 2, 3, 4]}}}, "a.b.c.1", true]) is {'a': {'b': {'c': [1, true, 3, 4]}}}. Strings that can be * casted to numerals are presumed to be array indexes * @param value the value * @return the json element */ public JsonElement set(JsonElement source, JsonArray jpath, Boolean value) { return set(source, jpath, new JsonPrimitive(value)); }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Sets the string at the specified jpath. * * @param source the source//from w w w . j a v a 2 s. c om * @param jpath the fully qualified json path to the field required. eg set({'a': {'b': {'c': [1, * 2, 3, 4]}}}, "a.b.c.1", 5]) is {'a': {'b': {'c': [1, 5, 3, 4]}}}. Strings that can be * casted to numerals are presumed to be array indexes * @param value the value * @return the json element */ public JsonElement set(JsonElement source, JsonArray jpath, Number value) { return set(source, jpath, new JsonPrimitive(value)); }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the typed path./*from www .java 2 s .com*/ * * @param p the p * @param strToNum the str to num * @return the typed path */ private JsonElement getTypedPath(Object p, Boolean strToNum) { JsonElement result = JsonNull.INSTANCE; if (p instanceof Number) { result = new JsonPrimitive((Number) p); } else if (p instanceof String) { String sp = StringUtils.trimToEmpty((String) p); result = strToNum ? (NumberUtils.isCreatable(sp) ? new JsonPrimitive(NumberUtils.toInt(sp)) : new JsonPrimitive(sp)) : new JsonPrimitive(sp); } else if (p instanceof Boolean) { result = new JsonPrimitive((Boolean) p); } else if (p instanceof Character) { result = new JsonPrimitive((Character) p); } else if (p instanceof JsonElement) { result = (JsonElement) p; } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Query./*from w w w. j av a 2s .com*/ * * @param query the query * @return the json element */ public JsonElement query(JsonObject query) { JsonElement result = null; try { Mono<JsonArray> reduce = queryAsync(query).reduce(new JsonArray(), (resultarray, json) -> { resultarray.add(json); return resultarray; }); result = reduce.block(); } catch (RuntimeException e) { result = new JsonPrimitive(e.getMessage()); } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Query async.//from www. jav a 2s .c o m * * @param array the array * @param query the query * @return the observable */ private Flux<JsonElement> queryAsync(JsonArray array, JsonObject query) { return Flux.fromIterable(array).publishOn(Schedulers.elastic()) .filter(json -> filter(json, query.get("where"))).compose(obs -> { Long limit = jsonLever.asLong(query.get("limit"), 0L); return limit > 0 ? obs.take(limit.intValue()) : obs; }).publishOn(Schedulers.elastic()) .compose(obs -> StringUtils.equals(jsonLever.getString(query, "select"), "count") ? obs.count().map(count -> new JsonPrimitive(count)) : obs.map(json -> select(json, query.get("select")))); }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Query async.//w w w. j a v a 2 s . c o m * * @param obj the obj * @param query the query * @return the observable */ private Flux<JsonElement> queryAsync(JsonElement obj, JsonObject query) { return Flux.just(obj).publishOn(Schedulers.elastic()).filter(json -> filter(json, query.get("where"))) .compose(obs -> { Long limit = jsonLever.asLong(query.get("limit"), 0L); return limit > 0 ? obs.take(limit.intValue()) : obs; }).publishOn(Schedulers.elastic()) .compose(obs -> StringUtils.equals(jsonLever.getString(query, "select"), "count") ? obs.count().map(count -> new JsonPrimitive(count)) : obs.map(json -> select(json, query.get("select")))); }
From source file:com.bccriskadvisory.link.rest.gson.ZonedDateTimeAdapter.java
License:Apache License
@Override public JsonElement serialize(ZonedDateTime src, Type type, JsonSerializationContext context) { return new JsonPrimitive(JSON_PATTERN.format(src)); }
From source file:com.bekwam.resignator.model.ConfigurationJSONAdapter.java
License:Apache License
private JsonArray serializeRecentProfiles(List<String> recentProfiles) { JsonArray rps = new JsonArray(); for (String rp : recentProfiles) { rps.add(new JsonPrimitive(rp)); }//from www. j a v a2 s .co m return rps; }
From source file:com.bios.controller.services.AuthenticationService.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request//from ww w. ja va 2s . co m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs This is for the web services * for authentication Service */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); JsonObject object = new JsonObject(); HttpSession session = request.getSession(); if (username == null || username.length() == 0) { JsonArray array = new JsonArray(); JsonPrimitive value = new JsonPrimitive("blank username"); array.add(value); object.addProperty("status", "error"); object.add("message", array); System.out.println(object); response.setContentType("application/json"); response.getWriter().write(object.toString()); return; } if (password == null || password.length() == 0) { JsonArray array = new JsonArray(); JsonPrimitive value = new JsonPrimitive("blank password"); array.add(value); object.addProperty("status", "error"); object.add("message", array); System.out.println(object); response.setContentType("application/json"); response.getWriter().write(object.toString()); return; } if (username.equals("admin") && password.equals("admin")) { // Grant access and return token String token = JWTUtility.sign("abcdefgh12345678", username); object.addProperty("status", "success"); object.addProperty("token", token); session.setAttribute("token", token); } else { JsonArray array = new JsonArray(); JsonPrimitive value = new JsonPrimitive("invalid username/password"); array.add(value); object.addProperty("status", "error"); object.add("message", array); } System.out.println(object); response.setContentType("application/json"); response.getWriter().write(object.toString()); }
From source file:com.bios.controller.services.BidStatusService.java
/** * Handles the HTTP <code>GET</code> method to determine whether currently * placed bids will be successful at the end of the round. A json object * that contains the live bidding data relating to the bid will be created. * * @param request servlet request/*from ww w .ja v a2s. 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 doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ConnectionManager manager = new ConnectionManager(); String requestObject = request.getParameter("r"); String token = request.getParameter("token"); ArrayList<String> errorList = new ArrayList<String>(); JsonObject responseObject = new JsonObject(); JsonArray allErrors = new JsonArray(); if (requestObject == null) { errorList.add("missing r"); } else if (requestObject.length() == 0) { errorList.add("blank r"); } if (token == null) { errorList.add("missing token"); } else if (token.length() == 0) { errorList.add("blank token"); } else { try { String result = JWTUtility.verify(token, "abcdefgh12345678"); if (!result.equals("admin")) { errorList.add("invalid token"); } } catch (JWTException e) { // do we need this? // This means not an admin, or token expired errorList.add("invalid username/token"); } } JsonElement jelementRequest = new JsonParser().parse(requestObject); JsonObject json = jelementRequest.getAsJsonObject(); JsonElement crse = json.get("course"); JsonElement sec = json.get("section"); if (crse == null) { errorList.add("missing course"); } else if (crse.getAsString().length() == 0) { errorList.add("blank course"); } if (sec == null) { errorList.add("missing section"); } else if (sec.getAsString().length() == 0) { errorList.add("blank section"); } if (errorList.size() > 0) { errorList.sort(new ErrorMsgComparator()); for (String s : errorList) { allErrors.add(new JsonPrimitive(s)); } responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } String courseID = crse.getAsString(); String sectionID = sec.getAsString(); int round = manager.getBiddingRound(); String status = manager.getBiddingRoundStatus(); // Section that user has selected Section section = SectionDAO.getInstance().findSection(courseID, sectionID); Course course = CourseDAO.getInstance().findCourse(courseID); if (course == null) { allErrors.add(new JsonPrimitive("invalid course")); } else if (course != null && section == null) { allErrors.add(new JsonPrimitive("invalid section")); } if (allErrors.size() > 0) { responseObject.addProperty("status", "error"); responseObject.add("message", allErrors); response.setContentType("application/json"); response.getWriter().write(responseObject.toString()); return; } // How many students have already been enrolled in the section successfully ArrayList<SectionStudent> sectionStudentList = SectionStudentDAO.getInstance() .getSectionStudentListWithID(courseID, sectionID); // Size of the class minus the already enrolled number, which essentially means vacancy int vacancy = section.getSize() - sectionStudentList.size(); // Minimum bid value that has to be shown to user, default is 10 double minBidValue = 10; // This lists the bids that are still pending approval in round 2, and contains the bids // that user has selected, and these bids are sorted from highest to lowest ArrayList<Bid> allBids = BidDAO.getInstance().getPendingBidsWithID(courseID, sectionID); // ArrayList<Bid> allBids= null; // if (round == 1 && status.equals("started")){ // allBids = BidDAO.getInstance().getBidList(); // } else if (status.equals("stopped") && round == 1){ // allBids = RoundOneBidDAO.getInstance().getBidList(); // } else if(round == 2 && status.equals("started")){ // allBids = BidDAO.getInstance().getPendingBids(); // } else{ // allBids = RoundTwoBidDAO.getInstance().getBidList(); // } allBids.sort(new BidComparator()); // This JsonObject is to be used as the reponse to the user JsonObject object = new JsonObject(); object.addProperty("status", "success"); DecimalFormat df = new DecimalFormat("0.00"); JsonArray arrayOfBids = new JsonArray(); if (round == 1 && status.equals("started")) { object.addProperty("vacancy", section.getSize()); if (section.getSize() > allBids.size() && allBids.size() > 0) { minBidValue = allBids.get(allBids.size() - 1).getAmount(); } else if (allBids.size() >= section.getSize()) { minBidValue = allBids.get(section.getSize() - 1).getAmount(); } object.addProperty("min-bid-amount", minBidValue); for (Bid bid : allBids) { JsonObject innerObject = new JsonObject(); Student stud = StudentDAO.getInstance().retrieve(bid.getUserID()); innerObject.add("userid", new JsonPrimitive(bid.getUserID())); innerObject.add("amount", new JsonPrimitive(bid.getAmount())); String result = df.format(stud.geteDollar()); double eDollar = Double.parseDouble(result); innerObject.add("balance", new JsonPrimitive(eDollar)); innerObject.add("status", new JsonPrimitive("pending")); arrayOfBids.add(innerObject); } } else if (round == 1 && status.equals("stopped")) { object.addProperty("vacancy", section.getSize() - BidDAO.getInstance().getSuccessfulBidsWithID(courseID, sectionID).size()); allBids = BidDAO.getInstance().getBids(courseID, sectionID); if (allBids.size() >= vacancy && vacancy != 0) { // should this be >=? wha if vacancy==0? minBidValue = allBids.get(vacancy - 1).getAmount(); } else if (allBids.size() < vacancy && allBids.size() > 0) { minBidValue = allBids.get(allBids.size() - 1).getAmount(); } object.addProperty("min-bid-amount", minBidValue); allBids = BidDAO.getInstance().getBids(courseID, sectionID); for (Bid bid : allBids) { JsonObject innerObject = new JsonObject(); Student stud = StudentDAO.getInstance().retrieve(bid.getUserID()); innerObject.add("userid", new JsonPrimitive(bid.getUserID())); innerObject.add("amount", new JsonPrimitive(bid.getAmount())); String result = df.format(stud.geteDollar()); double eDollar = Double.parseDouble(result); innerObject.add("balance", new JsonPrimitive(eDollar)); innerObject.add("status", new JsonPrimitive(bid.getStatus())); arrayOfBids.add(innerObject); } } else if (round == 2 && status.equals("started")) { object.addProperty("vacancy", vacancy); // This is to re-compute the minimum bid value // if (allBids.size() >= vacancy && vacancy != 0) { // should this be >=? wha if vacancy==0? // minBidValue = allBids.get(vacancy - 1).getAmount() + 1; // } // This allows us to store the minimum bids, and also to persist the bids such that it stores the min bid amount if (MinBidDAO.getInstance().updateMinBid(courseID + "-" + sectionID, minBidValue)) { // Bid was updated successfully object.addProperty("min-bid-amount", minBidValue); } else { object.addProperty("min-bid-amount", MinBidDAO.getInstance().getValue(courseID + "-" + sectionID)); } for (int i = 0; i < allBids.size(); i++) { Bid bid = allBids.get(i); JsonObject innerObject = new JsonObject(); Student stud = StudentDAO.getInstance().retrieve(bid.getUserID()); innerObject.add("userid", new JsonPrimitive(bid.getUserID())); innerObject.add("amount", new JsonPrimitive(bid.getAmount())); String result = df.format(stud.geteDollar()); double eDollar = Double.parseDouble(result); innerObject.add("balance", new JsonPrimitive(eDollar)); // If there are vacancies still if (vacancy >= allBids.size()) { innerObject.add("status", new JsonPrimitive("success")); } else if (allBids.size() > vacancy) { // If this bid is still within the vacancy requirements if (i <= vacancy) { Bid firstFailedBid = allBids.get(vacancy); if (bid.getAmount() == firstFailedBid.getAmount()) { innerObject.add("status", new JsonPrimitive("fail")); } else { innerObject.add("status", new JsonPrimitive("success")); } } else if (i > vacancy) { //what if vacancy+1's bid amout innerObject.add("status", new JsonPrimitive("fail")); } } arrayOfBids.add(innerObject); } } else { object.addProperty("vacancy", section.getSize() - BidDAO.getInstance().getSuccessfulBidsWithID(courseID, sectionID).size()); allBids = BidDAO.getInstance().getSuccessfulBidsWithID(courseID, sectionID); allBids.sort(new BidComparator()); minBidValue = allBids.get(allBids.size() - 1).getAmount(); object.addProperty("min-bid-amount", minBidValue); for (Bid bid : allBids) { JsonObject innerObject = new JsonObject(); Student stud = StudentDAO.getInstance().retrieve(bid.getUserID()); innerObject.add("userid", new JsonPrimitive(bid.getUserID())); innerObject.add("amount", new JsonPrimitive(bid.getAmount())); String result = df.format(stud.geteDollar()); double eDollar = Double.parseDouble(result); innerObject.add("balance", new JsonPrimitive(eDollar)); innerObject.add("status", new JsonPrimitive(bid.getStatus())); arrayOfBids.add(innerObject); } } object.add("students", arrayOfBids); response.setContentType("application/json"); response.getWriter().write(object.toString()); }