List of usage examples for com.google.gson JsonArray add
public void add(JsonElement element)
From source file:com.certus.mobile.controllers.SingleItemTypeAdapter.java
@Override public JsonElement serialize(ProductHasSize phs, Type type, JsonSerializationContext jsc) { String path = ""; try {/*from w ww . j a va 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("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.cinchapi.concourse.importer.LineBasedImporter.java
License:Apache License
/** * Import the data contained in {@code file} into {@link Concourse}. * <p>/* w ww . ja v a2s.c o m*/ * <strong>Note</strong> that if {@code resolveKey} is specified, an attempt * will be made to add the data in from each group into the existing records * that are found using {@code resolveKey} and its corresponding value in * the group. * </p> * * @param file * @param resolveKey * @return a collection of {@link ImportResult} objects that describes the * records created/affected from the import and whether any errors * occurred. */ public final Set<Long> importFile(String file, @Nullable String resolveKey) { // TODO add option to specify batchSize, which is how many objects to // send over the wire in one atomic batch List<String> lines = FileOps.readLines(file); String[] keys = header(); JsonArray array = new JsonArray(); boolean checkedFileFormat = false; for (String line : lines) { if (!checkedFileFormat) { validateFileFormat(line); checkedFileFormat = true; } if (keys == null) { keys = parseKeys(line); log.info("Parsed keys from header: " + line); } else { JsonObject object = parseLine(line, keys); if (resolveKey != null && object.has(resolveKey)) { JsonElement resolveValue = object.get(resolveKey); if (!resolveValue.isJsonArray()) { JsonArray temp = new JsonArray(); temp.add(resolveValue); resolveValue = temp; } for (int i = 0; i < resolveValue.getAsJsonArray().size(); ++i) { String value = resolveValue.getAsJsonArray().get(i).toString(); Object stored = Convert.stringToJava(value); Set<Long> resolved = concourse.find(resolveKey, Operator.EQUALS, stored); for (long record : resolved) { object = parseLine(line, keys); // this is // inefficient, but // there is no good // way to clone the // original object object.addProperty(Constants.JSON_RESERVED_IDENTIFIER_NAME, record); array.add(object); } } } else { array.add(object); } log.info("Importing {}", line); } } Set<Long> records = importString(array.toString()); return records; }
From source file:com.cinchapi.concourse.server.ConcourseServer.java
License:Apache License
/** * Do the work to jsonify (dump to json string) each of the {@code records}, * possibly at {@code timestamp} (if it is greater than 0) using the * {@code store}./* w ww .j a va 2s . c o m*/ * * @param records * @param timestamp * @param identifier - will include the primary key for each record in the * dump, if set to {@code true} * @param store * @return the json string dump */ private static String jsonify0(List<Long> records, long timestamp, boolean identifier, Store store) { JsonArray array = new JsonArray(); for (long record : records) { Map<String, Set<TObject>> data = timestamp == 0 ? store.select(record) : store.select(record, timestamp); JsonElement object = DataServices.gson().toJsonTree(data); if (identifier) { object.getAsJsonObject().addProperty(GlobalState.JSON_RESERVED_IDENTIFIER_NAME, record); } array.add(object); } return array.size() == 1 ? array.get(0).toString() : array.toString(); }
From source file:com.claresco.tinman.json.JsonUtility.java
License:Open Source License
/** * /*w w w . j a va 2 s .c o m*/ * Description: * Create a JsonArray from XapiInteractionComponents * * Params: * */ protected static JsonArray jsonArrayFromXapiInteractionComponent( ArrayList<XapiInteractionComponent> theComponents, JsonSerializationContext theContext) { JsonArray theArray = new JsonArray(); for (XapiInteractionComponent component : theComponents) { theArray.add(theContext.serialize(component)); } return theArray; }
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 w ww .j a v a 2 s. c om*/ 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 w w w.j a v a 2s . c o 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; }
From source file:com.claresco.tinman.json.XapiContextActivitiesJson.java
License:Open Source License
@Override public JsonElement serialize(XapiContextActivities arg0, Type arg1, JsonSerializationContext arg2) { JsonObject result = new JsonObject(); JsonArray theParentArray = new JsonArray(); if (arg0.hasParent()) { for (XapiActivity activity : arg0.getParent()) { theParentArray.add(arg2.serialize(activity)); }//from www .j a v a2 s .c o m result.add("parent", theParentArray); } JsonArray theGroupingArray = new JsonArray(); if (arg0.hasGrouping()) { for (XapiActivity activity : arg0.getGrouping()) { theGroupingArray.add(arg2.serialize(activity)); } result.add("grouping", theGroupingArray); } if (arg0.hasCategory()) { JsonArray theCategoryArray = serialize(arg0.getCategory(), arg2); if (theCategoryArray != null) { result.add("category", theCategoryArray); } } if (arg0.hasOther()) { JsonArray theOtherArray = serialize(arg0.getOther(), arg2); if (theOtherArray != null) { result.add("other", theOtherArray); } } return result; }
From source file:com.claresco.tinman.json.XapiContextActivitiesJson.java
License:Open Source License
private JsonArray serialize(ArrayList<XapiActivity> theActivityArray, JsonSerializationContext theSerializationContext) { JsonArray theArray = new JsonArray(); for (XapiActivity activity : theActivityArray) { theArray.add(theSerializationContext.serialize(activity, XapiActivity.class)); }//from w ww . j av a 2 s . c o m if (theArray.size() == 0) { return null; } return theArray; }
From source file:com.claresco.tinman.json.XapiCredentialsListJson.java
License:Open Source License
@Override public JsonElement serialize(XapiCredentialsList arg0, Type arg1, JsonSerializationContext arg2) { JsonArray theResult = new JsonArray(); for (XapiKeySecret theKS : arg0.keySet()) { JsonObject theObject = new JsonObject(); JsonObject theKeySecretJson = new JsonObject(); theKeySecretJson.addProperty(KEY, theKS.getKey()); theKeySecretJson.addProperty(SECRET, theKS.getSecret()); theObject.add(KEYSECRET, theKeySecretJson); theObject.add(CREDENTIALS, arg2.serialize(arg0.get(theKS), XapiCredentials.class)); theResult.add(theObject); }//from w w w .ja va 2 s.co m return theResult; }
From source file:com.claresco.tinman.json.XapiGroupJson.java
License:Open Source License
@Override public JsonElement serialize(XapiGroup arg0, Type arg1, JsonSerializationContext arg2) { JsonObject result = new JsonObject(); result.addProperty("name", arg0.getName()); result.addProperty("objectType", arg0.getObjectType()); XapiInverseFunctionalIdentifier theIdentifier = arg0.getInverseFuncId(); if (theIdentifier.hasMbox()) { result.addProperty("mbox", theIdentifier.getMbox().toString()); }//from w ww.ja v a 2s. co m result.addProperty("mbox_sha1sum", theIdentifier.getMboxSha1Sum()); if (theIdentifier.hasOpenId()) { result.addProperty("openid", theIdentifier.getOpenId().toString()); } result.add("account", arg2.serialize(theIdentifier.getAccount())); // Iterate through the member to create a jsonarray JsonArray memberArray = new JsonArray(); for (XapiAgent a : arg0.getMember()) { memberArray.add(arg2.serialize(a)); } result.add("member", memberArray); return result; }