use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JsonObjectTest method testOrder.
@Test
public void testOrder() {
List<String> expectedKeys = new ArrayList<>();
int size = 100;
StringBuilder sb = new StringBuilder("{");
for (int i = 0; i < size; i++) {
sb.append("\"key-").append(i).append("\":").append(i).append(",");
expectedKeys.add("key-" + i);
}
sb.setCharAt(sb.length() - 1, '}');
JsonObject obj = new JsonObject(sb.toString());
List<String> keys = new ArrayList<>();
// ordered because of Jackson uses a LinkedHashMap
obj.forEach(e -> keys.add(e.getKey()));
assertEquals(expectedKeys, keys);
keys.clear();
// Ordered because we preserve the LinkedHashMap
obj.copy().forEach(e -> keys.add(e.getKey()));
assertEquals(expectedKeys, keys);
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JsonArrayTest method testIterator.
@Test
public void testIterator() {
jsonArray.add("foo");
jsonArray.add(123);
JsonObject obj = new JsonObject().put("foo", "bar");
jsonArray.add(obj);
Iterator<Object> iter = jsonArray.iterator();
assertTrue(iter.hasNext());
Object entry = iter.next();
assertEquals("foo", entry);
assertTrue(iter.hasNext());
entry = iter.next();
assertEquals(123, entry);
assertTrue(iter.hasNext());
entry = iter.next();
assertEquals(obj, entry);
assertFalse(iter.hasNext());
iter.remove();
assertFalse(jsonArray.contains(obj));
assertEquals(2, jsonArray.size());
}
use of io.vertx.core.json.JsonObject 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));
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JsonArrayTest method testDecode.
@Test
public void testDecode() {
byte[] bytes = TestUtils.randomByteArray(10);
String strBytes = Base64.getEncoder().encodeToString(bytes);
Instant now = Instant.now();
String strInstant = ISO_INSTANT.format(now);
String json = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",\"" + strInstant + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]";
JsonArray arr = new JsonArray(json);
assertEquals("foo", arr.getString(0));
assertEquals(Integer.valueOf(123), arr.getInteger(1));
assertEquals(Long.valueOf(1234l), arr.getLong(2));
assertEquals(Float.valueOf(1.23f), arr.getFloat(3));
assertEquals(Double.valueOf(2.34d), arr.getDouble(4));
assertEquals(true, arr.getBoolean(5));
assertTrue(TestUtils.byteArraysEqual(bytes, arr.getBinary(6)));
assertEquals(now, arr.getInstant(7));
assertTrue(arr.hasNull(8));
JsonObject obj = arr.getJsonObject(9);
assertEquals("bar", obj.getString("foo"));
JsonArray arr2 = arr.getJsonArray(10);
assertEquals("foo", arr2.getString(0));
assertEquals(Integer.valueOf(123), arr2.getInteger(1));
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class KeyStoreTest method testCopyKeyCertOptions.
@Test
public void testCopyKeyCertOptions() throws Exception {
PemKeyCertOptions options = new PemKeyCertOptions(new JsonObject());
String keyPath = TestUtils.randomAlphaString(100);
Buffer keyValue = Buffer.buffer(TestUtils.randomAlphaString(100));
String certPath = TestUtils.randomAlphaString(100);
Buffer certValue = Buffer.buffer(TestUtils.randomAlphaString(100));
options.setKeyPath(keyPath);
options.setKeyValue(keyValue);
options.setCertPath(certPath);
options.setCertValue(certValue);
options = new PemKeyCertOptions(options);
assertEquals(keyPath, options.getKeyPath());
assertEquals(keyValue, options.getKeyValue());
assertEquals(certPath, options.getCertPath());
assertEquals(certValue, options.getCertValue());
options = new PemKeyCertOptions(options.toJson());
assertEquals(keyPath, options.getKeyPath());
assertEquals(keyValue, options.getKeyValue());
assertEquals(certPath, options.getCertPath());
assertEquals(certValue, options.getCertValue());
}
Aggregations