Search in sources :

Example 21 with JsonArray

use of io.vertx.core.json.JsonArray 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 22 with JsonArray

use of io.vertx.core.json.JsonArray 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 23 with JsonArray

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

the class JsonArrayTest method testAddAllJsonArray.

@Test
public void testAddAllJsonArray() {
    jsonArray.add("bar");
    JsonArray arr = new JsonArray().add("foo").add(48);
    assertSame(jsonArray, jsonArray.addAll(arr));
    assertEquals(arr.getString(0), jsonArray.getString(1));
    assertEquals(arr.getInteger(1), jsonArray.getInteger(2));
    try {
        jsonArray.add((JsonArray) null);
        fail();
    } catch (NullPointerException e) {
    // OK
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Test(org.junit.Test)

Example 24 with JsonArray

use of io.vertx.core.json.JsonArray 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 25 with JsonArray

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

the class JsonArrayTest method testCreateFromListCharSequence.

@Test
public void testCreateFromListCharSequence() {
    List<Object> list = new ArrayList<>();
    list.add("foo");
    list.add(123);
    list.add(new StringBuilder("eek"));
    JsonArray arr = new JsonArray(list);
    assertEquals("foo", arr.getString(0));
    assertEquals(Integer.valueOf(123), arr.getInteger(1));
    assertEquals("eek", arr.getString(2));
    assertSame(list, arr.getList());
}
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)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