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:org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoaderTest.java

@Test
public void testBuildNodes() throws Exception {
    Class<PhyConfigLoader> class1 = PhyConfigLoader.class;
    Method method = class1.getDeclaredMethod("buildNodes", new Class[] { JsonNode.class });
    method.setAccessible(true);//w  w w  . j a v a  2  s .  c  o m

    JsonNode nodesRoot = mock(JsonNode.class);
    JsonNode nodes = mock(JsonNode.class);
    JsonNode node = mock(JsonNode.class);
    JsonNode node_temp = mock(JsonNode.class);
    JsonNode port_temp = mock(JsonNode.class);
    JsonNode attribute_temp = mock(JsonNode.class);
    JsonNode attribute_temp_son = mock(JsonNode.class);
    JsonNode ports = mock(JsonNode.class);
    JsonNode attributes = mock(JsonNode.class);
    JsonNode portAttributes = mock(JsonNode.class);
    JsonNode portAttribute = mock(JsonNode.class);
    JsonNode attributes_son = mock(JsonNode.class);
    JsonNode port = mock(JsonNode.class);
    List<PhysicalNode> result;

    when(nodesRoot.path(any(String.class))).thenReturn(nodes);
    when(nodes.size()).thenReturn(1);
    when(nodes.get(any(Integer.class))).thenReturn(node);
    //get into method "buildnode"
    when(node.get(any(String.class))).thenReturn(node_temp);
    when(node_temp.asText()).thenReturn(new String(""))//branch null
            .thenReturn(new String("test")) //node_id
            .thenReturn(new String("switch")); //NODE_TYPE

    result = (List<PhysicalNode>) method.invoke(phyConfigLoader, nodesRoot); //return empty list
    Assert.assertTrue(result.size() == 0);
    verify(node_temp).asText();

    when(node.path(any(String.class))).thenReturn(ports)//PORTS
            .thenReturn(attributes);//ATTRIBUTES
    ////get into method "buildports"
    when(ports.size()).thenReturn(1);
    when(ports.get(any(Integer.class))).thenReturn(port);
    ///////get into method "buildport"
    when(port.get(any(String.class))).thenReturn(port_temp);
    when(port_temp.asText()).thenReturn(new String("test"))//PORT_ID
            .thenReturn(new String("switch"));//PORT_TYPE
    when(port.path(any(String.class))).thenReturn(portAttributes);
    ////////get into method "buildportattributes"
    when(portAttributes.size()).thenReturn(1);
    when(portAttributes.get(any(Integer.class))).thenReturn(attributes_son);
    //////////get into method "buildPortAttribute"
    when(attributes_son.path(any(String.class))).thenReturn(attribute_temp_son);
    when(attribute_temp_son.asText()).thenReturn(new String("zm")).thenReturn(new String("zm"));
    ////return to method "buildnode" and get into method "..............buildNodeAttributes"
    when(attributes.size()).thenReturn(1);
    when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
    //////get into method "..............buildNodeAttribute"
    when(portAttribute.path(any(String.class))).thenReturn(attribute_temp);
    when(attribute_temp.asText()).thenReturn(new String("test"))//ATTRIBUTE_NAME
            .thenReturn(new String("test"));//ATTRIBUTE_VALUE

    result = (List<PhysicalNode>) method.invoke(phyConfigLoader, nodesRoot); //return empty list
    Assert.assertTrue(result.size() == 1);

}

From source file:tests.SearchTests.java

@Test
public void itemByIdRoute() {
    running(TEST_SERVER, new Runnable() {
        @Override/*from  w  ww  .  j  a  v  a  2 s . c  o m*/
        public void run() {
            final JsonNode jsonObject = Json.parse(call("item/BT000000079:DE-Sol1:GA%20644"));
            assertThat(jsonObject.isArray()).isTrue();
            assertThat(jsonObject.size()).isEqualTo(1);
        }
    });
}

From source file:tests.SearchTests.java

@Test
public void searchViaApiFull() {
    running(TEST_SERVER, new Runnable() {
        @Override//from w  w  w .ja  va 2  s  .  c  om
        public void run() {
            final JsonNode jsonObject = Json.parse(call("resource?author=abraham&format=full"));
            assertThat(jsonObject.isArray()).isTrue();
            assertThat(jsonObject.size())//
                    .isGreaterThan(5 + META).isLessThan(10 + META);
            assertThat(jsonObject.get(0 + META).isContainerNode()).isTrue();
        }
    });
}

From source file:tests.SearchTests.java

@Test
public void searchViaApiShort() {
    running(TEST_SERVER, new Runnable() {
        @Override/* ww  w .j  a v  a  2 s  . c o  m*/
        public void run() {
            final JsonNode jsonObject = Json.parse(call("resource?author=abraham&format=short"));
            assertThat(jsonObject.isArray()).isTrue();
            assertThat(jsonObject.size()).isGreaterThan(5).isLessThan(10);
            assertThat(jsonObject.elements().next().isContainerNode()).isFalse();
        }
    });
}

From source file:fr.gouv.vitam.query.parser.AbstractQueryParser.java

/**
 * $path : [ id1, id2, ... ]//from  w w w. j  a  va  2  s .co m
 *
 * @param refCommand
 * @param command
 * @return the corresponding TypeRequest
 * @throws InvalidParseOperationException
 */
protected final TypeRequest analyzePath(final String refCommand, final JsonNode command)
        throws InvalidParseOperationException {
    final TypeRequest tr0 = new TypeRequest();
    final REQUEST req = getRequestId(refCommand);
    tr0.type = req;
    if (command == null) {
        throw new InvalidParseOperationException("Not correctly parsed: " + refCommand);
    }
    tr0.refId = new ArrayList<>(command.size());
    lastDepth += command.size();
    for (final JsonNode jsonNode : command) {
        tr0.refId.add(jsonNode.asText());
    }
    return tr0;
}

From source file:com.spoiledmilk.ibikecph.navigation.routing_engine.SMRoute.java

boolean parseFromJson(JsonNode jsonRoot, SMRouteListener listener, boolean isBrokenRoute) {
    LOG.d("SMRoute parseFromJson() json = " + jsonRoot);
    synchronized (this) {
        if (jsonRoot == null) {
            return false;
        }/*  w  w  w  .  jav  a 2s. c  o m*/
        waypoints = decodePolyline(jsonRoot.path("route_geometry").textValue());

        if (waypoints.size() < 2) {
            return false;
        }

        turnInstructions = new ArrayList<SMTurnInstruction>();
        pastTurnInstructions = new LinkedList<SMTurnInstruction>();
        visitedLocations = new ArrayList<Location>();
        estimatedArrivalTime = jsonRoot.path("route_summary").path("total_time").asInt();
        arrivalTime = estimatedArrivalTime;
        distancePassed = 0d;
        if (estimatedRouteDistance < 0)
            estimatedRouteDistance = jsonRoot.path("route_summary").path("total_distance").asInt();

        routeChecksum = null;
        destinationHint = null;

        if (!jsonRoot.path("hint_data").path("checksum").isMissingNode()) {
            routeChecksum = jsonRoot.path("hint_data").path("checksum").asText();
        }

        JsonNode hint_locations = jsonRoot.path("hint_data").path("locations");
        if (hint_locations != null && !hint_locations.isMissingNode() && hint_locations.size() > 0) {
            destinationHint = jsonRoot.path("hint_data").path("locations").get(hint_locations.size() - 1)
                    .asText();
        }

        JsonNode routeInstructionsArr = jsonRoot.path("route_instructions");
        if (routeInstructionsArr != null && routeInstructionsArr.size() > 0) {
            int prevlengthInMeters = 0;
            String prevlengthWithUnit = "";
            boolean isFirst = true;
            for (JsonNode instructionNode : routeInstructionsArr) {
                SMTurnInstruction instruction = new SMTurnInstruction();

                String[] arr = instructionNode.get(0).asText().split("-");
                if (arr.length < 1)
                    continue;
                int pos = Integer.valueOf(arr[0]);
                if (pos <= 17) {
                    instruction.drivingDirection = TurnDirection.values()[pos];
                    if (arr.length > 1 && arr[1] != null) {
                        instruction.ordinalDirection = arr[1];
                    } else {
                        instruction.ordinalDirection = "";
                    }

                    instruction.wayName = instructionNode.get(1).asText();
                    if (instruction.wayName.matches("\\{.+\\:.+\\}"))
                        instruction.wayName = IbikeApplication.getString(instruction.wayName);
                    instruction.wayName = instruction.wayName.replaceAll("&#39;", "'");
                    instruction.lengthInMeters = prevlengthInMeters;
                    prevlengthInMeters = instructionNode.get(2).asInt();
                    instruction.timeInSeconds = instructionNode.get(4).asInt();
                    instruction.lengthWithUnit = prevlengthWithUnit;
                    if (instructionNode.size() > 8) {
                        instruction.vehicle = instructionNode.get(8).asInt();
                    }
                    /**
                     * Save length to next turn with units so we don't have to generate it each time It's formatted just the way we like it
                     */
                    instruction.fixedLengthWithUnit = Util.formatDistance(prevlengthInMeters);
                    prevlengthWithUnit = instructionNode.get(5).asText();
                    instruction.directionAbrevation = instructionNode.get(6).asText();
                    instruction.azimuth = (float) instructionNode.get(7).asDouble();

                    if (isFirst) {
                        instruction.generateStartDescriptionString();
                        isFirst = false;
                    } else {
                        instruction.generateDescriptionString();
                    }
                    instruction.generateFullDescriptionString();
                    int position = instructionNode.get(3).asInt();
                    instruction.waypointsIndex = position;
                    if (waypoints != null && position >= 0 && position < waypoints.size())
                        instruction.loc = waypoints.get(position);
                    turnInstructions.add(instruction);
                }
            }
        }
        if (isRouteBroken && turnInstructions != null) {

            double dStat1 = Double.MAX_VALUE, dStat2 = Double.MAX_VALUE;
            for (int i = 0; i < waypoints.size(); i++) {
                if (startStation.distanceTo(waypoints.get(i)) < dStat1) {
                    dStat1 = startStation.distanceTo(waypoints.get(i));
                    waypointStation1 = i;
                }
                if (endStation.distanceTo(waypoints.get(i)) < dStat2) {
                    dStat2 = endStation.distanceTo(waypoints.get(i));
                    waypointStation2 = i;
                }
            }

            Iterator<SMTurnInstruction> it2 = turnInstructions.iterator();
            float distToStart = Float.MAX_VALUE, distToEnd = Float.MAX_VALUE;
            while (it2.hasNext()) {
                SMTurnInstruction smt = it2.next();
                if (smt.loc.distanceTo(startStation) < distToStart && smt.waypointsIndex <= waypointStation1) {
                    distToStart = smt.loc.distanceTo(startStation);
                    station1 = smt;
                }
                if (smt.loc.distanceTo(endStation) < distToEnd && smt.waypointsIndex <= waypointStation2) {
                    distToEnd = smt.loc.distanceTo(endStation);
                    station2 = smt;
                }
            }

            station1.convertToStation(startStationName, stationIcon);
            station2.convertToStation(endStationName, stationIcon);
            int startIndex = turnInstructions.indexOf(station1);
            int endIndex = turnInstructions.indexOf(station2);
            while (startIndex < endIndex - 1) {
                turnInstructions.remove(startIndex + 1);
                startIndex = turnInstructions.indexOf(station1);
                endIndex = turnInstructions.indexOf(station2);
            }
        }

        int longestStreet = 0;
        viaStreets = "";

        int n = jsonRoot.path("route_name").size();
        if (n > 0) {
            int i = 0;
            for (JsonNode streetNode : jsonRoot.path("route_name")) {
                i++;
                viaStreets += streetNode.asText() + (i == n ? "" : ", ");
            }
        }
        if (viaStreets == null || viaStreets.trim().equals("")) {
            for (int i = 1; i < turnInstructions.size() - 1; i++) {
                SMTurnInstruction inst = turnInstructions.get(i);
                if (inst.lengthInMeters > longestStreet) {
                    longestStreet = inst.lengthInMeters;
                    viaStreets = turnInstructions.get(i - 1).wayName;
                }
            }
        }

        lastVisitedWaypointIndex = 0;
    }
    return true;
}

From source file:org.opendaylight.groupbasedpolicy.renderer.opflex.mit.MitLib.java

/**
 * Take the {@link ManagedObject} and deserialize the properties
 * into a concrete type to be used by the renderer. It also
 * adds URIs for any children that are referenced in the
 * properties to the MO's "children" array.
 *
 * @param mo//from w  w w  .  j  a  v a2  s .c  o m
 */
public PolicyObjectInstance deserializeMoProperties(ManagedObject mo, OpflexMit mit) {

    /*
     * The subject gives us the class to use for the schema
     */
    PolicyClassInfo pci = mit.getClass(mo.getSubject());

    // sanity checks
    if (pci == null)
        return null;

    PolicyObjectInstance poi = new PolicyObjectInstance(pci.getClassId());

    if (mo.getProperties() == null)
        return poi;

    for (ManagedObject.Property prop : mo.getProperties()) {
        PolicyPropertyInfo ppi = pci.getProperty(prop.getName());
        if ((ppi == null) || (prop.getData() == null))
            continue;

        JsonNode node = prop.getData();

        boolean vectored = false;
        if (ppi.getPropCardinality().equals(PolicyPropertyInfo.PropertyCardinality.VECTOR)) {
            vectored = true;
        }
        switch (ppi.getType()) {
        case STRING:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<String> sl = new ArrayList<String>();
                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);
                    if (!jn.isTextual())
                        continue;

                    sl.add(jn.asText());
                }
                poi.setString(ppi.getPropId(), sl);
            } else {
                if (!node.isTextual())
                    continue;

                poi.setString(ppi.getPropId(), node.asText());
            }
            break;

        case U64:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<BigInteger> bil = new ArrayList<BigInteger>();

                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);
                    if (!jn.isBigInteger())
                        continue;

                    bil.add(new BigInteger(jn.asText()));
                }
                poi.setUint64(ppi.getPropId(), bil);
            } else {
                if (!node.isBigInteger())
                    continue;

                poi.setUint64(ppi.getPropId(), new BigInteger(node.asText()));
            }
            break;

        case S64:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<Long> ll = new ArrayList<Long>();

                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);

                    if (!jn.isBigInteger())
                        continue;

                    ll.add(jn.asLong());
                }
                poi.setInt64(ppi.getPropId(), ll);
            } else {
                if (!node.isBigInteger())
                    continue;

                poi.setInt64(ppi.getPropId(), node.asLong());
            }
            break;

        case REFERENCE:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<PolicyObjectInstance.PolicyReference> prl = new ArrayList<PolicyObjectInstance.PolicyReference>();

                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);
                    PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(jn, mit);
                    if (pr == null)
                        continue;

                    prl.add(pr);
                }
                poi.setReference(ppi.getPropId(), prl);
            } else {
                PolicyObjectInstance.PolicyReference pr = deserializeMoPropertyRef(node, mit);
                if (pr == null)
                    continue;

                poi.setReference(ppi.getPropId(), pr);
            }
            break;

        case ENUM8:
        case ENUM16:
        case ENUM32:
        case ENUM64:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<BigInteger> bil = new ArrayList<BigInteger>();

                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);
                    if (!jn.isTextual())
                        continue;
                    BigInteger bi = deserializeMoPropertyEnum(node, ppi);

                    bil.add(bi);
                }
                poi.setUint64(ppi.getPropId(), bil);
            } else {
                if (!node.isTextual())
                    continue;

                BigInteger bi = deserializeMoPropertyEnum(node, ppi);
                poi.setUint64(ppi.getPropId(), bi);
            }
            break;

        case MAC:
            if (vectored == true) {
                if (!node.isArray())
                    continue;

                List<MacAddress> ml = new ArrayList<MacAddress>();

                for (int i = 0; i < node.size(); i++) {
                    JsonNode jn = node.get(i);

                    if (!jn.isTextual())
                        continue;

                    ml.add(new MacAddress(jn.asText()));
                }
                poi.setMacAddress(ppi.getPropId(), ml);
            } else {
                if (!node.isTextual())
                    continue;

                poi.setMacAddress(ppi.getPropId(), new MacAddress(node.asText()));
            }
            break;

        case COMPOSITE:

        }

    }

    return poi;
}

From source file:com.neovisionaries.security.JsonDigestUpdater.java

private boolean shouldIgnore(JsonNode value) {
    if (ignoreNull && value.isNull()) {
        // The value of the field is null. Ignore this entry.
        return true;
    }//w  w  w  . j av  a2s. c  om

    if (ignoreFalse && value.isBoolean()) {
        // Ignore this entry if its value is false.
        return value.asBoolean() == false;
    }

    if (ignoreZero && value.isNumber()) {
        // Ignore this entry if its value is zero.
        return isZero(value);
    }

    if (ignoreEmptyString && value.isTextual()) {
        // Ignore this entry if its value is an empty string.
        return value.textValue().length() == 0;
    }

    if (ignoreEmptyArray && value.isArray()) {
        // Ignore this entry if its value is an empty array.
        return value.size() == 0;
    }

    if (ignoreEmptyObject && value.isObject()) {
        // Ignore this entry if its value is an empty object.
        return value.size() == 0;
    }

    // Should not ignore.
    return false;
}

From source file:tests.SearchTests.java

@Test
public void resByNwBibSubjectUri() {
    running(TEST_SERVER, new Runnable() {
        @Override/* www.j  ava  2 s . c  om*/
        public void run() {
            final JsonNode jsonObject = Json
                    .parse(call("resource?nwbibsubject=http://purl.org/lobid/nwbib#s552000"));
            assertThat(jsonObject.isArray()).isTrue();
            assertThat(jsonObject.size()).isEqualTo(1 + META);
        }
    });
}