Example usage for com.fasterxml.jackson.databind JsonNode size

List of usage examples for com.fasterxml.jackson.databind JsonNode size

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JsonNode size.

Prototype

public int size() 

Source Link

Usage

From source file:com.spoiledmilk.cykelsuperstier.break_rote.LocalTrainData.java

public static ArrayList<ITransportationInfo> getNext3Arrivals(String fromStation, String toStation, String line,
        Context context) {/*from w  ww .  java2s  . co  m*/
    ArrayList<ITransportationInfo> ret = new ArrayList<ITransportationInfo>();
    String lineNumber = line.split(" ")[0];
    String jsonStr = Util.stringFromJsonAssets(context, "stations/local-trains-timetable.json");
    JsonNode root = Util.stringToJsonNode(jsonStr);
    JsonNode lines = root.get("local-trains");
    for (int i = 0; i < lines.size(); i++) {
        if (lines.get(i).get("line").asText().trim().equalsIgnoreCase(lineNumber.trim())) {
            JsonNode stationsArray = lines.get(i).get("stations");
            int direction = -1, index1 = -1, index2 = -1;
            for (int j = 0; j < stationsArray.size(); j++) {
                if (stationsArray.get(j).asText().trim().equalsIgnoreCase(fromStation.trim()) && index1 < 0) {
                    index1 = j;
                    if (direction < 0)
                        direction = DEPARTURE;
                    else
                        break;
                }
                if (stationsArray.get(j).asText().trim().equalsIgnoreCase(toStation.trim()) && index2 < 0) {
                    index2 = j;
                    if (direction < 0)
                        direction = ARRIVAL;

                    else
                        break;
                }
            }
            if (direction < 0 || index1 < 0 || index2 < 0)
                break;
            JsonNode timetableNodeContainer = direction == DEPARTURE ? lines.get(i).get("departure")
                    : lines.get(i).get("arrival");
            Calendar calendar = Calendar.getInstance();
            int day = calendar.get(Calendar.DAY_OF_WEEK);
            if (day == 1)
                day = 6;
            else
                day -= 2;
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int minute = calendar.get(Calendar.MINUTE);
            JsonNode timeTableNode;
            if (day >= 0 && day <= 4) {
                timeTableNode = timetableNodeContainer.get("weekdays");
                if (timeTableNode != null)
                    getTimesForWeekdays(hour, minute, ret, timeTableNode, index1, index2, stationsArray.size());
            } else if (day == 5) { // Saturday
                timeTableNode = timetableNodeContainer.get("weekend").get("saturday");
                if (timeTableNode != null) {
                    JsonNode dataNodeArray = timetableNodeContainer.get("weekend").get("data-table");
                    getTimesForWeekend(hour, minute, ret, timeTableNode, index1, index2, stationsArray.size(),
                            dataNodeArray);
                }
            } else { // Saturday
                timeTableNode = timetableNodeContainer.get("weekend").get("sunday");
                if (timeTableNode != null) {
                    JsonNode dataNodeArray = timetableNodeContainer.get("weekend").get("data-table");
                    getTimesForWeekend(hour, minute, ret, timeTableNode, index1, index2, stationsArray.size(),
                            dataNodeArray);
                }
            }
            if (timeTableNode == null)
                break;

            break;
        }
    }
    return ret;
}

From source file:io.gravitee.policy.oauth2.Oauth2Policy.java

static boolean hasRequiredScopes(JsonNode oauthResponseNode, List<String> requiredScopes,
        String scopeSeparator) {/*from   ww w . j  a v a2 s .  co m*/
    if (requiredScopes == null) {
        return true;
    }

    JsonNode scopesNode = oauthResponseNode.path(OAUTH_PAYLOAD_SCOPE_NODE);

    List<String> scopes;
    if (scopesNode instanceof ArrayNode) {
        Iterator<JsonNode> scopeIterator = scopesNode.elements();
        scopes = new ArrayList<>(scopesNode.size());
        List<String> finalScopes = scopes;
        scopeIterator.forEachRemaining(jsonNode -> finalScopes.add(jsonNode.asText()));
    } else {
        scopes = Arrays.asList(scopesNode.asText().split(scopeSeparator));
    }

    return scopes.containsAll(requiredScopes);
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static JsonNode getNormalPriceMoneyNode(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");
        if (variants.size() == 0) {
            return DEFAULT_MONEY_NODE;
        }//  w w  w .j a  v  a2 s.c om
        JsonNode moneyNode = variants.get(0).path("priceMoney");
        BigDecimal price = getAmountFromMoneyNode(moneyNode);
        for (int i = 1; i < variants.size(); i++) {
            JsonNode currMoneyNode = variants.get(i).path("priceMoney");
            BigDecimal curr = getAmountFromMoneyNode(currMoneyNode);
            if (curr.compareTo(price) > 0) {
                price = curr;
                moneyNode = currMoneyNode;
            }
        }
        return moneyNode;

    case DIGITAL:
        JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
        return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;

    default:
        return DEFAULT_MONEY_NODE;
    }
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static boolean isSoldOut(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");

    switch (type) {
    case PHYSICAL:
    case SERVICE:
        JsonNode variants = structuredContent.path("variants");
        int size = variants.size();
        for (int i = 0; i < size; i++) {
            JsonNode variant = variants.get(i);
            if (isTruthy(variant.path("unlimited")) || variant.path("qtyInStock").asInt() > 0) {
                return false;
            }/*from   w  w w.j ava 2 s  .c  o  m*/
        }
        return true;

    case DIGITAL:
    case GIFT_CARD:
        return false;

    default:
        return true;
    }
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static boolean isOnSale(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");

    switch (type) {
    case PHYSICAL:
    case SERVICE:
        JsonNode variants = structuredContent.path("variants");
        int size = variants.size();
        for (int i = 0; i < size; i++) {
            JsonNode variant = variants.get(i);
            if (isTruthy(variant.path("onSale"))) {
                return true;
            }//from  w w  w .ja  v a 2s . c o  m
        }
        break;

    case DIGITAL:
        return isTruthy(structuredContent.path("onSale"));

    case GIFT_CARD:
    default:
        break;
    }
    return false;
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static JsonNode getSalePriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch (type) {
    case PHYSICAL:
    case SERVICE:
        JsonNode variants = structuredContent.path("variants");
        if (variants.size() == 0) {
            return DEFAULT_MONEY_NODE;
        }//  w ww.  j  a  v  a2  s . co m
        BigDecimal salePrice = null;
        JsonNode salePriceNode = null;
        for (int i = 0; i < variants.size(); i++) {
            JsonNode variant = variants.path(i);
            JsonNode priceMoney = variant.path("salePriceMoney");
            BigDecimal price = getAmountFromMoneyNode(priceMoney);
            if (isTruthy(variant.path("onSale")) && (salePriceNode == null || price.compareTo(salePrice) < 0)) {
                salePrice = price;
                salePriceNode = priceMoney;
            }
        }
        return (salePriceNode == null) ? DEFAULT_MONEY_NODE : salePriceNode;

    case DIGITAL:
        JsonNode digitalMoneyNode = structuredContent.path("salePriceMoney");
        return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;

    case GIFT_CARD:
        // this should never happen

    default:
        return DEFAULT_MONEY_NODE;
    }
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static double getTotalStockRemaining(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");

    if (EnumSet.of(ProductType.DIGITAL, ProductType.GIFT_CARD).contains(type)) {
        return Double.POSITIVE_INFINITY;
    } else {/*from  w w w  .j a v  a2s .c om*/
        int total = 0;
        JsonNode variants = structuredContent.path("variants");
        for (int i = 0; i < variants.size(); i++) {
            JsonNode variant = variants.get(i);
            if (isTruthy(variant.path("unlimited"))) {
                return Double.POSITIVE_INFINITY;
            } else {
                total += variant.path("qtyInStock").asLong();
            }
        }
        return total;
    }
}

From source file:com.baasbox.util.QueryParams.java

public static QueryParams getParamsFromJson(JsonNode node) {
    if (node == null) {
        return QueryParams.getInstance();
    }/*from w  w  w  .j a  va 2 s. c  om*/
    Map<String, String[]> query = new HashMap<String, String[]>();
    Iterator<Map.Entry<String, JsonNode>> nodes = node.fields();
    while (nodes.hasNext()) {
        Map.Entry<String, JsonNode> next = nodes.next();
        String k = next.getKey();
        JsonNode val = next.getValue();
        if (val.isArray()) {
            String[] ary = new String[val.size()];
            int idx = 0;
            for (JsonNode n : val) {
                String s = n == null ? null : n.asText();
                ary[idx++] = s;
            }
            query.put(k, ary);
        } else {
            String[] o = { val.asText() };
            query.put(k, o);
        }
    }
    return getParamsFromQueryString(query);
}

From source file:com.squarespace.template.plugins.platform.CommerceUtils.java

public static JsonNode getFromPriceMoneyNode(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");
        if (variants.size() == 0) {
            return DEFAULT_MONEY_NODE;
        }//from   w w  w. ja va2  s.com

        JsonNode first = variants.get(0);
        JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney")
                : first.path("priceMoney");
        BigDecimal price = getAmountFromMoneyNode(moneyNode);
        for (int i = 1; i < variants.size(); i++) {
            JsonNode var = variants.get(i);
            JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney")
                    : var.path("priceMoney");

            BigDecimal current = getAmountFromMoneyNode(currentMoneyNode);
            if (current.compareTo(price) < 0) {
                price = current;
                moneyNode = currentMoneyNode;
            }
        }
        return moneyNode;

    case DIGITAL:
        JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
        return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;

    default:
        return DEFAULT_MONEY_NODE;
    }
}

From source file:com.spoiledmilk.cykelsuperstier.break_rote.STrainData.java

public static ArrayList<ITransportationInfo> getNext3Arrivals(String stationFrom, String stationTo, String line,
        Context context) {//w  ww  . ja va  2 s.  com
    ArrayList<ITransportationInfo> ret = new ArrayList<ITransportationInfo>();
    try {
        String bufferString = Util.stringFromJsonAssets(context, "stations/" + filename);
        JsonNode actualObj = Util.stringToJsonNode(bufferString);
        JsonNode lines = actualObj.get("timetable");

        // find the data for the current transportation line
        for (int i = 0; i < lines.size(); i++) {
            JsonNode lineJson = lines.get(i);
            String[] sublines = line.split(",");
            for (int ii = 0; ii < sublines.length; ii++) {
                if (lineJson.get("line").asText().equalsIgnoreCase(sublines[ii].trim())) {
                    int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
                    if (day == 1)
                        day = 6;
                    else
                        day -= 2;
                    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                    int minute = Calendar.getInstance().get(Calendar.MINUTE);
                    JsonNode stationData = null;
                    if ((day == 4 || day == 5)
                            && isFridaySaturdayNight(lineJson.get("night-after-friday-saturday"), hour)) {
                        // night after Friday or Saturday
                        stationData = lineJson.get("night-after-friday-saturday");
                    } else if (day < 5) {
                        // weekdays
                        stationData = lineJson.get("weekdays");
                    } else {
                        // weekend
                        stationData = lineJson.get("weekend");
                    }
                    JsonNode stations = stationData.get("data");
                    int[] AStationMinutes = null, BStationMinutes = null;
                    // find the data for the start and end station and choose the direction
                    direction = NOT_SET;
                    for (int j = 0; j < stations.size(); j++) {
                        JsonNode st = stations.get(j);
                        if (st.get("station").asText().equalsIgnoreCase(stationFrom) && direction == NOT_SET
                                && AStationMinutes == null) {
                            direction = DEPARTURE;
                            AStationMinutes = getMinutesArray(st.get("departure").asText());
                        } else if (st.get("station").asText().equalsIgnoreCase(stationFrom)
                                && AStationMinutes == null) {
                            AStationMinutes = getMinutesArray(st.get("arrival").asText());
                            break;
                        } else if (st.get("station").asText().equalsIgnoreCase(stationTo)
                                && direction == NOT_SET && BStationMinutes == null) {
                            direction = ARRIVAL;
                            BStationMinutes = getMinutesArray(st.get("departure").asText());
                        } else if (st.get("station").asText().equalsIgnoreCase(stationTo)
                                && BStationMinutes == null) {
                            BStationMinutes = getMinutesArray(st.get("arrival").asText());
                            break;
                        }
                    }
                    if (AStationMinutes == null || BStationMinutes == null)
                        continue;
                    JsonNode subLines = direction == DEPARTURE ? stationData.get("departure")
                            : stationData.get("arrival");
                    JsonNode subLine = subLines.get(0);
                    if (hasTrain(hour, subLine.get("night").asText(), subLine.get("day").asText())) {
                        int count = 0;
                        for (int k = 0; k < AStationMinutes.length; k++) {
                            if (minute <= AStationMinutes[k]) {
                                int arrivalHour = hour;
                                int time = BStationMinutes[k] - AStationMinutes[k];
                                if (AStationMinutes[k] > BStationMinutes[k]) {
                                    arrivalHour++;
                                    time = 60 - AStationMinutes[k] + BStationMinutes[k];
                                }
                                ret.add(new STrainData(
                                        (hour < 10 ? "0" + hour : "" + hour) + ":"
                                                + (AStationMinutes[k] < 10 ? "0" + AStationMinutes[k]
                                                        : "" + AStationMinutes[k]),
                                        (arrivalHour < 10 ? "0" + arrivalHour : "" + arrivalHour) + ":"
                                                + (BStationMinutes[k] < 10 ? "0" + BStationMinutes[k]
                                                        : "" + BStationMinutes[k]),
                                        time));
                                if (++count == 3)
                                    break;
                            }
                        }
                        // second pass to get the times for the next hour
                        hour++;
                        for (int k = 0; k < AStationMinutes.length && count < 3; k++) {
                            int arrivalHour = hour;
                            int time = BStationMinutes[k] - AStationMinutes[k];
                            if (AStationMinutes[k] > BStationMinutes[k]) {
                                arrivalHour++;
                                time = 60 - AStationMinutes[k] + BStationMinutes[k];
                            }
                            ret.add(new STrainData(
                                    (hour < 10 ? "0" + hour : "" + hour) + ":"
                                            + (AStationMinutes[k] < 10 ? "0" + AStationMinutes[k]
                                                    : "" + AStationMinutes[k]),
                                    (arrivalHour < 10 ? "0" + arrivalHour : "" + arrivalHour) + ":"
                                            + (BStationMinutes[k] < 10 ? "0" + BStationMinutes[k]
                                                    : "" + BStationMinutes[k]),
                                    time));
                        }
                    }
                    return ret;
                }
            }
        }
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null)
            LOG.e(e.getLocalizedMessage());
    }

    return ret;
}