List of usage examples for com.fasterxml.jackson.databind JsonNode size
public int size()
From source file:com.basho.riak.client.itest.ITestORM.java
@Test public void storeDomainObjectWithUserMeta() throws Exception { final String userId = UUID.randomUUID().toString(); final String email = "customer@megacorp.com"; final String languageCode = "en"; final String favColor = "fav-color"; final String blue = "blue"; final String username = "userX"; final Bucket users = client.createBucket(bucketName).execute(); final Customer c1 = new Customer(userId); c1.setUsername(username);/*from ww w . j av a2 s .c o m*/ c1.setEmailAddress(email); c1.setLanguageCode(languageCode); c1.addPreference(favColor, blue); users.store(c1).execute(); // fetch it as an IRiakObject and check the meta data IRiakObject iro = users.fetch(userId).execute(); Map<String, String> meta = iro.getMeta(); // check that meta values haven't leaked into the JSON JsonNode value = new ObjectMapper().readTree(iro.getValueAsString()); assertEquals(3, value.size()); assertEquals(username, value.get("username").textValue()); assertEquals(email, value.get("emailAddress").textValue()); assertEquals(null, value.get("shoeSize").textValue()); assertEquals(2, meta.size()); assertEquals(languageCode, meta.get("language-pref")); assertEquals(blue, meta.get(favColor)); // fetch it as a Customer and check the meta data Customer actual = users.fetch(userId, Customer.class).execute(); Map<String, String> prefs = actual.getPreferences(); assertEquals(languageCode, actual.getLanguageCode()); assertEquals(1, prefs.size()); assertEquals(blue, prefs.get(favColor)); }
From source file:org.dswarm.graph.json.deserializer.ModelDeserializer.java
@Override public Model deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { final ObjectCodec oc = jp.getCodec(); if (oc == null) { return null; }/*from w w w . j ava 2s . c o m*/ final JsonNode node = oc.readTree(jp); if (node == null) { return null; } if (!ArrayNode.class.isInstance(node)) { throw new JsonParseException("expected a JSON array full of resource objects of the model", jp.getCurrentLocation()); } if (node.size() <= 0) { return null; } final Model model = new Model(); for (final JsonNode resourceNode : node) { final Resource resource = resourceNode.traverse(oc).readValueAs(Resource.class); model.addResource(resource); } return model; }
From source file:io.mesosphere.mesos.frameworks.cassandra.scheduler.api.NodeControllerTest.java
@Test public void testSeedNodes() throws Exception { threeNodeCluster();/*from w w w. ja v a 2s. c o m*/ Tuple2<Integer, JsonNode> tup = getJson("/node/seed/all"); assertThat(tup._1.intValue()).isEqualTo(200); JsonNode json = tup._2; assertThat(json.get("nativePort").asInt()).isEqualTo(9042); assertThat(json.get("rpcPort").asInt()).isEqualTo(9160); JsonNode seeds = json.get("seeds"); assertThat(seeds.isArray()); assertThat(seeds.size()).isEqualTo(2); assertThat(seeds.get(0).asText()).isEqualTo(slaves[0]._2); assertThat(seeds.get(1).asText()).isEqualTo(slaves[1]._2); // make node 2 non-seed tup = postJson(String.format("/node/%s/make-non-seed", slaves[1]._2)); assertThat(tup._1.intValue()).isEqualTo(200); json = tup._2; assertThat(json.get("ip").asText()).isEqualTo(slaves[1]._2); assertThat(json.has("hostname")).isTrue(); assertThat(json.get("oldSeedState").asBoolean()).isTrue(); assertThat(json.get("success").asBoolean()).isTrue(); assertThat(json.get("seedState").asBoolean()).isFalse(); assertThat(json.has("error")).isFalse(); // verify tup = getJson("/node/seed/all"); assertThat(tup._1.intValue()).isEqualTo(200); json = tup._2; seeds = json.get("seeds"); assertThat(seeds.isArray()); assertThat(seeds.size()).isEqualTo(1); assertThat(seeds.get(0).asText()).isEqualTo(slaves[0]._2); // make node 2 non-seed again tup = postJson(String.format("/node/%s/make-non-seed", slaves[1]._2)); assertThat(tup._1.intValue()).isEqualTo(200); json = tup._2; assertThat(json.get("ip").asText()).isEqualTo(slaves[1]._2); assertThat(json.has("hostname")).isTrue(); assertThat(json.get("oldSeedState").asBoolean()).isFalse(); assertThat(json.get("success").asBoolean()).isFalse(); assertThat(json.get("seedState").asBoolean()).isFalse(); assertThat(json.has("error")).isFalse(); // non-existing tup = postJson("/node/foobar/make-non-seed"); assertThat(tup._1.intValue()).isEqualTo(404); // too few seeds tup = postJson(String.format("/node/%s/make-non-seed", slaves[0]._2)); assertThat(tup._1.intValue()).isEqualTo(400); json = tup._2; assertThat(json.get("ip").asText()).isEqualTo(slaves[0]._2); assertThat(json.has("hostname")).isTrue(); assertThat(json.get("oldSeedState").asBoolean()).isTrue(); assertThat(json.get("success").asBoolean()).isFalse(); assertThat(json.has("error")).isTrue(); assertThat(json.get("error").isTextual()).isTrue(); }
From source file:com.github.fge.jsonschema.keyword.validator.draftv3.ExtendsValidator.java
@Override public void validate(final Processor<FullData, FullData> processor, final ProcessingReport report, final MessageBundle bundle, final FullData data) throws ProcessingException { final SchemaTree tree = data.getSchema(); final JsonNode node = tree.getNode().get(keyword); FullData newData;/*from w ww . j a v a2s. c o m*/ if (node.isObject()) { newData = data.withSchema(tree.append(JsonPointer.of(keyword))); processor.process(report, newData); return; } /* * Not an object? An array */ final int size = node.size(); JsonPointer pointer; for (int index = 0; index < size; index++) { pointer = JsonPointer.of(keyword, index); newData = data.withSchema(tree.append(pointer)); processor.process(report, newData); } }
From source file:com.turn.shapeshifter.NamedSchemaSerializerTest.java
@Test public void testSerializeWithRepeatedPrimitive() throws Exception { NamedSchema schema = NamedSchema.of(Union.getDescriptor(), "Union"); SchemaRegistry registry = new SchemaRegistry(); registry.register(schema);// w ww .j a v a 2 s . co m Union union = Union.newBuilder().addInt32Repeated(42).addInt32Repeated(42).build(); JsonNode result = new NamedSchemaSerializer(schema).serialize(union, registry); JsonNode array = result.get("int32Repeated"); Assert.assertTrue(array.isArray()); Assert.assertEquals(2, array.size()); for (JsonNode item : array) { Assert.assertEquals(JsonToken.VALUE_NUMBER_INT, item.asToken()); Assert.assertEquals(42, item.intValue()); } }
From source file:com.github.mrstampy.gameboot.otp.websocket.WebSocketEndpoint.java
private void unencrypted(Session ctx, byte[] msg) throws Exception { Response r = getResponse(msg); lastResponse = r;/*from w w w . java2s . c om*/ log.info("Unencrypted: on session {}\n{}", ctx.getId(), mapper.writeValueAsString(r)); if (!ok(r.getResponseCode())) return; if (ResponseCode.INFO == r.getResponseCode()) { Object[] payload = r.getPayload(); if (payload == null || payload.length == 0 || !(payload[0] instanceof Map<?, ?>)) { throw new IllegalStateException("Expecting map of systemId:[value]"); } systemId = (Long) ((Map<?, ?>) payload[0]).get("systemId"); log.info("Setting system id {}", systemId); this.session = ctx; return; } JsonNode node = mapper.readTree(msg); JsonNode response = node.get("payload"); boolean hasKey = response != null && response.isArray() && response.size() == 1; if (hasKey) { log.info("Setting key"); otpKey = response.get(0).binaryValue(); return; } switch (r.getType()) { case OtpKeyRequest.TYPE: log.info("Deleting key"); otpKey = null; break; default: break; } }
From source file:com.turn.shapeshifter.NamedSchemaSerializerTest.java
@Test public void testSerializeWithRepeatedObject() throws Exception { NamedSchema schema = NamedSchema.of(Union.getDescriptor(), "Union").useSchema("union_repeated", "Union"); SchemaRegistry registry = new SchemaRegistry(); registry.register(schema);// w ww .j a v a2s . co m Union subUnion = Union.newBuilder().setInt32Value(42).build(); Union union = Union.newBuilder().addUnionRepeated(subUnion).addUnionRepeated(subUnion).build(); JsonNode result = new NamedSchemaSerializer(schema).serialize(union, registry); JsonNode array = result.get("unionRepeated"); Assert.assertTrue(array.isArray()); Assert.assertEquals(2, array.size()); for (JsonNode item : array) { Assert.assertTrue(item.isObject()); Assert.assertEquals(42, item.get("int32Value").intValue()); } }