List of usage examples for com.fasterxml.jackson.databind JsonNode size
public int size()
From source file:com.infinities.keystone4j.admin.v3.role.RoleV3ResourceTest.java
@Test public void testListRoles() throws JsonProcessingException, IOException { Response response = target("/v3/roles").register(JacksonFeature.class).register(ObjectMapperResolver.class) .request()// www . j ava2 s . c o m .header("X-Auth-Token", Config.Instance.getOpt(Config.Type.DEFAULT, "admin_token").asText()).get(); assertEquals(200, response.getStatus()); String ret = response.readEntity(String.class); System.err.println(ret); JsonNode node = JsonUtils.convertToJsonNode(ret); JsonNode rolesJ = node.get("roles"); assertEquals(2, rolesJ.size()); JsonNode roleJ = rolesJ.get(0); assertEquals(role1.getId(), roleJ.get("id").asText()); assertEquals(role1.getName(), roleJ.get("name").asText()); assertEquals(role1.getDescription(), roleJ.get("description").asText()); }
From source file:com.yahoo.elide.tests.BookAuthorIT.java
@Test public void testSparseTwoDataFieldValuesNoIncludes() throws Exception { JsonNode responseBody = objectMapper.readTree(RestAssured.given().contentType(JSONAPI_CONTENT_TYPE) .accept(JSONAPI_CONTENT_TYPE).param("fields[book]", "title,language").get("/book").asString()); Assert.assertTrue(responseBody.has("data")); for (JsonNode bookNode : responseBody.get("data")) { Assert.assertTrue(bookNode.has(ATTRIBUTES)); Assert.assertFalse(bookNode.has(RELATIONSHIPS)); JsonNode attributes = bookNode.get(ATTRIBUTES); Assert.assertEquals(attributes.size(), 2); Assert.assertTrue(attributes.has("title")); Assert.assertTrue(attributes.has("language")); }/* www .j a va 2 s . c om*/ Assert.assertFalse(responseBody.has(INCLUDED)); }
From source file:com.github.javaplugs.mybatis.JsonNodeValue.java
/** * Return true if value is not present or if underlying JSON is empty object, array or null. * WARNING this method can throw same exceptions as {@link JsonNodeValue#get()} in a case if * source is invalid JSON string./* w ww . j a v a 2 s . co m*/ */ public boolean isEmpty() { if (!isPresent()) { return true; } JsonNode n = get(); if ((n.isObject() || n.isArray()) && n.size() == 0) { return true; } if (n.isNull()) { return true; } return false; }
From source file:com.redhat.lightblue.config.CrudConfiguration.java
@Override public void initializeFromJson(JsonNode node) { if (node != null) { JsonNode x = node.get("controllers"); if (x instanceof ArrayNode) { List<ControllerConfiguration> list = new ArrayList<>(x.size()); for (Iterator<JsonNode> itr = ((ArrayNode) x).elements(); itr.hasNext();) { JsonNode controllerNode = itr.next(); ControllerConfiguration controller = new ControllerConfiguration(); controller.initializeFromJson(controllerNode); list.add(controller);//from w w w. j ava 2 s .c om } controllers = list.toArray(new ControllerConfiguration[list.size()]); } else { throw new IllegalArgumentException( "'controllers' must be instanceof ArrayNode: " + node.toString()); } x = node.get("validateRequests"); if (x != null) validateRequests = x.booleanValue(); } }
From source file:com.yahoo.elide.tests.BookAuthorIT.java
@Test public void testSparseSingleDataFieldValue() throws Exception { JsonNode responseBody = objectMapper .readTree(RestAssured.given().contentType(JSONAPI_CONTENT_TYPE).accept(JSONAPI_CONTENT_TYPE) .param("include", "authors").param("fields[book]", "title").get("/book").asString()); Assert.assertTrue(responseBody.has("data")); for (JsonNode bookNode : responseBody.get("data")) { Assert.assertTrue(bookNode.has(ATTRIBUTES)); Assert.assertFalse(bookNode.has(RELATIONSHIPS)); JsonNode attributes = bookNode.get(ATTRIBUTES); Assert.assertEquals(attributes.size(), 1); Assert.assertTrue(attributes.has("title")); }/* w w w .ja v a2 s . com*/ Assert.assertTrue(responseBody.has(INCLUDED)); for (JsonNode include : responseBody.get(INCLUDED)) { Assert.assertFalse(include.has(ATTRIBUTES)); Assert.assertFalse(include.has(RELATIONSHIPS)); } }
From source file:edu.berkeley.ground.postgres.dao.version.PostgresItemDao.java
protected T retrieve(String sql, Object field) throws GroundException { JsonNode json = Json.parse(PostgresUtils.executeQueryToJson(dbSource, sql)); if (json.size() == 0) { throw new GroundException(ExceptionType.ITEM_NOT_FOUND, this.getType().getSimpleName(), field.toString());// w ww . j a va 2 s . c om } Class<T> type = this.getType(); JsonNode itemJson = json.get(0); long id = itemJson.get("itemId").asLong(); String name = itemJson.get("name").asText(); String sourceKey = itemJson.get("sourceKey").asText(); Object[] args = { id, name, sourceKey, this.postgresTagDao.retrieveFromDatabaseByItemId(id) }; Constructor<T> constructor; try { constructor = type.getConstructor(long.class, String.class, String.class, Map.class); return constructor.newInstance(args); } catch (Exception e) { throw new GroundException(ExceptionType.OTHER, String.format("Catastrophic failure: Unable to instantiate Item.\n%s: %s.", e.getClass().getName(), e.getMessage())); } }
From source file:com.irccloud.android.data.ChannelsDataSource.java
public synchronized void updateMode(int bid, String mode, JsonNode ops, boolean init) { Channel c = getChannelForBuffer(bid); if (c != null) { c.key = false;// ww w .ja v a 2 s . co m JsonNode add = ops.get("add"); for (int i = 0; i < add.size(); i++) { JsonNode m = add.get(i); c.addMode(m.get("mode").asText(), m.get("param").asText(), init); } JsonNode remove = ops.get("remove"); for (int i = 0; i < remove.size(); i++) { JsonNode m = remove.get(i); c.removeMode(m.get("mode").asText()); } c.mode = mode; } }
From source file:com.turn.shapeshifter.AutoSerializerTest.java
@Test public void testSerializeWithRepeatedPrimitive() throws Exception { Actor actor = Actor.newBuilder().setName("James Dean").addQuotes("Foo").build(); JsonNode result = new AutoSerializer(Actor.getDescriptor()).serialize(actor, ReadableSchemaRegistry.EMPTY); Assert.assertTrue(result.isObject()); Assert.assertEquals(JsonToken.VALUE_STRING, result.get("name").asToken()); Assert.assertEquals("James Dean", result.get("name").asText()); JsonNode array = result.get("quotes"); Assert.assertTrue(array.isArray());/*from www. jav a 2s.com*/ Assert.assertEquals(1, array.size()); JsonNode quoteNode = array.get(0); Assert.assertEquals(JsonToken.VALUE_STRING, quoteNode.asToken()); Assert.assertEquals("Foo", quoteNode.asText()); }
From source file:com.googlecode.jsonschema2pojo.integration.EnumIT.java
@Test @SuppressWarnings("unchecked") public void jacksonCanMarshalEnums() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException { Object valueWithEnumProperty = parentClass.newInstance(); Method enumSetter = parentClass.getMethod("setEnumProperty", enumClass); enumSetter.invoke(valueWithEnumProperty, enumClass.getEnumConstants()[2]); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(valueWithEnumProperty); JsonNode jsonTree = objectMapper.readTree(jsonString); assertThat(jsonTree.size(), is(1)); assertThat(jsonTree.has("enum_Property"), is(true)); assertThat(jsonTree.get("enum_Property").isTextual(), is(true)); assertThat(jsonTree.get("enum_Property").asText(), is("3rd one")); }
From source file:org.lendingclub.mercator.newrelic.NewRelicClientImpl.java
@Override public ObjectNode getAlertConditionForPolicy(String policyId) { ObjectNode alertConditionsNode = mapper.createObjectNode(); List<JsonNode> alertConditionsList = new ArrayList<JsonNode>(); int page = 1; JsonNode conditions = getAlertConditionByPolicy(policyId, page); while (conditions.size() != 0) { List<JsonNode> tempPoliciesList = convertJsonNodeToList(conditions); alertConditionsList.addAll(tempPoliciesList); page++;/* w ww .j a v a 2s . c o m*/ conditions = getAlertConditionByPolicy(policyId, page); } alertConditionsNode.put("conditions", mapper.valueToTree(alertConditionsList)); return alertConditionsNode; }