Search in sources :

Example 6 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class EventBusTestBase method testReplyJsonArray.

@Test
public void testReplyJsonArray() {
    JsonArray arr = new JsonArray();
    arr.add(TestUtils.randomUnicodeString(100)).add(TestUtils.randomInt()).add(TestUtils.randomBoolean());
    testReply(arr, (received) -> {
        assertEquals(arr, received);
        // Make sure it's copied
        assertFalse(arr == received);
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Test(org.junit.Test)

Example 7 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class FaultToleranceVerticle method ping.

private void ping(Message<JsonArray> message) {
    JsonArray jsonArray = message.body();
    for (int i = 0; i < jsonArray.size(); i++) {
        int node = jsonArray.getInteger(i);
        for (int j = 0; j < numAddresses; j++) {
            vertx.eventBus().send(createAddress(node, j), "ping", ar -> {
                if (ar.succeeded()) {
                    vertx.eventBus().send("control", "pong");
                } else {
                    Throwable cause = ar.cause();
                    if (cause instanceof ReplyException) {
                        ReplyException replyException = (ReplyException) cause;
                        if (replyException.failureType() == ReplyFailure.NO_HANDLERS) {
                            vertx.eventBus().send("control", "noHandlers");
                            return;
                        }
                    }
                    log.error("Unexpected error during ping (id=" + id + ")", cause);
                }
            });
        }
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ReplyException(io.vertx.core.eventbus.ReplyException)

Example 8 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class DeploymentTest method testJsonOptions.

@Test
public void testJsonOptions() {
    JsonObject config = new JsonObject().put("foo", "bar");
    Random rand = new Random();
    boolean worker = rand.nextBoolean();
    boolean multiThreaded = rand.nextBoolean();
    String isolationGroup = TestUtils.randomAlphaString(100);
    boolean ha = rand.nextBoolean();
    List<String> cp = Arrays.asList("foo", "bar");
    List<String> isol = Arrays.asList("com.foo.MyClass", "org.foo.*");
    String poolName = TestUtils.randomAlphaString(10);
    int poolSize = TestUtils.randomPositiveInt();
    long maxWorkerExecuteTime = TestUtils.randomPositiveLong();
    JsonObject json = new JsonObject();
    json.put("config", config);
    json.put("worker", worker);
    json.put("multiThreaded", multiThreaded);
    json.put("isolationGroup", isolationGroup);
    json.put("ha", ha);
    json.put("extraClasspath", new JsonArray(cp));
    json.put("isolatedClasses", new JsonArray(isol));
    json.put("workerPoolName", poolName);
    json.put("workerPoolSize", poolSize);
    json.put("maxWorkerExecuteTime", maxWorkerExecuteTime);
    DeploymentOptions options = new DeploymentOptions(json);
    assertEquals(worker, options.isWorker());
    assertEquals(multiThreaded, options.isMultiThreaded());
    assertEquals(isolationGroup, options.getIsolationGroup());
    assertEquals("bar", options.getConfig().getString("foo"));
    assertEquals(ha, options.isHa());
    assertEquals(cp, options.getExtraClasspath());
    assertEquals(isol, options.getIsolatedClasses());
    assertEquals(poolName, options.getWorkerPoolName());
    assertEquals(poolSize, options.getWorkerPoolSize());
    assertEquals(maxWorkerExecuteTime, options.getMaxWorkerExecuteTime());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 9 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class ConversionHelperTest method testWrapObject.

/**
   * Confirm that when we convert to map/list form we do so recursively.
   */
@Test
public void testWrapObject() {
    // Create a JsonObject with nested JsonObject and JsonArray values
    JsonObject obj = new JsonObject().put("nestedObj", new JsonObject().put("key", "value")).put("nestedList", new JsonArray().add(new JsonObject().put("key", "value")));
    // Get the wrapped form and confirm that it acted recursively
    Map<String, Object> wrapped = ConversionHelper.fromObject(obj);
    assertTrue(wrapped.get("nestedObj") instanceof Map);
    List<Object> theList = (List<Object>) wrapped.get("nestedList");
    assertTrue(theList.get(0) instanceof Map);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) AsciiString(io.netty.util.AsciiString) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 10 with JsonArray

use of io.vertx.core.json.JsonArray in project vert.x by eclipse.

the class ConversionHelperTest method testFromJsonArray.

@Test
public void testFromJsonArray() {
    JsonArray object = new JsonArray();
    object.add("the_string");
    object.add(4);
    object.add(true);
    object.add("hello".getBytes());
    object.add(new JsonObject().put("nested", 4));
    object.add(new JsonArray().add(1).add(2).add(3));
    List<Object> map = ConversionHelper.fromObject(object);
    assertEquals(6, map.size());
    assertEquals("the_string", map.get(0));
    assertEquals(4, map.get(1));
    assertEquals(true, map.get(2));
    assertEquals("hello", new String(Base64.getDecoder().decode((String) map.get(3))));
    assertEquals(Collections.singletonMap("nested", 4), map.get(4));
    assertEquals(Arrays.asList(1, 2, 3), map.get(5));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)78 Test (org.junit.Test)61 JsonObject (io.vertx.core.json.JsonObject)51 ArrayList (java.util.ArrayList)15 Instant (java.time.Instant)7 HashMap (java.util.HashMap)7 AsciiString (io.netty.util.AsciiString)5 Buffer (io.vertx.core.buffer.Buffer)5 Message (io.vertx.core.eventbus.Message)3 HttpMethod (io.vertx.core.http.HttpMethod)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ReplyException (io.vertx.core.eventbus.ReplyException)1 DecodeException (io.vertx.core.json.DecodeException)1 PemTrustOptions (io.vertx.core.net.PemTrustOptions)1 LocalMap (io.vertx.core.shareddata.LocalMap)1 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1