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.unboundid.scim2.common.DiffTestCase.java

/**
 * Test comparison of multi-valued complex attributes.
 *
 * @throws Exception if an error occurs.
 *//*from w w  w . j  a v a 2  s.  c  om*/
@Test
public void testDiffMultiValuedAttribute() throws Exception {
    // *** multi-valued ***
    ObjectNode source = JsonUtils.getJsonNodeFactory().objectNode();
    ObjectNode target = JsonUtils.getJsonNodeFactory().objectNode();

    // - unchanged
    String email1 = "bjensen@example.com";
    String email2 = "babs@jensen.org";

    source.putArray("emails").add(email1).add(email2);
    target.putArray("emails").add(email1).add(email2);

    // - added
    String phone1 = "1234567890";
    String phone2 = "0987654321";

    target.putArray("phones").add(phone1).add(phone2);

    // - removed
    String im1 = "babs";
    String im2 = "bjensen";

    source.putArray("ims").add(im1).add(im2);
    target.putArray("ims");

    // - updated
    // -- unchanged
    String photo0 = "http://photo0";
    String photo1 = "http://photo1";
    // -- add a new value
    String photo3 = "http://photo3";
    // -- remove a value
    String thumbnail = "http://thumbnail1";

    source.putArray("photos").add(photo0).add(photo1).add(thumbnail);
    target.putArray("photos").add(photo0).add(photo1).add(photo3);

    // -- updated with all new values
    String entitlement1 = "admin";
    String entitlement2 = "user";
    String entitlement3 = "inactive";
    source.putArray("entitlements").add(entitlement1).add(entitlement2);
    target.putArray("entitlements").add(entitlement3);

    List<PatchOperation> d = JsonUtils.diff(source, target, false);
    assertEquals(d.size(), 4);

    assertTrue(d.contains(PatchOperation.remove(Path.root().attribute("ims"))));
    assertTrue(
            d.contains(PatchOperation.remove(Path.root().attribute("photos", Filter.eq("value", thumbnail)))));
    ObjectNode replaceValue = JsonUtils.getJsonNodeFactory().objectNode();
    replaceValue.putArray("entitlements").add(entitlement3);
    replaceValue.putArray("phones").add(phone1).add(phone2);
    assertTrue(d.contains(PatchOperation.replace(replaceValue)));
    ObjectNode addValue = JsonUtils.getJsonNodeFactory().objectNode();
    addValue.putArray("photos").add(photo3);
    assertTrue(d.contains(PatchOperation.add(addValue)));

    List<PatchOperation> d2 = JsonUtils.diff(source, target, true);
    for (PatchOperation op : d2) {
        op.apply(source);
    }
    removeNullNodes(target);

    // Have to compare photos explicitly since ordering of array values doesn't
    // matter.
    JsonNode sourcePhotos = source.remove("photos");
    JsonNode targetPhotos = target.remove("photos");
    assertEquals(sourcePhotos.size(), targetPhotos.size());
    for (JsonNode sourceValue : sourcePhotos) {
        boolean found = false;
        for (JsonNode targetValue : targetPhotos) {
            if (sourceValue.equals(targetValue)) {
                found = true;
                break;
            }
        }
        if (!found) {
            fail("Source photo value " + sourceValue + " not in target photo array " + targetPhotos);
        }
    }
    assertEquals(source, target);
}

From source file:com.delphix.delphix.DelphixEngine.java

/**
 * List timeflows in the Delphix Engine/*from  w  ww.j av a 2  s.com*/
 */
public LinkedHashMap<String, DelphixTimeflow> listTimeflows()
        throws ClientProtocolException, IOException, DelphixEngineException {
    // Get containers
    LinkedHashMap<String, DelphixTimeflow> timeflows = new LinkedHashMap<String, DelphixTimeflow>();
    JsonNode timeflowsJSON = engineGET(PATH_TIMEFLOW).get(FIELD_RESULT);

    // Loop through container list
    for (int i = 0; i < timeflowsJSON.size(); i++) {
        JsonNode timeflowJSON = timeflowsJSON.get(i);
        // Create container object from JSON result
        JsonNode parentPoint = timeflowJSON.get(FIELD_PARENT_POINT);
        String timestamp = "N/A";
        if (!parentPoint.isNull()) {
            timestamp = parentPoint.get(FIELD_TIMESTAMP).asText();
        }
        DelphixTimeflow timeflow = new DelphixTimeflow(timeflowJSON.get(FIELD_REFERENCE).asText(),
                timeflowJSON.get(FIELD_NAME).asText(), timestamp, timeflowJSON.get(FIELD_CONTAINER).asText());
        timeflows.put(timeflow.getReference(), timeflow);
    }

    return timeflows;
}

From source file:com.delphix.delphix.DelphixEngine.java

/**
 * List environments in the Delphix Engine
 *//*w ww . java 2 s. co  m*/
public LinkedHashMap<String, DelphixEnvironment> listEnvironments()
        throws ClientProtocolException, IOException, DelphixEngineException {
    // Get containers
    LinkedHashMap<String, DelphixEnvironment> environments = new LinkedHashMap<String, DelphixEnvironment>();
    JsonNode environmentsJSON = engineGET(PATH_ENVIRONMENT).get(FIELD_RESULT);

    // Loop through container list
    for (int i = 0; i < environmentsJSON.size(); i++) {
        JsonNode environmentJSON = environmentsJSON.get(i);
        DelphixEnvironment environment = new DelphixEnvironment(environmentJSON.get(FIELD_REFERENCE).asText(),
                environmentJSON.get(FIELD_NAME).asText());
        environments.put(environment.getReference(), environment);
    }

    return environments;
}

From source file:com.linecorp.armeria.common.thrift.text.TTextProtocol.java

/**
 * Helper shared by read{List/Set}Begin//from w ww .j  a  va  2 s . com
 */
private int readSequenceBegin() throws TException {
    getCurrentContext().read();
    if (getCurrentContext().isMapKey()) {
        throw new TException(SEQUENCE_AS_KEY_ILLEGAL);
    }

    JsonNode curElem = getCurrentContext().getCurrentChild();
    if (!curElem.isArray()) {
        throw new TException("Expected JSON Array!");
    }

    pushContext(new SequenceContext(curElem));
    return curElem.size();
}

From source file:com.delphix.delphix.DelphixEngine.java

/**
 * List containers in the Delphix Engine
 *//* w  ww . ja  va2 s  . c om*/
public LinkedHashMap<String, DelphixContainer> listContainers()
        throws ClientProtocolException, IOException, DelphixEngineException {
    // Get containers
    LinkedHashMap<String, DelphixContainer> containers = new LinkedHashMap<String, DelphixContainer>();
    JsonNode containersJSON = engineGET(PATH_DATABASE).get(FIELD_RESULT);

    // Loop through container list
    for (int i = 0; i < containersJSON.size(); i++) {
        JsonNode containerJSON = containersJSON.get(i);
        ContainerType type;

        /*
         * Set the type of the container. Versions of Delphix before 4.4
         * classify transformation containers and restoration datasets as
         * VDBs. They are differentiated in 4.4.
         */
        if (containerJSON.get(FIELD_PROVISION_CONTAINER).asText().equals("null")) {
            type = ContainerType.SOURCE;
        } else {
            type = ContainerType.VDB;
        }

        // Create container object from JSON result
        DelphixContainer container = new DelphixContainer(engineAddress, containerJSON.get(FIELD_NAME).asText(),
                containerJSON.get(FIELD_REFERENCE).asText(), type, containerJSON.get(FIELD_GROUP).asText(),
                containerJSON.get(FIELD_CURRENT_TIMEFLOW).asText(), containerJSON.get(FIELD_TYPE).asText());
        containers.put(container.getReference(), container);
    }

    return containers;
}

From source file:com.delphix.delphix.DelphixEngine.java

/**
 * Get cluster nodes//  w w w. ja  v a 2s  . c  om
 */
public ArrayList<DelphixClusterNode> listClusterNodes() throws IOException, DelphixEngineException {
    JsonNode result = engineGET(PATH_CLUSTER_NODES);
    JsonNode jsonClusterNodes = result.get(FIELD_RESULT);
    ArrayList<DelphixClusterNode> clusterNodes = new ArrayList<DelphixClusterNode>();
    for (int i = 0; i < jsonClusterNodes.size(); i++) {
        DelphixClusterNode clusterNode = new DelphixClusterNode(
                jsonClusterNodes.get(i).get(FIELD_NAME).asText(),
                jsonClusterNodes.get(i).get(FIELD_REFERENCE).asText(),
                jsonClusterNodes.get(i).get(FIELD_CLUSTER).asText());
        clusterNodes.add(clusterNode);
    }
    return clusterNodes;
}

From source file:com.delphix.delphix.DelphixEngine.java

/**
 * Get the compatible provision repositories
 *//*w ww. j  a va  2 s  .c om*/
private ArrayList<DelphixRepository> getCompatibleRepositories(String environmentRef,
        String provisionParameters) throws IOException, DelphixEngineException {
    JsonNode result = enginePOST(PATH_COMPATIBLE_REPOSITORIES,
            String.format(CONTENT_COMPATIBLE_REPOSITORIES, environmentRef, provisionParameters));
    JsonNode jsonRepositories = result.get(FIELD_RESULT).get(FIELD_REPOSITORIES);
    ArrayList<DelphixRepository> repositories = new ArrayList<DelphixRepository>();
    for (int i = 0; i < jsonRepositories.size(); i++) {
        DelphixRepository repository = new DelphixRepository(jsonRepositories.get(i).get(FIELD_NAME).asText(),
                jsonRepositories.get(i).get(FIELD_REFERENCE).asText(),
                jsonRepositories.get(i).get(FIELD_ENVIRONMENT).asText(),
                jsonRepositories.get(i).get(FIELD_RAC).asBoolean());
        repositories.add(repository);
    }
    return repositories;
}

From source file:org.opendaylight.nemo.renderer.cli.physicalnetwork.PhysicalResourceLoaderTest.java

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

    List<PhysicalNode> result = new ArrayList<PhysicalNode>();
    JsonNode nodesRoot = mock(JsonNode.class);
    JsonNode nodes = mock(JsonNode.class);
    JsonNode node = mock(JsonNode.class);
    JsonNode node_temp_buildnode = mock(JsonNode.class);
    JsonNode ports = mock(JsonNode.class);
    JsonNode attributes = mock(JsonNode.class);
    JsonNode port = mock(JsonNode.class);
    JsonNode port_temp_buildport = mock(JsonNode.class);
    JsonNode portAttributes = mock(JsonNode.class);
    JsonNode portAttribute = mock(JsonNode.class);
    JsonNode port_temp_buildPortAttribute = mock(JsonNode.class);
    JsonNode portAttribute_father = mock(JsonNode.class);
    JsonNode attribute_temp_father = mock(JsonNode.class);

    when(nodesRoot.path(any(String.class))).thenReturn(nodes);
    when(nodes.size()).thenReturn(1);
    when(nodes.get(any(Integer.class))).thenReturn(node);
    //get into method "build Node"
    when(node.get(any(String.class))).thenReturn(node_temp_buildnode);
    when(node_temp_buildnode.asText()).thenReturn(new String(""))//test null
            .thenReturn(new String("test"))//node id
            .thenReturn(new String("switch"));// node type
    result = (List<PhysicalNode>) method.invoke(physicalResourceLoader, nodesRoot);
    Assert.assertTrue(result.size() == 0);
    verify(node_temp_buildnode).asText();
    when(node.path(any(String.class))).thenReturn(ports)//get into method "build ports"
            .thenReturn(attributes); //get into method "build attributes"
    ////get into method"build ports"
    when(ports.size()).thenReturn(1);
    when(ports.get(any(Integer.class))).thenReturn(port);
    //////get into method "build port"
    when(port.get(any(String.class))).thenReturn(port_temp_buildport);
    when(port_temp_buildport.asText()).thenReturn(new String("test"))//port id
            .thenReturn(new String("external"))//port type
            .thenReturn(new String("00:11:22:33:44:55"))//if(!(port.get("port-mac-address").asText().equals("")))  get in
            .thenReturn(new String("00:11:22:33:44:55"))//mac address
            .thenReturn(new String(""));// if(port.get("bandwidth").asText().equals(""))  get in
    when(port.path(any(String.class))).thenReturn(portAttributes);
    ////////get into method "buildPortAttributes" args(portAttributes)
    when(portAttributes.size()).thenReturn(1);
    when(portAttributes.get(any(Integer.class))).thenReturn(portAttribute);
    ////////// get into method "buildPortAttribute" args(portAttribute)
    when(portAttribute.path(any(String.class))).thenReturn(port_temp_buildPortAttribute);
    when(port_temp_buildPortAttribute.asText()).thenReturn(new String("test"))//ATTRIBUTE_NAME
            .thenReturn(new String("test"));//ATTRIBUTE_VALUE
    ////return to method "buildnode" and get into method "..............buildNodeAttributes"
    when(attributes.size()).thenReturn(1);
    when(attributes.get(any(Integer.class))).thenReturn(portAttribute_father);
    //////get into method "..............buildNodeAttribute"
    when(portAttribute_father.path(any(String.class))).thenReturn(attribute_temp_father);
    when(attribute_temp_father.asText()).thenReturn(new String("test"))//ATTRIBUTE_NAME
            .thenReturn(new String("test"));//ATTRIBUTE_VALUE
    result = (List<PhysicalNode>) method.invoke(physicalResourceLoader, nodesRoot); //return empty list
    Assert.assertTrue(result.size() == 1);
    verify(port_temp_buildport, times(5)).asText();
    verify(attribute_temp_father, times(2)).asText();
}

From source file:com.github.fge.jackson.JsonNumEquals.java

@Override
protected boolean doEquivalent(final JsonNode a, final JsonNode b) {
    /*// ww w. j  a va2 s. c o m
     * If both are numbers, delegate to the helper method
     */
    if (a.isNumber() && b.isNumber())
        return numEquals(a, b);

    final NodeType typeA = NodeType.getNodeType(a);
    final NodeType typeB = NodeType.getNodeType(b);

    /*
     * If they are of different types, no dice
     */
    if (typeA != typeB)
        return false;

    /*
     * For all other primitive types than numbers, trust JsonNode
     */
    if (!a.isContainerNode())
        return a.equals(b);

    /*
     * OK, so they are containers (either both arrays or objects due to the
     * test on types above). They are obviously not equal if they do not
     * have the same number of elements/members.
     */
    if (a.size() != b.size())
        return false;

    /*
     * Delegate to the appropriate method according to their type.
     */
    return typeA == NodeType.ARRAY ? arrayEquals(a, b) : objectEquals(a, b);
}

From source file:nosqltools.JSONUtilities.java

private DefaultMutableTreeNode makeJtree(String name, JsonNode node) {
    //instance of default mutable tree node with the root name of that object
    DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(name);
    //iterator that stores the fields of the JSON documents in the collection.
    Iterator<Entry<String, JsonNode>> iterator = node.fields();

    while (iterator.hasNext()) {
        //the iterator returns the next entry 
        Entry<String, JsonNode> entry = iterator.next();
        //the entry will be added to the tree node and formatted as key:value
        treeNode.add(makeJtree(entry.getKey() + " : " + entry.getValue(), entry.getValue()));
    }/*from   w w w.j a v  a  2  s .c o  m*/

    //if an array is found within an object
    if (node.isArray()) {
        for (int i = 0; i < node.size(); i++) {
            //create a child and get the information
            JsonNode child = node.get(i);

            //the isValueNode returns valid String representation of the container value, if the node is a value node else null
            if (child.isValueNode())
                treeNode.add(new DefaultMutableTreeNode(child.asText()));
            else
                treeNode.add(makeJtree(String.format("Node %d", i), child));
        }
    }

    return treeNode;
}