List of usage examples for com.fasterxml.jackson.databind JsonNode size
public int size()
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static ArrayNode getItemVariantOptions(JsonNode item) { JsonNode structuredContent = item.path("structuredContent"); JsonNode variants = structuredContent.path("variants"); if (variants.size() <= 1) { return EMPTY_ARRAY; }/*from w ww . j a va 2 s . c o m*/ ArrayNode userDefinedOptions = JsonUtils.createArrayNode(); JsonNode ordering = structuredContent.path("variantOptionOrdering"); for (int i = 0; i < ordering.size(); i++) { String optionName = ordering.path(i).asText(); ObjectNode option = JsonUtils.createObjectNode(); option.put("name", optionName); option.put("values", JsonUtils.createArrayNode()); userDefinedOptions.add(option); } for (int i = 0; i < variants.size(); i++) { JsonNode variant = variants.path(i); JsonNode attributes = variant.get("attributes"); if (attributes == null) { continue; } Iterator<String> fields = attributes.fieldNames(); while (fields.hasNext()) { String field = fields.next(); String variantOptionValue = attributes.get(field).asText(); ObjectNode userDefinedOption = null; for (int j = 0; j < userDefinedOptions.size(); j++) { ObjectNode current = (ObjectNode) userDefinedOptions.get(j); if (current.get("name").asText().equals(field)) { userDefinedOption = current; } } if (userDefinedOption != null) { boolean hasOptionValue = false; ArrayNode optionValues = (ArrayNode) userDefinedOption.get("values"); for (int k = 0; k < optionValues.size(); k++) { String optionValue = optionValues.get(k).asText(); if (optionValue.equals(variantOptionValue)) { hasOptionValue = true; break; } } if (!hasOptionValue) { optionValues.add(variantOptionValue); } } } } return userDefinedOptions; }
From source file:com.spoiledmilk.cykelsuperstier.break_rote.LocalTrainData.java
private static void getTimesForWeekdays(int hour, int minute, ArrayList<ITransportationInfo> times, JsonNode timeTableNode, int index1, int index2, int numOfStations) { ArrayList<TimeData> allTimes = new ArrayList<TimeData>(); for (int i = 0; i < timeTableNode.size(); i++) { JsonNode intervalNode = timeTableNode.get(i); try {/* w ww .j a va 2 s . c o m*/ double currentTime = ((double) hour) + ((double) minute) / 100d; double startInterval = intervalNode.get("start-time").asDouble(); double endInterval = intervalNode.get("end-time").asDouble(); if (endInterval < 1.0) endInterval += 24.0; if (currentTime >= startInterval && currentTime <= endInterval) { JsonNode dataNode = intervalNode.get("data"); int[] minutesArray = new int[dataNode.size()]; for (int j = 0; j < minutesArray.length; j++) { minutesArray[j] = dataNode.get(j).asInt(); } int currentHour = hour; int currentIndex = 0; int count = 0; while (currentHour <= endInterval && count < 3 && currentIndex < minutesArray.length) { double time1 = minutesArray[currentIndex + index1]; double time2 = minutesArray[currentIndex + index2]; if (time1 < 0 || time2 < 0) continue; time1 /= 100d; time1 += currentHour; time2 /= 100d; time2 += currentHour; if (time2 < time1) time2 += 1.0; allTimes.add(new TimeData(time1, time2)); currentHour++; count++; currentIndex += numOfStations; } } } catch (Exception e) { LOG.e(e.getLocalizedMessage()); } } // sort the list for (int i = 0; i < allTimes.size() - 1; i++) { for (int j = i + 1; j < allTimes.size(); j++) { TimeData td1 = allTimes.get(i); TimeData td2 = allTimes.get(j); if (td1.stationAtime > td2.stationAtime) { allTimes.set(i, td2); allTimes.set(j, td1); } } } LOG.d("sorted times = " + allTimes.toString()); // return the 3 results for (int i = 0; i < 3 && i < allTimes.size(); i++) { TimeData temp = allTimes.get(i); int time = 0; int minutes1 = (int) ((temp.stationAtime - (double) ((int) temp.stationAtime)) * 100); int minutes2 = (int) ((temp.stationBtime - (double) ((int) temp.stationBtime)) * 100); time = (((int) temp.stationBtime) - ((int) temp.stationAtime)) * 60; if (minutes2 >= minutes1) time = minutes2 - minutes1; else { time += 60 - minutes1 + minutes2; time -= 60; } String formattedTime1 = (((int) temp.stationAtime) < 10 ? "0" + ((int) temp.stationAtime) : "" + ((int) temp.stationAtime)) + ":" + (minutes1 < 10 ? "0" + minutes1 : "" + minutes1); String formattedTime2 = (((int) temp.stationBtime) < 10 ? "0" + ((int) temp.stationBtime) : "" + ((int) temp.stationBtime)) + ":" + (minutes2 < 10 ? "0" + minutes2 : "" + minutes2); times.add(new LocalTrainData(formattedTime1, formattedTime2, time)); } }
From source file:models.NasaRegistration.java
public static List<NasaRegistration> all() { List<NasaRegistration> allUsers = new ArrayList<NasaRegistration>(); // Call the API to get the json string JsonNode usersNode = APICall.callAPI(GET_ALL_USER_DATA); // if no value is returned or error or is not json array if (usersNode == null || usersNode.has("error") || !usersNode.isArray()) { return allUsers; }// w ww . ja va 2 s . co m // parse the json string into object for (int i = 0; i < usersNode.size(); i++) { JsonNode json = usersNode.path(i); NasaRegistration getUser = new NasaRegistration(); getUser.setUserNameField(json.findPath("userName").asText()); getUser.setPasswordField(json.findPath("password").asText()); getUser.setfNameField(json.findPath("firstName").asText()); getUser.setEmail("static email"); getUser.setGoal("static goal"); System.out.println("UserName:" + getUser.getfNameField()); allUsers.add(getUser); } return allUsers; }
From source file:com.spoiledmilk.cykelsuperstier.break_rote.LocalTrainData.java
private static void getTimesForWeekend(int hour, int minute, ArrayList<ITransportationInfo> times, JsonNode timeTableNode, int index1, int index2, int numOfStations, JsonNode dataNodeArray) { ArrayList<TimeData> allTimes = new ArrayList<TimeData>(); for (int i = 0; i < timeTableNode.size(); i++) { JsonNode intervalNode = timeTableNode.get(i); try {//from ww w . j a va 2 s. c o m double currentTime = ((double) hour) + ((double) minute) / 100d; double startInterval = intervalNode.get("start-time").asDouble(); double endInterval = intervalNode.get("end-time").asDouble(); if (endInterval < 1.0) endInterval += 24.0; if (currentTime >= startInterval && currentTime <= endInterval) { JsonNode dataNode = dataNodeArray.get(i); int[] minutesArray = new int[dataNode.size()]; for (int j = 0; j < minutesArray.length; j++) { minutesArray[j] = dataNode.get(j).asInt(); } int currentHour = hour; int currentIndex = 0; int count = 0; while (currentHour <= endInterval && count < 3 && currentIndex < minutesArray.length) { double time1 = minutesArray[currentIndex + index1]; double time2 = minutesArray[currentIndex + index2]; if (time1 < 0 || time2 < 0) continue; time1 /= 100d; time1 += currentHour; time2 /= 100d; time2 += currentHour; if (time2 < time1) time2 += 1.0; allTimes.add(new TimeData(time1, time2)); currentHour++; count++; currentIndex += numOfStations; } } } catch (Exception e) { LOG.e(e.getLocalizedMessage()); } } // sort the list for (int i = 0; i < allTimes.size() - 1; i++) { for (int j = i + 1; j < allTimes.size(); j++) { TimeData td1 = allTimes.get(i); TimeData td2 = allTimes.get(j); if (td1.stationAtime > td2.stationAtime) { allTimes.set(i, td2); allTimes.set(j, td1); } } } LOG.d("sorted times = " + allTimes.toString()); // return the 3 results for (int i = 0; i < 3 && i < allTimes.size(); i++) { TimeData temp = allTimes.get(i); int time = 0; int minutes1 = (int) ((temp.stationAtime - (double) ((int) temp.stationAtime)) * 100); int minutes2 = (int) ((temp.stationBtime - (double) ((int) temp.stationBtime)) * 100); time = (((int) temp.stationBtime) - ((int) temp.stationAtime)) * 60; if (minutes2 >= minutes1) time = minutes2 - minutes1; else { time += 60 - minutes1 + minutes2; time -= 60; } String formattedTime1 = (((int) temp.stationAtime) < 10 ? "0" + ((int) temp.stationAtime) : "" + ((int) temp.stationAtime)) + ":" + (minutes1 < 10 ? "0" + minutes1 : "" + minutes1); String formattedTime2 = (((int) temp.stationBtime) < 10 ? "0" + ((int) temp.stationBtime) : "" + ((int) temp.stationBtime)) + ":" + (minutes2 < 10 ? "0" + minutes2 : "" + minutes2); times.add(new LocalTrainData(formattedTime1, formattedTime2, time)); } }
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static boolean hasVariedPrices(JsonNode item) { ProductType type = getProductType(item); JsonNode structuredContent = item.path("structuredContent"); switch (type) { case PHYSICAL: case SERVICE: case GIFT_CARD: JsonNode variants = structuredContent.path("variants"); JsonNode first = variants.get(0); for (int i = 1; i < variants.size(); i++) { JsonNode var = variants.get(i); boolean flag1 = !var.path("onSale").equals(first.path("onSale")); boolean flag2 = isTruthy(first.path("onSale")) && !var.path("salePrice").equals(first.path("salePrice")); boolean flag3 = !var.path("price").equals(first.path("price")); if (flag1 || flag2 || flag3) { return true; }/*from ww w .j av a 2s .c om*/ } return false; case DIGITAL: default: return false; } }
From source file:models.metadata.NasaRMdata.java
public static List<NasaRMdata> all() { List<NasaRMdata> allUsers = new ArrayList<NasaRMdata>(); // Call the API to get the json string JsonNode usersNode = APICall.callAPI(GET_ALL_USER_DATA); // if no value is returned or error or is not json array if (usersNode == null || usersNode.has("error") || !usersNode.isArray()) { return allUsers; }/* w w w . ja v a2 s . com*/ // parse the json string into object for (int i = 0; i < usersNode.size(); i++) { JsonNode json = usersNode.path(i); NasaRMdata newUser = new NasaRMdata(); newUser.setUserNameField(json.findPath("userName").asText()); newUser.setPasswordField(json.findPath("password").asText()); newUser.setfNameField(json.findPath("firstName").asText()); allUsers.add(newUser); } return allUsers; }
From source file:com.squarespace.template.GeneralUtils.java
/** * Determines the boolean value of a node based on its type. */// w w w. j av a 2s . c o m public static boolean isTruthy(JsonNode node) { if (node.isTextual()) { return !node.asText().equals(""); } if (node.isNumber() || node.isBoolean()) { return node.asLong() != 0; } if (node.isMissingNode() || node.isNull()) { return false; } return node.size() != 0; }
From source file:com.squarespace.template.plugins.platform.CommerceUtils.java
public static boolean hasVariants(JsonNode item) { JsonNode structuredContent = item.path("structuredContent"); JsonNode variants = structuredContent.path("variants"); ProductType type = getProductType(item); return type.equals(ProductType.DIGITAL) ? false : variants.size() > 1; }
From source file:com.flipkart.zjsonpatch.JsonDiff.java
private static void compareArray(List<Diff> diffs, List<Object> path, JsonNode source, JsonNode target) { List<JsonNode> lcs = getLCS(source, target); int srcIdx = 0; int targetIdx = 0; int lcsIdx = 0; int srcSize = source.size(); int targetSize = target.size(); int lcsSize = lcs.size(); int pos = 0;/*ww w . j a va 2 s . c o m*/ while (lcsIdx < lcsSize) { JsonNode lcsNode = lcs.get(lcsIdx); JsonNode srcNode = source.get(srcIdx); JsonNode targetNode = target.get(targetIdx); if (lcsNode.equals(srcNode) && lcsNode.equals(targetNode)) { // Both are same as lcs node, nothing to do here srcIdx++; targetIdx++; lcsIdx++; pos++; } else { if (lcsNode.equals(srcNode)) { // src node is same as lcs, but not targetNode //addition List<Object> currPath = getPath(path, pos); diffs.add(Diff.generateDiff(Operation.ADD, currPath, targetNode)); pos++; targetIdx++; } else if (lcsNode.equals(targetNode)) { //targetNode node is same as lcs, but not src //removal, List<Object> currPath = getPath(path, pos); diffs.add(Diff.generateDiff(Operation.REMOVE, currPath, srcNode)); srcIdx++; } else { List<Object> currPath = getPath(path, pos); //both are unequal to lcs node generateDiffs(diffs, currPath, srcNode, targetNode); srcIdx++; targetIdx++; pos++; } } } while ((srcIdx < srcSize) && (targetIdx < targetSize)) { JsonNode srcNode = source.get(srcIdx); JsonNode targetNode = target.get(targetIdx); List<Object> currPath = getPath(path, pos); generateDiffs(diffs, currPath, srcNode, targetNode); srcIdx++; targetIdx++; pos++; } pos = addRemaining(diffs, path, target, pos, targetIdx, targetSize); removeRemaining(diffs, path, pos, srcIdx, srcSize, source); }
From source file:models.DataSet.java
public static List<DataSet> queryDataSet(String dataSetName, String agency, String instrument, String physicalVariable, String gridDimension, Date dataSetStartTime, Date dataSetEndTime) { List<DataSet> dataset = new ArrayList<DataSet>(); ObjectMapper mapper = new ObjectMapper(); ObjectNode queryJson = mapper.createObjectNode(); queryJson.put("name", dataSetName); queryJson.put("agencyId", agency); queryJson.put("instrument", instrument); queryJson.put("physicalVariable", physicalVariable); queryJson.put("gridDimension", gridDimension); if (dataSetEndTime != null) { queryJson.put("dataSetEndTime", dataSetEndTime.getTime()); }//w w w.j a va 2 s .c om if (dataSetStartTime != null) { queryJson.put("dataSetStartTime", dataSetStartTime.getTime()); } JsonNode dataSetNode = APICall.postAPI(DATASET_QUERY, queryJson); if (dataSetNode == null || dataSetNode.has("error") || !dataSetNode.isArray()) { return dataset; } for (int i = 0; i < dataSetNode.size(); i++) { JsonNode json = dataSetNode.path(i); DataSet newDataSet = deserializeJsonToDataSet(json); dataset.add(newDataSet); } return dataset; }