Refine search
private void addToHA(String deploymentID, String verticleName, DeploymentOptions deploymentOptions) { String encoded; synchronized (haInfo) { JsonObject verticleConf = new JsonObject().put("dep_id", deploymentID); verticleConf.put("verticle_name", verticleName); verticleConf.put("options", deploymentOptions.toJson()); JsonArray haMods = haInfo.getJsonArray("verticles"); haMods.add(verticleConf); encoded = haInfo.encode(); clusterMap.put(nodeID, encoded); } }
private void now(RoutingContext context) { logger.info("Time request from {}", context.request().remoteAddress()); JsonObject data = new JsonObject() .put("powered-by", "vertx") .put("current-time", System.currentTimeMillis()); context.response() .putHeader("Content-Type", "application/json") .end(data.encode()); } }
@Override public void start() throws Exception { vertx.createHttpServer().requestHandler(req -> { req.response() .putHeader("Content/type", "application/json") .end(new JsonObject() .put("firstName", "Dale") .put("lastName", "Cooper") .put("male", true) .encode() ); }).listen(8080, listenResult -> { if (listenResult.failed()) { System.out.println("Could not start HTTP server"); listenResult.cause().printStackTrace(); } else { System.out.println("Server started"); } }); } }
@Override public void start() throws Exception { vertx.createHttpServer().requestHandler(req -> { req.response() .putHeader("Content/type", "application/json") .end(new JsonObject() .put("firstName", "Dale") .put("lastName", "Cooper") .put("male", true) .encode() ); }).listen(8080, listenResult -> { if (listenResult.failed()) { System.out.println("Could not start HTTP server"); listenResult.cause().printStackTrace(); } else { System.out.println("Server started"); } }); } }
private void loadData(MongoClient db) { db.dropCollection("users", drop -> { if (drop.failed()) { throw new RuntimeException(drop.cause()); } List<JsonObject> users = new LinkedList<>(); users.add(new JsonObject() .put("username", "pmlopes") .put("firstName", "Paulo") .put("lastName", "Lopes") .put("address", "The Netherlands")); users.add(new JsonObject() .put("username", "timfox") .put("firstName", "Tim") .put("lastName", "Fox") .put("address", "The Moon")); for (JsonObject user : users) { db.insert("users", user, res -> { System.out.println("inserted " + user.encode()); }); } }); } }
albums.add(new JsonObject() .put("artist", "The Wurzels") .put("genre", "Scrumpy and Western") .put("price", 0.99)); albums.add(new JsonObject() .put("artist", "Vanilla Ice") .put("genre", "Hip Hop") .put("price", 0.01)); albums.add(new JsonObject() .put("artist", "Ena Baga") .put("genre", "Easy Listening") System.out.println("inserted " + album.encode()); });
@Test public void testCreateFromBuffer() { JsonObject excepted = new JsonObject(); excepted.put("foo", "bar"); excepted.put("quux", 123); Buffer buf = Buffer.buffer(excepted.encode()); assertEquals(excepted, new JsonObject(buf)); }
@Override public void encodeToWire(Buffer buffer, CustomMessage customMessage) { // Easiest ways is using JSON object JsonObject jsonToEncode = new JsonObject(); jsonToEncode.put("statusCode", customMessage.getStatusCode()); jsonToEncode.put("resultCode", customMessage.getResultCode()); jsonToEncode.put("summary", customMessage.getSummary()); // Encode object to string String jsonToStr = jsonToEncode.encode(); // Length of JSON: is NOT characters count int length = jsonToStr.getBytes().length; // Write data into given buffer buffer.appendInt(length); buffer.appendString(jsonToStr); }
protected Process startExternalNode(int id) throws Exception { String javaHome = System.getProperty("java.home"); String classpath = System.getProperty("java.class.path"); List<String> command = new ArrayList<>(); command.add(javaHome + File.separator + "bin" + File.separator + "java"); command.add("-classpath"); command.add(classpath); command.addAll(getExternalNodeSystemProperties()); command.add(Launcher.class.getName()); command.add("run"); command.add(FaultToleranceVerticle.class.getName()); command.add("-cluster"); command.add("-conf"); command.add(new JsonObject().put("id", id).put("addressesCount", ADDRESSES_COUNT).encode()); return new ProcessBuilder(command).inheritIO().start(); }
vertx.eventBus().<JsonObject>send("backend", new JsonObject() .put("op", "get") .put("id", productID), msg -> { JsonObject json = msg.result().body(); if (json != null) { asyncResponse.resume(json.encode()); } else { asyncResponse.resume(Response.status(Response.Status.NOT_FOUND).build());
@Test public void testRunVerticleWithConfFile() throws Exception { Path tempDir = testFolder.newFolder().toPath(); Path tempFile = Files.createTempFile(tempDir, "conf", "json"); MyLauncher launcher = new MyLauncher(); JsonObject conf = new JsonObject().put("foo", "bar").put("wibble", 123); Files.write(tempFile, conf.encode().getBytes()); String[] args = {"run", "java:" + TestVerticle.class.getCanonicalName(), "-conf", tempFile.toString()}; launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); assertEquals(conf, TestVerticle.conf); }
@Test public void testRunVerticleWithConfFile() throws Exception { Path tempDir = testFolder.newFolder().toPath(); Path tempFile = Files.createTempFile(tempDir, "conf", "json"); MyStarter starter = new MyStarter(); JsonObject conf = new JsonObject().put("foo", "bar").put("wibble", 123); Files.write(tempFile, conf.encode().getBytes()); String[] args = {"run", "java:" + TestVerticle.class.getCanonicalName(), "-conf", tempFile.toString()}; starter.run(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); assertEquals(conf, TestVerticle.conf); cleanup(starter); }
@Test public void testRunVerticleWithConfString() throws Exception { MyLauncher launcher = new MyLauncher(); JsonObject conf = new JsonObject().put("foo", "bar").put("wibble", 123); String[] args = {"run", "java:" + TestVerticle.class.getCanonicalName(), "-conf", conf.encode()}; launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); assertEquals(conf, TestVerticle.conf); }
@Test public void testEncode() throws Exception { jsonObject.put("mystr", "foo"); jsonObject.put("mycharsequence", new StringBuilder("oob")); jsonObject.put("myint", 123); jsonObject.put("mylong", 1234l); jsonObject.put("myfloat", 1.23f); jsonObject.put("mydouble", 2.34d); jsonObject.put("myboolean", true); byte[] bytes = TestUtils.randomByteArray(10); jsonObject.put("mybinary", bytes); Instant now = Instant.now(); jsonObject.put("myinstant", now); jsonObject.putNull("mynull"); jsonObject.put("myobj", new JsonObject().put("foo", "bar")); jsonObject.put("myarr", new JsonArray().add("foo").add(123)); String strBytes = Base64.getEncoder().encodeToString(bytes); String expected = "{\"mystr\":\"foo\",\"mycharsequence\":\"oob\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + ISO_INSTANT.format(now) + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; String json = jsonObject.encode(); assertEquals(expected, json); }
@Test public void testCommentsInJson() { String jsonWithComments = "// single line comment\n" + "/*\n" + " This is a multi \n" + " line comment\n" + "*/\n" + "{\n" + "// another single line comment this time inside the JSON object itself\n" + " \"foo\": \"bar\" // and a single line comment at end of line \n" + "/*\n" + " This is a another multi \n" + " line comment this time inside the JSON object itself\n" + "*/\n" + "}"; JsonObject json = new JsonObject(jsonWithComments); assertEquals("{\"foo\":\"bar\"}", json.encode()); }
@Test public void testRunVerticleWithConfString() throws Exception { MyStarter starter = new MyStarter(); JsonObject conf = new JsonObject().put("foo", "bar").put("wibble", 123); String[] args = {"run", "java:" + TestVerticle.class.getCanonicalName(), "-conf", conf.encode()}; starter.run(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); assertEquals(conf, TestVerticle.conf); cleanup(starter); }
@Test public void testToJsonObject() throws Exception { JsonObject obj = new JsonObject(); obj.put("wibble", "wibble_value"); obj.put("foo", 5); obj.put("bar", true); Buffer buff = Buffer.buffer(obj.encode()); assertEquals(obj, buff.toJsonObject()); buff = Buffer.buffer(TestUtils.randomAlphaString(10)); try { buff.toJsonObject(); fail(); } catch (DecodeException ignore) { } }
mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "demo")); mongo.find("users", new JsonObject(), lookup -> { mongo.findOne("users", new JsonObject().put("_id", ctx.request().getParam("id")), null, lookup -> { } else { ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json"); ctx.response().end(user.encode()); ctx.response().end(newUser.encode()); }); ctx.response().end(user.encode()); });
@Test public void testDecode() throws Exception { byte[] bytes = TestUtils.randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); Instant now = Instant.now(); String strInstant = ISO_INSTANT.format(now); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(json); assertEquals(json, obj.encode()); assertEquals("foo", obj.getString("mystr")); assertEquals(Integer.valueOf(123), obj.getInteger("myint")); assertEquals(Long.valueOf(1234), obj.getLong("mylong")); assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat")); assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble")); assertTrue(obj.getBoolean("myboolean")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("myinstant")); assertTrue(obj.containsKey("mynull")); JsonObject nestedObj = obj.getJsonObject("myobj"); assertEquals("bar", nestedObj.getString("foo")); JsonArray nestedArr = obj.getJsonArray("myarr"); assertEquals("foo", nestedArr.getString(0)); assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1))); }
private static void replyError(SockJSSocket sock, String err) { JsonObject envelope = new JsonObject().put("type", "err").put("body", err); sock.write(buffer(envelope.encode())); }