@Override public String toString() { return encode(); }
@Override public void writeToBuffer(Buffer buffer) { String encoded = encode(); byte[] bytes = encoded.getBytes(); buffer.appendInt(bytes.length); buffer.appendBytes(bytes); }
private void handleListProducts(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); conn.query("SELECT id, name, price, weight FROM products", query -> { if (query.failed()) { sendError(500, response); } else { JsonArray arr = new JsonArray(); query.result().getRows().forEach(arr::add); routingContext.response().putHeader("content-type", "application/json").end(arr.encode()); } }); }
@GET @Path("/products") @Produces({MediaType.APPLICATION_JSON}) public void list( // Suspend the request @Suspended final AsyncResponse asyncResponse, // Inject the Vertx instance @Context Vertx vertx) { // Send a list message to the backend vertx.eventBus().<JsonArray>send("backend", new JsonObject().put("op", "list"), msg -> { // When we get the response we resume the Jax-RS async response if (msg.succeeded()) { JsonArray json = msg.result().body(); if (json != null) { asyncResponse.resume(json.encode()); } else { asyncResponse.resume(Response.status(Response.Status.NOT_FOUND).build()); } } else { asyncResponse.resume(Response.status(Response.Status.INTERNAL_SERVER_ERROR).build()); } }); } }
System.out.println(line.encode());
System.out.println(line.encode());
System.out.println(line.encode());
System.out.println(line.encode());
.handler(row -> { System.out.println(row.encode()); }) .endHandler(v -> {
@Test public void testCreateFromBuffer() { JsonArray excepted = new JsonArray(); excepted.add("foobar"); excepted.add(123); Buffer buf = Buffer.buffer(excepted.encode()); assertEquals(excepted, new JsonArray(buf)); }
@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 array 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 array itself\n" + "*/\n" + "]"; JsonArray json = new JsonArray(jsonWithComments); assertEquals("[\"foo\",\"bar\"]", json.encode()); }
@Override public void start() throws Exception { JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true") .put("driver_class", "org.hsqldb.jdbcDriver"); JDBCClient jdbc = JDBCClient.createShared(vertx, config); jdbc .rxGetConnection() // Connect to the database .flatMapObservable(conn -> { // With the connection... return conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))") // ...create test table .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")) // ...insert a row .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")) // ...another one .flatMap(result -> conn.rxQueryStream("SELECT * FROM test")) // ...get values stream .flatMapObservable(sqlRowStream -> { return sqlRowStream.toObservable() // Transform the stream into an Observable... .doOnTerminate(conn::close); // ...and close the connection when the stream is fully read or an error occurs }); }).subscribe(row -> System.out.println("Row : " + row.encode())); } }
@Override public void start() throws Exception { JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true") .put("driver_class", "org.hsqldb.jdbcDriver"); JDBCClient jdbc = JDBCClient.createShared(vertx, config); jdbc .rxGetConnection() // Connect to the database .flatMapPublisher(conn -> { // With the connection... return conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))") // ...create test table .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")) // ...insert a row .flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")) // ...another one .flatMap(result -> conn.rxQueryStream("SELECT * FROM test")) // ...get values stream .flatMapPublisher(sqlRowStream -> { return sqlRowStream.toFlowable() // Transform the stream into a Flowable... .doOnTerminate(() -> { // ...and close the connection when the stream is fully read or an // error occurs conn.close(); System.out.println("Connection closed"); }); }); }).subscribe(row -> System.out.println("Row : " + row.encode())); } }
@Test public void testToJsonArray() throws Exception { JsonArray arr = new JsonArray(); arr.add("wibble"); arr.add(5); arr.add(true); Buffer buff = Buffer.buffer(arr.encode()); assertEquals(arr, buff.toJsonArray()); buff = Buffer.buffer(TestUtils.randomAlphaString(10)); try { buff.toJsonObject(); fail(); } catch (DecodeException ignore) { } }
@Test public void testToString() { jsonArray.add("foo").add(123); assertEquals(jsonArray.encode(), jsonArray.toString()); }
ctx.response().end(json.encode()); }); });
ctx.response().end(json.encode()); }); });
@Test public void testEncode() throws Exception { jsonArray.add("foo"); jsonArray.add(123); jsonArray.add(1234l); jsonArray.add(1.23f); jsonArray.add(2.34d); jsonArray.add(true); byte[] bytes = TestUtils.randomByteArray(10); jsonArray.add(bytes); jsonArray.addNull(); jsonArray.add(new JsonObject().put("foo", "bar")); jsonArray.add(new JsonArray().add("foo").add(123)); String strBytes = Base64.getEncoder().encodeToString(bytes); String expected = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]"; String json = jsonArray.encode(); assertEquals(expected, json); }
@Test public void testCreateFromBuffer() { JsonArray excepted = new JsonArray(); excepted.add("foobar"); excepted.add(123); Buffer buf = Buffer.buffer(excepted.encode()); assertEquals(excepted, new JsonArray(buf)); }
@Test public void testToString() { jsonArray.add("foo").add(123); assertEquals(jsonArray.encode(), jsonArray.toString()); }