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