io.vertx.core.parsetools.JsonParser Java Examples

The following examples show how to use io.vertx.core.parsetools.JsonParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonStreamTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleStream(TestContext tc) {
  AtomicInteger counter = new AtomicInteger();
  Async async = tc.async();
  JsonParser parser = JsonParser.newParser().objectValueMode()
    .exceptionHandler(tc::fail)
    .handler(event -> {
      JsonObject object = event.objectValue();
      tc.assertEquals(counter.getAndIncrement(), object.getInteger("count"));
      tc.assertEquals("some message", object.getString("data"));
    })
    .endHandler(x -> async.complete());

  client.get("/?separator=nl&count=10").as(BodyCodec.jsonStream(parser)).send(x -> {
    if (x.failed()) {
      tc.fail(x.cause());
    }
  });
}
 
Example #2
Source File: JsonStreamTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleStreamUsingBlankLine(TestContext tc) {
  AtomicInteger counter = new AtomicInteger();
  Async async = tc.async();
  JsonParser parser = JsonParser.newParser().objectValueMode()
    .exceptionHandler(tc::fail)
    .handler(event -> {
      JsonObject object = event.objectValue();
      tc.assertEquals(counter.getAndIncrement(), object.getInteger("count"));
      tc.assertEquals("some message", object.getString("data"));
    })
    .endHandler(x -> async.complete());

  client.get("/?separator=bl&count=10").as(BodyCodec.jsonStream(parser)).send(x -> {
    if (x.failed()) {
      tc.fail(x.cause());
    }
  });
}
 
Example #3
Source File: WebClientExamples.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
public void receiveResponseAsJsonStream(WebClient client) {
  JsonParser parser = JsonParser.newParser().objectValueMode();
  parser.handler(event -> {
    JsonObject object = event.objectValue();
    System.out.println("Got " + object.encode());
  });
  client
    .get(8080, "myserver.mycompany.com", "/some-uri")
    .as(BodyCodec.jsonStream(parser))
    .send()
    .onSuccess(res ->
      System.out.println("Received response with status code" + res.statusCode()))
    .onFailure(err ->
      System.out.println("Something went wrong " + err.getMessage()));
}
 
Example #4
Source File: JsonStreamBodyCodec.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public JsonStreamBodyCodec(JsonParser parser) {
  this.parser = Objects.requireNonNull(parser, "The parser must be set");
  this.delegate = new StreamingBodyCodec(new WriteStream<Buffer>() {
    @Override
    public WriteStream<Buffer> exceptionHandler(Handler<Throwable> handler) {
      parser.exceptionHandler(handler);
      return this;
    }

    @Override
    public Future<Void> write(Buffer buffer) {
      parser.handle(buffer);
      return Future.succeededFuture();
    }

    @Override
    public void write(Buffer data, Handler<AsyncResult<Void>> handler) {
      parser.handle(data);
      if (handler != null) {
        handler.handle(Future.succeededFuture());
      }
    }

    @Override
    public void end(Handler<AsyncResult<Void>> handler) {
      parser.end();
      if (handler != null) {
        handler.handle(Future.succeededFuture());
      }
    }

    @Override
    public WriteStream<Buffer> setWriteQueueMaxSize(int i) {
      return this;
    }

    @Override
    public boolean writeQueueFull() {
      return false;
    }

    @Override
    public WriteStream<Buffer> drainHandler(@Nullable Handler<Void> handler) {
      return this;
    }
  });
}
 
Example #5
Source File: JsonStreamBodyCodec.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
/**
 * @return the JSON parser.
 */
JsonParser getParser() {
  return parser;
}
 
Example #6
Source File: BodyCodec.java    From vertx-web with Apache License 2.0 2 votes vote down vote up
/**
 * A body codec that parse the response as a JSON stream.
 *
 * @param parser the non-null JSON parser to emits the JSON object. The parser must be configured for the stream. Not
 *               e that you need to keep a reference on the parser to retrieved the JSON events.
 * @return the body codec for a write stream
 */
static BodyCodec<Void> jsonStream(JsonParser parser) {
  return new JsonStreamBodyCodec(parser);
}