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));
}
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());
}
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
}
}
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);
}
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());
}
Aggregations