Example usage for com.fasterxml.jackson.databind.node ArrayNode size

List of usage examples for com.fasterxml.jackson.databind.node ArrayNode size

Introduction

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

Prototype

public int size() 

Source Link

Usage

From source file:org.emfjson.jackson.tests.uuids.UuidSaveTest.java

@Test
public void testSerializeOneRootWithTwoChildHavingOneReference() throws IOException {
    Resource resource = createUuidResource("test.xmi", mapper);

    Container root = ModelFactory.eINSTANCE.createContainer();
    ConcreteTypeOne one = ModelFactory.eINSTANCE.createConcreteTypeOne();
    ConcreteTypeOne two = ModelFactory.eINSTANCE.createConcreteTypeOne();

    one.setName("one");
    two.setName("two");

    one.getRefProperty().add(two);/*from www . j av a2s. c om*/

    root.getElements().add(one);
    root.getElements().add(two);

    resource.getContents().add(root);

    JsonNode node = mapper.valueToTree(root);

    assertNotNull(node);
    assertNotNull(node.get("@id"));
    assertEquals(uuid(root), uuid(node));

    assertTrue(node.get("elements").isArray());

    ArrayNode elements = (ArrayNode) node.get("elements");
    assertEquals(2, elements.size());

    JsonNode node1 = elements.get(0);
    JsonNode node2 = elements.get(1);

    assertNotNull(node1.get("@id"));
    assertEquals(uuid(one), uuid(node1));

    assertNotNull(node2.get("@id"));
    assertEquals(uuid(two), uuid(node2));

    assertNotNull(node1.get("refProperty"));
    assertNull(node2.get("refProperty"));
    assertTrue(node1.get("refProperty").isArray());

    ArrayNode refProperty = (ArrayNode) node1.get("refProperty");
    assertEquals(1, refProperty.size());

    JsonNode ref = refProperty.get(0);
    assertNotNull(ref.get("$ref"));

    assertEquals(uuid(two), ref.get("$ref").asText());
}

From source file:org.gitana.platform.client.transfer.TransferImportJob.java

public List<TransferDependency> getTargets() {
    List<TransferDependency> targets = new ArrayList<TransferDependency>();

    ArrayNode array = getArray(FIELD_TARGETS);
    for (int i = 0; i < array.size(); i++) {
        ObjectNode object = (ObjectNode) array.get(i);

        String typeId = JsonUtil.objectGetString(object, "typeId");
        String id = JsonUtil.objectGetString(object, "id");

        TransferDependency dependency = new TransferDependency(typeId, id);
        targets.add(dependency);//from   w  ww. j  ava 2 s. c  om
    }

    return targets;
}

From source file:de.thingweb.client.ClientFactory.java

protected Client pickClient() throws UnsupportedException, URISyntaxException {
    // check for right protocol&encoding
    List<Client> clients = new ArrayList<>(); // it is assumed URIs are ordered by priority

    JsonNode uris = thing.getMetadata().get("uris");
    if (uris != null) {
        if (uris.getNodeType() == JsonNodeType.STRING) {
            checkUri(uris.asText(), clients);
        } else if (uris.getNodeType() == JsonNodeType.ARRAY) {
            ArrayNode an = (ArrayNode) uris;
            for (int i = 0; i < an.size(); i++) {
                checkUri(an.get(i).asText(), i, clients);
            }//from   ww  w.j  a v a 2  s.c om
        }

        // int prio = 1;
        //      for (JsonNode juri : uris) {
        //       String suri = juri.asText();
        //        URI uri = new URI(suri);
        //        if(isCoapScheme(uri.getScheme())) {
        //          clients.add(new CoapClientImpl(suri, thing));
        //          log.info("Found matching client '" + CoapClientImpl.class.getName() + "' with priority " + prio++);
        //        } else if(isHttpScheme(uri.getScheme())) {
        //          clients.add(new HttpClientImpl(suri, thing));
        //          log.info("Found matching client '" + HttpClientImpl.class.getName() + "' with priority " + prio++);
        //        } 
        //      }
    }

    // take priority into account
    if (clients.isEmpty()) {
        log.warn("No fitting client implementation found!");
        throw new UnsupportedException("No fitting client implementation found!");
        // return null;
    } else {
        // pick first one with highest priority
        Client c = clients.get(0);
        log.info("Use '" + c.getClass().getName() + "' according to priority");
        return c;
    }

}

From source file:com.turn.shapeshifter.AutoParser.java

/**
 * Parses a repeated field.//  w w w .  j  av a 2s.  com
 *
 * @param builder the builder in which the parsed field should be set
 * @param fieldName the JSON name of the field
 * @param field the descriptor of the repeated field being parsed
 * @param valueNode the JSON node being parsed
 * @param registry used as a source of schemas generated on the fly for
 * sub-objects
 * @throws ParsingException
 */
private void parseRepeatedField(Message.Builder builder, String fieldName, FieldDescriptor field,
        JsonNode valueNode, ReadableSchemaRegistry registry) throws ParsingException {
    if (!valueNode.isArray()) {
        throw new IllegalArgumentException(
                "Field '" + fieldName + "' is expected to be an array, but was " + valueNode.asToken());
    }
    ArrayNode array = (ArrayNode) valueNode;
    if (array.size() != 0) {
        for (JsonNode item : array) {
            Object value = parseValue(item, field, registry);
            if (value != null) {
                builder.addRepeatedField(field, value);
            }
        }
    }
}

From source file:org.walkmod.conf.providers.yml.RemoveIncludesOrExcludesYMLAction.java

@Override
public void doAction(JsonNode node) throws Exception {
    if (chain == null) {
        chain = "default";
    }/*from  ww  w .j a va 2  s . c  om*/

    if (node.has("chains")) {
        JsonNode chains = node.get("chains");
        if (chains.isArray()) {
            ArrayNode chainsArray = (ArrayNode) chains;
            int limit = chainsArray.size();
            JsonNode selectedChain = null;
            for (int i = 0; i < limit && selectedChain == null; i++) {
                JsonNode chainNode = chainsArray.get(i);
                if (chainNode.has("name")) {
                    if (chainNode.get("name").asText().equals(chain)) {
                        selectedChain = chainNode;
                    }
                }
            }
            if (selectedChain != null) {
                if (setToReader) {
                    JsonNode reader = null;
                    if (selectedChain.has("reader")) {
                        reader = selectedChain.get("reader");
                    }
                    if (reader != null) {
                        removesIncludesOrExcludesList((ObjectNode) reader);
                    }
                }
                if (setToWriter) {
                    JsonNode writer = null;
                    if (selectedChain.has("writer")) {
                        writer = selectedChain.get("writer");
                    }
                    if (writer != null) {
                        removesIncludesOrExcludesList((ObjectNode) writer);
                    }
                }
            }
        }
        provider.write(node);
    }
}

From source file:org.apache.solr.kelvin.scorer.EventCollectorScorer.java

@Override
public void configure(JsonNode config) throws Exception {
    if (!config.path("excludes").isMissingNode()) {
        ArrayNode excludes = ConfigurableLoader.assureArray(config.path("excludes"));
        for (int i = 0; i < excludes.size(); i++)
            excludeList.add(excludes.get(i).asText());
    } else {//w  ww  .ja  v  a 2s.c o m
        //defaults
        excludeList.add("org.apache.solr.kelvin.events.ResponseDecodedTestEvent");
        excludeList.add("org.apache.solr.kelvin.events.TestCaseTestEvent");
        //excludeList.add("org.apache.solr.kelvin.events.MissingFieldTestEvent");
        //excludeList.add("org.apache.solr.kelvin.events.MissingResultTestEvent");
    }
}

From source file:com.unboundid.scim2.server.utils.ResourceTrimmer.java

/**
 * Trim attributes of an inner object node to return.
 *
 * @param objectNode The object node to return.
 * @param parentPath  The parent path of attributes in the object.
 * @return The trimmed object node ready to return to the client.
 *//*from w w  w .  jav  a2  s.c om*/
private ObjectNode trimObjectNode(final ObjectNode objectNode, final Path parentPath) {
    ObjectNode objectToReturn = JsonUtils.getJsonNodeFactory().objectNode();
    Iterator<Map.Entry<String, JsonNode>> i = objectNode.fields();
    while (i.hasNext()) {
        Map.Entry<String, JsonNode> field = i.next();
        final Path path;
        if (parentPath.isRoot() && parentPath.getSchemaUrn() == null && SchemaUtils.isUrn(field.getKey())) {
            path = Path.root(field.getKey());
        } else {
            path = parentPath.attribute(field.getKey());
        }

        if (path.isRoot() || shouldReturn(path)) {
            if (field.getValue().isArray()) {
                ArrayNode trimmedNode = trimArrayNode((ArrayNode) field.getValue(), path);
                if (trimmedNode.size() > 0) {
                    objectToReturn.set(field.getKey(), trimmedNode);
                }
            } else if (field.getValue().isObject()) {
                ObjectNode trimmedNode = trimObjectNode((ObjectNode) field.getValue(), path);
                if (trimmedNode.size() > 0) {
                    objectToReturn.set(field.getKey(), trimmedNode);
                }
            } else {
                objectToReturn.set(field.getKey(), field.getValue());
            }
        }
    }
    return objectToReturn;
}

From source file:la.alsocan.jsonshapeshifter.transformations.AdvancedCollectionTransformationTest.java

@Test
public void embeddedCollectionBindingShouldProduceTheRightNumberOfElements() throws IOException {

    Schema source = Schema.buildSchema(new ObjectMapper().readTree(DataSet.EMBEDDED_COLLECTION_SCHEMA));
    Schema target = Schema.buildSchema(new ObjectMapper().readTree(DataSet.EMBEDDED_COLLECTION_SCHEMA));
    Transformation t = new Transformation(source, target);

    Iterator<SchemaNode> it = t.toBind();
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someArrayOfArray")));
    t.bind(it.next(), new ArrayNodeBinding((SchemaArrayNode) source.at("/someArrayOfArray/{i}")));
    t.bind(it.next(), new StringNodeBinding(source.at("/someArrayOfArray/{i}/{i}")));

    JsonNode payload = new ObjectMapper().readTree(DataSet.EMBEDDED_COLLECTION_PAYLOAD);
    JsonNode result = t.apply(payload);/*from  w w  w. j  av  a 2 s  .c om*/

    ArrayNode arrayCollection = (ArrayNode) result.at("/someArrayOfArray");
    assertThat(arrayCollection.size(), is(3));

    arrayCollection = (ArrayNode) result.at("/someArrayOfArray/0");
    assertThat(arrayCollection.size(), is(3));

    arrayCollection = (ArrayNode) result.at("/someArrayOfArray/1");
    assertThat(arrayCollection.size(), is(2));

    arrayCollection = (ArrayNode) result.at("/someArrayOfArray/2");
    assertThat(arrayCollection.size(), is(1));
}

From source file:org.flowable.rest.dmn.service.api.decision.DmnRuleServiceResourceTest.java

@DmnDeploymentAnnotation(resources = { "org/flowable/rest/dmn/service/api/decision/multi-hit.dmn" })
public void testExecutionDecision() throws Exception {
    // Add decision key
    ObjectNode requestNode = objectMapper.createObjectNode();
    requestNode.put("decisionKey", "decision1");

    // add input variable
    ArrayNode variablesNode = objectMapper.createArrayNode();
    ObjectNode variableNode = objectMapper.createObjectNode();
    variableNode.put("name", "inputVariable1");
    variableNode.put("type", "integer");
    variableNode.put("value", 5);
    variablesNode.add(variableNode);//ww  w.  jav a  2 s .  c o m

    requestNode.set("inputVariables", variablesNode);

    HttpPost httpPost = new HttpPost(
            SERVER_URL_PREFIX + DmnRestUrls.createRelativeResourceUrl(DmnRestUrls.URL_RULE_SERVICE_EXECUTE));
    httpPost.setEntity(new StringEntity(requestNode.toString()));
    CloseableHttpResponse response = executeRequest(httpPost, HttpStatus.SC_CREATED);

    // Check response
    JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
    closeResponse(response);

    ArrayNode resultVariables = (ArrayNode) responseNode.get("resultVariables");

    assertEquals(3, resultVariables.size());
}

From source file:io.wcm.caravan.pipeline.impl.JsonPathSelectorTests.java

@Test
public void testExtractArrayPathNoResults() {

    // if no selection matches the expression, but the property in the query exists, no results are returned
    ArrayNode result = new JsonPathSelector("$..book[?(@.title=='No such title')]").call(booksJson);

    assertEquals(0, result.size());
}