Search in sources :

Example 51 with JsonObject

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

the class JsonArrayTest method testGetValue.

@Test
public void testGetValue() {
    jsonArray.add(123);
    assertEquals(123, jsonArray.getValue(0));
    jsonArray.add(123l);
    assertEquals(123l, jsonArray.getValue(1));
    jsonArray.add(123f);
    assertEquals(123f, jsonArray.getValue(2));
    jsonArray.add(123d);
    assertEquals(123d, jsonArray.getValue(3));
    jsonArray.add(false);
    assertEquals(false, jsonArray.getValue(4));
    jsonArray.add(true);
    assertEquals(true, jsonArray.getValue(5));
    jsonArray.add("bar");
    assertEquals("bar", jsonArray.getValue(6));
    JsonObject obj = new JsonObject().put("blah", "wibble");
    jsonArray.add(obj);
    assertEquals(obj, jsonArray.getValue(7));
    JsonArray arr = new JsonArray().add("blah").add("wibble");
    jsonArray.add(arr);
    assertEquals(arr, jsonArray.getValue(8));
    byte[] bytes = TestUtils.randomByteArray(100);
    jsonArray.add(bytes);
    assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonArray.getValue(9))));
    Instant now = Instant.now();
    jsonArray.add(now);
    assertEquals(now, jsonArray.getInstant(10));
    jsonArray.addNull();
    assertNull(jsonArray.getValue(11));
    try {
        jsonArray.getValue(-1);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    try {
        jsonArray.getValue(12);
        fail();
    } catch (IndexOutOfBoundsException e) {
    // OK
    }
    // JsonObject with inner Map
    List<Object> list = new ArrayList<>();
    Map<String, Object> innerMap = new HashMap<>();
    innerMap.put("blah", "wibble");
    list.add(innerMap);
    jsonArray = new JsonArray(list);
    obj = (JsonObject) jsonArray.getValue(0);
    assertEquals("wibble", obj.getString("blah"));
    // JsonObject with inner List
    list = new ArrayList<>();
    List<Object> innerList = new ArrayList<>();
    innerList.add("blah");
    list.add(innerList);
    jsonArray = new JsonArray(list);
    arr = (JsonArray) jsonArray.getValue(0);
    assertEquals("blah", arr.getString(0));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HashMap(java.util.HashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 52 with JsonObject

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

the class JsonArrayTest method testCopy.

@Test
public void testCopy() {
    jsonArray.add("foo");
    jsonArray.add(123);
    JsonObject obj = new JsonObject().put("foo", "bar");
    jsonArray.add(obj);
    jsonArray.add(new StringBuilder("eeek"));
    JsonArray copy = jsonArray.copy();
    assertEquals("eeek", copy.getString(3));
    assertNotSame(jsonArray, copy);
    assertEquals(jsonArray, copy);
    assertEquals(4, copy.size());
    assertEquals("foo", copy.getString(0));
    assertEquals(Integer.valueOf(123), copy.getInteger(1));
    assertEquals(obj, copy.getJsonObject(2));
    assertNotSame(obj, copy.getJsonObject(2));
    copy.add("foo");
    assertEquals(4, jsonArray.size());
    jsonArray.add("bar");
    assertEquals(5, copy.size());
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 53 with JsonObject

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

the class JsonArrayTest method testEncodePrettily.

@Test
public void testEncodePrettily() 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, {" + Utils.LINE_SEPARATOR + "  \"foo\" : \"bar\"" + Utils.LINE_SEPARATOR + "}, [ \"foo\", 123 ] ]";
    String json = jsonArray.encodePrettily();
    assertEquals(expected, json);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 54 with JsonObject

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

the class JsonArrayTest method testGetList.

@Test
public void testGetList() {
    JsonObject obj = new JsonObject().put("quux", "wibble");
    jsonArray.add("foo").add(123).add(obj);
    List<Object> list = jsonArray.getList();
    list.remove("foo");
    assertFalse(jsonArray.contains("foo"));
    list.add("floob");
    assertTrue(jsonArray.contains("floob"));
    assertSame(obj, list.get(1));
    obj.remove("quux");
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 55 with JsonObject

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

the class JsonArrayTest method testAddJsonObject.

@Test
public void testAddJsonObject() {
    JsonObject obj = new JsonObject().put("foo", "bar");
    assertSame(jsonArray, jsonArray.add(obj));
    assertEquals(obj, jsonArray.getJsonObject(0));
    try {
        jsonArray.add((JsonObject) null);
        fail();
    } catch (NullPointerException e) {
    // OK
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)383 Test (org.junit.Test)210 JsonArray (io.vertx.core.json.JsonArray)89 HttpURLConnection (java.net.HttpURLConnection)68 Future (io.vertx.core.Future)55 Handler (io.vertx.core.Handler)51 Async (io.vertx.ext.unit.Async)50 Vertx (io.vertx.core.Vertx)48 TestContext (io.vertx.ext.unit.TestContext)48 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)47 RunWith (org.junit.runner.RunWith)47 Timeout (org.junit.rules.Timeout)43 Before (org.junit.Before)41 Rule (org.junit.Rule)33 EventBusMessage (org.eclipse.hono.util.EventBusMessage)27 AsyncResult (io.vertx.core.AsyncResult)26 Buffer (io.vertx.core.buffer.Buffer)26 Constants (org.eclipse.hono.util.Constants)25 Mockito (org.mockito.Mockito)25 Objects (java.util.Objects)23