io.vertx.core.json.JsonArray.encode()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(98)

本文整理了Java中io.vertx.core.json.JsonArray.encode()方法的一些代码示例,展示了JsonArray.encode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonArray.encode()方法的具体详情如下:
包路径:io.vertx.core.json.JsonArray
类名称:JsonArray
方法名:encode

JsonArray.encode介绍

[英]Encode the JSON array to a string
[中]将JSON数组编码为字符串

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public String toString() {
 return encode();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public void writeToBuffer(Buffer buffer) {
 String encoded = encode();
 byte[] bytes = encoded.getBytes();
 buffer.appendInt(bytes.length);
 buffer.appendBytes(bytes);
}

代码示例来源:origin: vert-x3/vertx-examples

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());
  }
 });
}

代码示例来源:origin: vert-x3/vertx-examples

@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());
   }
  });
 }
}

代码示例来源:origin: vert-x3/vertx-examples

System.out.println(line.encode());

代码示例来源:origin: vert-x3/vertx-examples

System.out.println(line.encode());

代码示例来源:origin: vert-x3/vertx-examples

System.out.println(line.encode());

代码示例来源:origin: vert-x3/vertx-examples

System.out.println(line.encode());

代码示例来源:origin: vert-x3/vertx-examples

.handler(row -> {
 System.out.println(row.encode());
})
.endHandler(v -> {

代码示例来源:origin: eclipse-vertx/vert.x

@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));
}

代码示例来源:origin: eclipse-vertx/vert.x

@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());
}

代码示例来源:origin: vert-x3/vertx-examples

@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()));
 }
}

代码示例来源:origin: vert-x3/vertx-examples

@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()));
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@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) {
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testToString() {
 jsonArray.add("foo").add(123);
 assertEquals(jsonArray.encode(), jsonArray.toString());
}

代码示例来源:origin: vert-x3/vertx-examples

ctx.response().end(json.encode());
 });
});

代码示例来源:origin: vert-x3/vertx-examples

ctx.response().end(json.encode());
 });
});

代码示例来源:origin: eclipse-vertx/vert.x

@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);
}

代码示例来源:origin: io.vertx/vertx-core

@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));
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testToString() {
 jsonArray.add("foo").add(123);
 assertEquals(jsonArray.encode(), jsonArray.toString());
}

相关文章