Search in sources :

Example 31 with JsonArray

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

the class JsonObjectTest method testCreateFromMapNestedJsonArray.

@Test
public void testCreateFromMapNestedJsonArray() {
    Map<String, Object> map = new HashMap<>();
    JsonArray nestedArr = new JsonArray().add("foo");
    map.put("nested", nestedArr);
    JsonObject obj = new JsonObject(map);
    JsonArray nestedRetrieved = obj.getJsonArray("nested");
    assertEquals("foo", nestedRetrieved.getString(0));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 32 with JsonArray

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

the class JsonArrayTest method testGetJsonArray.

@Test
public void testGetJsonArray() {
    JsonArray arr = new JsonArray().add("foo");
    jsonArray.add(arr);
    assertEquals(arr, jsonArray.getJsonArray(0));
    try {
        jsonArray.getJsonArray(-1);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    try {
        jsonArray.getJsonArray(1);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    jsonArray.add(123);
    try {
        jsonArray.getJsonArray(1);
        fail();
    } catch (ClassCastException e) {
    // OK
    }
    jsonArray.addNull();
    assertNull(jsonArray.getJsonArray(2));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Test(org.junit.Test)

Example 33 with JsonArray

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

the class JsonArrayTest method testInvalidValsOnCopy.

@Test
public void testInvalidValsOnCopy() {
    List<Object> invalid = new ArrayList<>();
    invalid.add(new SomeClass());
    JsonArray arr = new JsonArray(invalid);
    try {
        arr.copy();
        fail();
    } catch (IllegalStateException e) {
    // OK
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 34 with JsonArray

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

the class JsonArrayTest method testContains.

@Test
public void testContains() {
    jsonArray.add("wibble");
    jsonArray.add(true);
    jsonArray.add(123);
    JsonObject obj = new JsonObject();
    JsonArray arr = new JsonArray();
    jsonArray.add(obj);
    jsonArray.add(arr);
    assertFalse(jsonArray.contains("eek"));
    assertFalse(jsonArray.contains(false));
    assertFalse(jsonArray.contains(321));
    assertFalse(jsonArray.contains(new JsonObject().put("blah", "flib")));
    assertFalse(jsonArray.contains(new JsonArray().add("oob")));
    assertTrue(jsonArray.contains("wibble"));
    assertTrue(jsonArray.contains(true));
    assertTrue(jsonArray.contains(123));
    assertTrue(jsonArray.contains(obj));
    assertTrue(jsonArray.contains(arr));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 35 with JsonArray

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

the class JsonArrayTest method testCreateFromListNestedJsonArray.

@Test
public void testCreateFromListNestedJsonArray() {
    List<Object> list = new ArrayList<>();
    list.add("foo");
    list.add(123);
    JsonArray arr2 = new JsonArray().add("blah").add("wibble");
    list.add(arr2);
    JsonArray arr = new JsonArray(list);
    assertEquals("foo", arr.getString(0));
    assertEquals(Integer.valueOf(123), arr.getInteger(1));
    assertSame(list, arr.getList());
    assertSame(arr2, arr.getJsonArray(2));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)116 JsonObject (io.vertx.core.json.JsonObject)82 Test (org.junit.Test)72 ArrayList (java.util.ArrayList)19 Future (io.vertx.core.Future)15 Buffer (io.vertx.core.buffer.Buffer)15 Handler (io.vertx.core.Handler)14 HttpURLConnection (java.net.HttpURLConnection)13 HashMap (java.util.HashMap)13 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 RunWith (org.junit.runner.RunWith)10 StandardCharsets (java.nio.charset.StandardCharsets)9 Instant (java.time.Instant)9 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)9 Vertx (io.vertx.core.Vertx)8 Async (io.vertx.ext.unit.Async)8 Constants (org.eclipse.hono.util.Constants)8 CredentialsObject (org.eclipse.hono.util.CredentialsObject)8 Before (org.junit.Before)8