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.easarrive.aws.client.cloudsearch.model.Hit.java

public List<String> getListField(String field) {
    List<String> rlt = null;

    if (fields.containsKey(field)) {
        Object value = fields.get(field);
        if (value instanceof String) {
            JsonNode jsonNode = Jackson.jsonNodeOf((String) value);
            if (jsonNode.isArray()) {
                rlt = new ArrayList<String>();
                for (int index = 0; index < jsonNode.size(); index++) {
                    rlt.add(jsonNode.get(index).asText());
                }/*from  w w w .  j  a  v a  2s  . c o m*/
            }
        }
    }

    return rlt;
}

From source file:com.redhat.lightblue.eval.UpdaterTest.java

@Test
public void refSet() throws Exception {
    UpdateExpression expr = EvalTestContext.updateExpressionFromJson(
            "{'$set' : { 'field6.nf5.0': { '$valueof' : 'field3' }, 'field7.0' : {}}}");

    Updater updater = Updater.getInstance(JSON_NODE_FACTORY, md, expr);
    Assert.assertTrue(updater.update(jsonDoc, md.getFieldTreeRoot(), new Path()));
    Assert.assertEquals(jsonDoc.get(new Path("field3")).intValue(),
            jsonDoc.get(new Path("field6.nf5.0")).intValue());
    JsonNode node = jsonDoc.get(new Path("field7.0"));
    Assert.assertNotNull(node);/* w w  w.ja  va 2 s. c  o m*/
    Assert.assertEquals(0, node.size());
    Assert.assertTrue(node instanceof ObjectNode);
}

From source file:org.n52.iceland.config.json.JsonActivationDao.java

@Override
public Set<BindingKey> getBindingKeys() {
    readLock().lock();/*w ww .ja v a  2 s.  c om*/
    try {
        JsonNode node = getConfiguration().path(JsonConstants.ACTIVATION).path(JsonConstants.BINDINGS);
        Set<BindingKey> keys = new HashSet<>(node.size());
        node.path(JsonConstants.BY_PATH).fieldNames().forEachRemaining(k -> keys.add(new PathBindingKey(k)));
        node.path(JsonConstants.BY_MEDIA_TYPE).fieldNames()
                .forEachRemaining(k -> keys.add(new MediaTypeBindingKey(MediaType.parse(k))));
        return keys;
    } finally {
        readLock().unlock();
    }
}

From source file:org.activiti.rest.service.api.history.HistoricVariableInstanceCollectionResourceTest.java

protected void assertResultsPresentInDataResponse(String url, int numberOfResultsExpected, String variableName,
        Object variableValue) throws JsonProcessingException, IOException {

    // Do the actual call
    CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + url), HttpStatus.SC_OK);

    // Check status and size
    JsonNode dataNode = objectMapper.readTree(response.getEntity().getContent()).get("data");
    closeResponse(response);//  w  w w . j  a v  a 2s .c om
    assertEquals(numberOfResultsExpected, dataNode.size());

    // Check presence of ID's
    if (variableName != null) {
        boolean variableFound = false;
        Iterator<JsonNode> it = dataNode.iterator();
        while (it.hasNext()) {
            JsonNode dataElementNode = it.next();
            JsonNode variableNode = dataElementNode.get("variable");
            String name = variableNode.get("name").textValue();
            if (variableName.equals(name)) {
                variableFound = true;
                if (variableValue instanceof Boolean) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asBoolean() == (Boolean) variableValue);
                } else if (variableValue instanceof Integer) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asInt() == (Integer) variableValue);
                } else {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asText().equals((String) variableValue));
                }
            }
        }
        assertTrue("Variable " + variableName + " is missing", variableFound);
    }
}

From source file:org.flowable.rest.service.api.history.HistoricVariableInstanceCollectionResourceTest.java

protected void assertResultsPresentInDataResponse(String url, int numberOfResultsExpected, String variableName,
        Object variableValue) throws JsonProcessingException, IOException {

    // Do the actual call
    CloseableHttpResponse response = executeRequest(new HttpGet(SERVER_URL_PREFIX + url), HttpStatus.SC_OK);

    // Check status and size
    JsonNode dataNode = objectMapper.readTree(response.getEntity().getContent()).get("data");
    closeResponse(response);/*w w  w.ja v  a 2 s  .c  om*/
    assertEquals(numberOfResultsExpected, dataNode.size());

    // Check presence of ID's
    if (variableName != null) {
        boolean variableFound = false;
        Iterator<JsonNode> it = dataNode.iterator();
        while (it.hasNext()) {
            JsonNode dataElementNode = it.next();
            JsonNode variableNode = dataElementNode.get("variable");
            String name = variableNode.get("name").textValue();
            if (variableName.equals(name)) {
                variableFound = true;
                if (variableValue instanceof Boolean) {
                    assertTrue("Variable value is not equal",
                            variableNode.get("value").asBoolean() == (Boolean) variableValue);
                } else if (variableValue instanceof Integer) {
                    assertEquals("Variable value is not equal", variableNode.get("value").asInt(),
                            (int) (Integer) variableValue);
                } else {
                    assertEquals("Variable value is not equal", variableNode.get("value").asText(),
                            (String) variableValue);
                }
            }
        }
        assertTrue("Variable " + variableName + " is missing", variableFound);
    }
}

From source file:com.infinities.keystone4j.admin.v3.group.GroupResourceTest.java

@Test
public void testListUsersInGroup() throws JsonProcessingException, IOException {
    Response response = target("/v3/groups").path(group.getId()).path("users").register(JacksonFeature.class)
            .register(ObjectMapperResolver.class).request()
            .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()).get();
    assertEquals(200, response.getStatus());
    JsonNode node = JsonUtils.convertToJsonNode(response.readEntity(String.class));
    JsonNode usersJ = node.get("users");
    assertEquals(1, usersJ.size());
    JsonNode userJ = usersJ.get(0);/*from   w  ww  .  ja  v a  2  s .c om*/
    assertEquals(user2.getId(), userJ.get("id").asText());
    assertEquals(user2.getName(), userJ.get("name").asText());
    assertEquals(user2.getDescription(), userJ.get("description").asText());
    assertEquals(user2.getDomain().getId(), userJ.get("domain_id").asText());
    assertEquals(user2.getDefaultProjectId(), userJ.get("default_project_id").asText());
    assertNull(userJ.get("password"));
}

From source file:com.crushpaper.DbJsonBackupForUserTest.java

@Test
public void test3() throws IOException {
    final TestEntrySet before = new TestEntrySet(
            new TestEntry[] { new TestEntry("1", 1), new TestEntry("2", 2) });

    final ObjectMapper mapper = new ObjectMapper();
    final JsonNode node1 = mapper.readTree("{\n" + "\"note\": \"1\",\n" + "\"modTime\": 1,\n"
            + "\"createTime\": 1,\n" + "\"id\": \"S3\",\n" + "\"type\": \"root\"\n" + "}");

    final JsonNode node2 = mapper.readTree("{\n" + "\"note\": \"2\",\n" + "\"modTime\": 2,\n"
            + "\"createTime\": 2,\n" + "\"id\": \"S4\",\n" + "\"type\": \"root\"\n" + "}\n");
    final Errors errors = new Errors();
    try {/*from   w w w.ja  v  a 2 s  . c o  m*/
        final User user = dbLogic.getOrCreateUser("user");
        assertTrue(dbLogic.addEntries(before, user, createTime, errors));
        dbLogic.commit();
        final StringBuilder result = new StringBuilder();
        dbLogic.backupJsonForUser(user, result);
        final JsonNode resultNode = mapper.readTree(result.toString());
        assertTrue(resultNode.isObject());
        final JsonNode entriesNodes = resultNode.get("entries");
        assertTrue(entriesNodes.isArray());
        assertEquals(2, entriesNodes.size());
        boolean matched1 = false, matched2 = false;
        for (int i = 0; i < 2; ++i) {
            final JsonNode obj = entriesNodes.get(i);
            if (obj.equals(node1)) {
                matched1 = true;
            } else if (obj.equals(node2)) {
                matched2 = true;
            }
        }

        assertTrue(matched1);
        assertTrue(matched2);
    } catch (final IOException e) {
        assertTrue(false);
    }
}

From source file:org.jboss.aerogear.sync.diffmatchpatch.JsonMapperTest.java

@Test
public void serializeEdit() {
    final String documentId = "1234";
    final String clientId = "client1";
    final PatchMessage<DiffMatchPatchEdit> patchMessage = patchMessage(documentId, clientId,
            DiffMatchPatchEdit.withChecksum("bogus")
                    .diff(new DiffMatchPatchDiff(Operation.UNCHANGED, "version"))
                    .diff(new DiffMatchPatchDiff(Operation.DELETE, "1"))
                    .diff(new DiffMatchPatchDiff(Operation.ADD, "2")).build());
    final String json = JsonMapper.toJson(patchMessage.edits().peek());
    final JsonNode edit = JsonMapper.asJsonNode(json);
    assertThat(edit.get("serverVersion").asText(), equalTo("0"));
    assertThat(edit.get("clientVersion").asText(), equalTo("0"));
    final JsonNode diffs = edit.get("diffs");
    assertThat(diffs.isArray(), is(true));
    assertThat(diffs.size(), is(3));
}

From source file:org.jsonschema2pojo.SchemaGenerator.java

private ObjectNode arraySchema(JsonNode exampleArray) {
    ObjectNode schema = OBJECT_MAPPER.createObjectNode();

    schema.put("type", "array");

    if (exampleArray.size() > 0) {

        JsonNode exampleItem = exampleArray.get(0).isObject() ? mergeArrayItems(exampleArray)
                : exampleArray.get(0);//ww  w .j ava  2 s  .  c o m

        schema.set("items", schemaFromExample(exampleItem));
    }

    return schema;
}

From source file:com.metamx.common.parsers.JSONParser.java

@Override
public Map<String, Object> parse(String input) {
    try {//from  ww  w  . ja  v a 2 s .  c om
        Map<String, Object> map = new LinkedHashMap<>();
        JsonNode root = objectMapper.readTree(input);

        Iterator<String> keysIter = (fieldNames == null ? root.fieldNames() : fieldNames.iterator());

        while (keysIter.hasNext()) {
            String key = keysIter.next();

            if (exclude.contains(key)) {
                continue;
            }

            JsonNode node = root.path(key);

            if (node.isArray()) {
                final List<Object> nodeValue = Lists.newArrayListWithExpectedSize(node.size());
                for (final JsonNode subnode : node) {
                    final Object subnodeValue = valueFunction.apply(subnode);
                    if (subnodeValue != null) {
                        nodeValue.add(subnodeValue);
                    }
                }
                map.put(key, nodeValue);
            } else {
                final Object nodeValue = valueFunction.apply(node);
                if (nodeValue != null) {
                    map.put(key, nodeValue);
                }
            }
        }
        return map;
    } catch (Exception e) {
        throw new ParseException(e, "Unable to parse row [%s]", input);
    }
}