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.dswarm.graph.json.deserializer.ResourceDeserializer.java

@Override
public Resource deserialize(final JsonParser jp, final DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    final ObjectCodec oc = jp.getCodec();

    if (oc == null) {

        return null;
    }//from   ww w.  ja va 2 s .c o  m

    final JsonNode node = oc.readTree(jp);

    if (node == null) {

        return null;
    }

    final Iterator<String> resourceUriFieldNames = node.fieldNames();

    if (resourceUriFieldNames == null || !resourceUriFieldNames.hasNext()) {

        return null;
    }

    final String resourceUri = resourceUriFieldNames.next();

    if (resourceUri == null) {

        return null;
    }

    final JsonNode resourceNode = node.get(resourceUri);

    if (resourceNode == null) {

        return null;
    }

    if (!ArrayNode.class.isInstance(resourceNode)) {

        throw new JsonParseException("expected a JSON array full of statement objects of the resource",
                jp.getCurrentLocation());
    }

    final Resource resource = new Resource(resourceUri);

    if (resourceNode.size() <= 0) {

        return resource;
    }

    for (final JsonNode statementNode : resourceNode) {

        final Statement statement = statementNode.traverse(oc).readValueAs(Statement.class);
        resource.addStatement(statement);
    }

    return resource;
}

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

private List<Attribute> buildPortAttributes(JsonNode attributes) {
    List<Attribute> attributeList = new ArrayList<Attribute>();
    for (int i = 0; i < attributes.size(); i++) {
        log.debug("build physical port attribute execution body.");
        JsonNode portAttribute = attributes.get(i);
        Attribute attribute = buildPortAttribute(portAttribute);
        if (attribute != null) {
            attributeList.add(attribute);
        }//from w w w.ja va2 s.c om
    }
    return attributeList;
}

From source file:com.googlecode.jsonschema2pojo.rules.DefaultRule.java

/**
 * Creates a default value for a list property by:
 * <ol>//ww  w  .j av  a  2s  .  c o  m
 * <li>Creating a new {@link ArrayList} with the correct generic type
 * <li>Using {@link Arrays#asList(Object...)} to initialize the list with
 * the correct default values
 * </ol>
 * 
 * @param fieldType
 *            the java type that applies for this field ({@link List} with
 *            some generic type argument)
 * @param node
 *            the node containing default values for this list
 * @return an expression that creates a default value that can be assigned
 *         to this field
 */
private JExpression getDefaultList(JType fieldType, JsonNode node) {

    JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);

    JClass listImplClass = fieldType.owner().ref(ArrayList.class);
    listImplClass = listImplClass.narrow(listGenericType);

    JInvocation newListImpl = JExpr._new(listImplClass);

    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
        }
        newListImpl.arg(invokeAsList);
    }

    return newListImpl;

}

From source file:org.opendaylight.nemo.renderer.openflow.physicalnetwork.PhyConfigLoader.java

private void buildExternals(JsonNode externalRoot) {
    JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC);
    log.debug("Build external network mac : {} .", exNetworkNodes);
    for (int i = 0; i < exNetworkNodes.size(); i++) {
        log.debug("Build external network execution body");
        JsonNode exNetworkNode = exNetworkNodes.get(i);
        buildExNetwork(exNetworkNode);/*from w  ww . j a va 2  s  . c  om*/
    }
}

From source file:com.ocdsoft.bacta.soe.json.schema.SoeDefaultRule.java

/**
 * Creates a default value for a set property by:
 * <ol>/*from   w w  w .j  av a2s. c om*/
 * <li>Creating a new {@link LinkedHashSet} with the correct generic type
 * <li>Using {@link Arrays#asList(Object...)} to initialize the set with the
 * correct default values
 * </ol>
 *
 * @param fieldType
 *            the java type that applies for this field ({@link Set} with
 *            some generic type argument)
 * @param node
 *            the node containing default values for this set
 * @return an expression that creates a default value that can be assigned
 *         to this field
 */
private JExpression getDefaultSet(JType fieldType, JsonNode node) {

    JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0);

    JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class);
    setImplClass = setImplClass.narrow(setGenericType);

    JInvocation newSetImpl = JExpr._new(setImplClass);

    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(setGenericType, defaultValue));
        }
        newSetImpl.arg(invokeAsList);
    } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
        return JExpr._null();
    }

    return newSetImpl;

}

From source file:com.ocdsoft.bacta.soe.json.schema.SoeDefaultRule.java

/**
 * Creates a default value for a list property by:
 * <ol>//from   w  w  w .  ja va2  s .  c om
 * <li>Creating a new {@link ArrayList} with the correct generic type
 * <li>Using {@link Arrays#asList(Object...)} to initialize the list with
 * the correct default values
 * </ol>
 *
 * @param fieldType
 *            the java type that applies for this field ({@link List} with
 *            some generic type argument)
 * @param node
 *            the node containing default values for this list
 * @return an expression that creates a default value that can be assigned
 *         to this field
 */
private JExpression getDefaultList(JType fieldType, JsonNode node) {

    JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0);

    JClass listImplClass = fieldType.owner().ref(ArrayList.class);
    listImplClass = listImplClass.narrow(listGenericType);

    JInvocation newListImpl = JExpr._new(listImplClass);

    if (node instanceof ArrayNode && node.size() > 0) {
        JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList");
        for (JsonNode defaultValue : node) {
            invokeAsList.arg(getDefaultValue(listGenericType, defaultValue));
        }
        newListImpl.arg(invokeAsList);
    } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) {
        return JExpr._null();
    }

    return newListImpl;

}

From source file:org.apache.taverna.robundle.manifest.TestManifestJSON.java

public void checkManifestJson(JsonNode json) {
    JsonNode context = json.get("@context");
    assertNotNull("Could not find @context", context);
    assertTrue("@context SHOULD be an array", context.isArray());
    assertTrue("@context SHOULD include a context", context.size() > 0);
    JsonNode lastContext = context.get(context.size() - 1);
    assertEquals("@context SHOULD include https://w3id.org/bundle/context as last item",
            "https://w3id.org/bundle/context", lastContext.asText());

    assertEquals("/", json.get("id").asText());

    JsonNode manifest = json.get("manifest");
    if (manifest.isValueNode()) {
        assertEquals("manifest SHOULD be literal value \"manifest.json\" or list", "manifest.json",
                manifest.asText());// w  w w.j  a  v a2 s .  com
    } else {
        assertTrue("manifest is neither literal or list", manifest.isArray());
        boolean found = false;
        for (JsonNode n : manifest) {
            found = n.asText().equals("manifest.json");
            if (found) {
                break;
            }
        }
        ;
        assertTrue("Could not find 'manifest.json' in 'manifest' list: " + manifest, found);
    }

    assertEquals("2013-03-05T17:29:03Z", json.get("createdOn").asText());
    JsonNode createdBy = json.get("createdBy");
    assertNotNull("Could not find createdBy", createdBy);
    assertEquals("http://example.com/foaf#alice", createdBy.get("uri").asText());
    assertEquals("http://orcid.org/0000-0002-1825-0097", createdBy.get("orcid").asText());
    assertEquals("Alice W. Land", createdBy.get("name").asText());

    JsonNode history = json.get("history");
    if (history.isValueNode()) {
        assertEquals("evolution.ttl", history.asText());
    } else {
        assertEquals("evolution.ttl", history.get(0).asText());
    }

    JsonNode aggregates = json.get("aggregates");
    assertTrue("aggregates not a list", aggregates.isArray());
    JsonNode soup = aggregates.get(0);
    if (soup.isValueNode()) {
        assertEquals("/folder/soup.jpeg", soup.asText());
    } else {
        assertEquals("/folder/soup.jpeg", soup.get("uri").asText());
    }

    JsonNode blog = aggregates.get(1);
    if (blog.isValueNode()) {
        assertEquals("http://example.com/blog/", blog.asText());
    } else {
        assertEquals("http://example.com/blog/", blog.get("uri").asText());
    }

    JsonNode readme = aggregates.get(2);
    assertEquals("/README.txt", readme.get("uri").asText());
    assertEquals("text/plain", readme.get("mediatype").asText());
    assertEquals("2013-02-12T19:37:32.939Z", readme.get("createdOn").asText());
    JsonNode readmeCreatedBy = readme.get("createdBy");
    assertEquals("http://example.com/foaf#bob", readmeCreatedBy.get("uri").asText());
    assertEquals("Bob Builder", readmeCreatedBy.get("name").asText());

    JsonNode comments = aggregates.get(3);
    assertEquals("http://example.com/comments.txt", comments.get("uri").asText());
    JsonNode bundledAs = comments.get("bundledAs");
    assertEquals("urn:uuid:a0cf8616-bee4-4a71-b21e-c60e6499a644", bundledAs.get("uri").asText());
    assertEquals("/folder/", bundledAs.get("folder").asText());
    assertEquals("external.txt", bundledAs.get("filename").asText());

    JsonNode annotations = json.get("annotations");
    assertTrue("annotations MUST be a list", annotations.isArray());

    JsonNode ann0 = annotations.get(0);
    assertEquals("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf", ann0.get("uri").asText());
    assertEquals("/folder/soup.jpeg", ann0.get("about").asText());
    assertEquals("annotations/soup-properties.ttl", ann0.get("content").asText());

    JsonNode ann1 = annotations.get(1);
    assertNull(ann1.get("annotation"));
    assertEquals("urn:uuid:a0cf8616-bee4-4a71-b21e-c60e6499a644", ann1.get("about").asText());
    assertEquals("http://example.com/blog/they-aggregated-our-file", ann1.get("content").asText());

    JsonNode ann2 = annotations.get(2);
    assertNull(ann2.get("annotation"));
    JsonNode about = ann2.get("about");
    assertTrue("about was not a list", about.isArray());
    assertEquals("/", about.get(0).asText());
    assertEquals("urn:uuid:d67466b4-3aeb-4855-8203-90febe71abdf", about.get(1).asText());
    assertEquals("annotations/a-meta-annotation-in-this-ro.txt", ann2.get("content").asText());

}

From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java

@Test
public void testAuthenticateForTokenById() throws JsonGenerationException, JsonMappingException, IOException {
    user = new User();
    user.setId("e7912c2225e84ac5905d8cf0b5040a6d");
    user.setPassword("f00@bar");
    // user.setDomain(domain);
    // domain = new Domain();
    // domain.setId("default");
    identity = new Identity();
    identity.getMethods().add("password");
    password = new Password();
    password.setUser(user);/*from   w  ww. j av a  2  s.co m*/
    identity.getAuthMethods().put("password", password);
    AuthV3 auth = new AuthV3();
    auth.setIdentity(identity);
    AuthV3Wrapper wrapper = new AuthV3Wrapper(auth);
    String json = JsonUtils.toJson(wrapper);
    JsonNode node = JsonUtils.convertToJsonNode(json);
    JsonNode authJson = node.get("auth");
    assertNotNull(authJson);
    JsonNode identityJson = authJson.get("identity");
    assertNotNull(identityJson);
    JsonNode methodsJson = identityJson.get("methods");
    assertEquals(1, methodsJson.size());
    assertEquals("password", methodsJson.get(0).asText());
    JsonNode passwordJson = identityJson.get("password");
    assertNotNull(passwordJson);
    JsonNode userJson = passwordJson.get("user");
    assertNotNull(userJson);
    assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText());
    assertEquals("f00@bar", userJson.get("password").asText());

    // authenticate
    Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request()
            .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE));

    assertEquals(201, response.getStatus());
    System.err.println(response.readEntity(String.class));
}

From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java

@Test
public void testAuthenticateForTokenByIdAnsProjectScope()
        throws JsonGenerationException, JsonMappingException, IOException {
    user = new User();
    user.setId("e7912c2225e84ac5905d8cf0b5040a6d");
    user.setPassword("f00@bar");
    identity = new Identity();
    identity.getMethods().add("password");
    password = new Password();
    password.setUser(user);// ww w.  j av  a 2 s  . c o m
    identity.getAuthMethods().put("password", password);
    Scope scope = new Scope();
    AuthV3 auth = new AuthV3();
    auth.setScope(scope);
    Project project = new Project();
    project.setId("88e550a135bb4e6da68e79e5b7c4b2f2");
    scope.setProject(project);
    auth.setIdentity(identity);
    AuthV3Wrapper wrapper = new AuthV3Wrapper(auth);
    String json = JsonUtils.toJson(wrapper);
    JsonNode node = JsonUtils.convertToJsonNode(json);
    JsonNode authJson = node.get("auth");
    assertNotNull(authJson);
    JsonNode identityJson = authJson.get("identity");
    assertNotNull(identityJson);
    JsonNode methodsJson = identityJson.get("methods");
    assertEquals(1, methodsJson.size());
    assertEquals("password", methodsJson.get(0).asText());
    JsonNode passwordJson = identityJson.get("password");
    assertNotNull(passwordJson);
    JsonNode userJson = passwordJson.get("user");
    assertNotNull(userJson);
    assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText());
    assertEquals("f00@bar", userJson.get("password").asText());

    // authenticate
    Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request()
            .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE));

    assertEquals(201, response.getStatus());
    System.err.println(response.readEntity(String.class));
}

From source file:com.infinities.keystone4j.admin.v3.auth.AuthResourceTest.java

@Test
public void testAuthenticateForTokenByIdAnsDomainScope()
        throws JsonGenerationException, JsonMappingException, IOException {
    user = new User();
    user.setId("e7912c2225e84ac5905d8cf0b5040a6d");
    user.setPassword("f00@bar");
    identity = new Identity();
    identity.getMethods().add("password");
    password = new Password();
    password.setUser(user);/*from   w w w  .  j av a 2s  .c o  m*/
    identity.getAuthMethods().put("password", password);
    Scope scope = new Scope();
    AuthV3 auth = new AuthV3();
    auth.setScope(scope);
    Domain d = new Domain();
    d.setId("default");
    scope.setDomain(d);
    auth.setIdentity(identity);
    AuthV3Wrapper wrapper = new AuthV3Wrapper(auth);
    String json = JsonUtils.toJson(wrapper);
    JsonNode node = JsonUtils.convertToJsonNode(json);
    JsonNode authJson = node.get("auth");
    assertNotNull(authJson);
    JsonNode identityJson = authJson.get("identity");
    assertNotNull(identityJson);
    JsonNode methodsJson = identityJson.get("methods");
    assertEquals(1, methodsJson.size());
    assertEquals("password", methodsJson.get(0).asText());
    JsonNode passwordJson = identityJson.get("password");
    assertNotNull(passwordJson);
    JsonNode userJson = passwordJson.get("user");
    assertNotNull(userJson);
    assertEquals("e7912c2225e84ac5905d8cf0b5040a6d", userJson.get("id").asText());
    assertEquals("f00@bar", userJson.get("password").asText());

    // authenticate
    Response response = target("/v3").path("auth").path("tokens").register(JacksonFeature.class).request()
            .post(Entity.entity(wrapper, MediaType.APPLICATION_JSON_TYPE));

    assertEquals(201, response.getStatus());
    String ret = response.readEntity(String.class);
    node = JsonUtils.convertToJsonNode(ret);
    assertNotNull(node.get("token").get("domain"));
    System.err.println(ret);
}