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());
}
use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonArrayTest method testAddJsonArray.
@Test
public void testAddJsonArray() {
JsonArray arr = new JsonArray().add("foo");
assertSame(jsonArray, jsonArray.add(arr));
assertEquals(arr, jsonArray.getJsonArray(0));
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 JsonObjectTest method testGetValue.
@Test
public void testGetValue() {
jsonObject.put("foo", 123);
assertEquals(123, jsonObject.getValue("foo"));
jsonObject.put("foo", 123l);
assertEquals(123l, jsonObject.getValue("foo"));
jsonObject.put("foo", 123f);
assertEquals(123f, jsonObject.getValue("foo"));
jsonObject.put("foo", 123d);
assertEquals(123d, jsonObject.getValue("foo"));
jsonObject.put("foo", false);
assertEquals(false, jsonObject.getValue("foo"));
jsonObject.put("foo", true);
assertEquals(true, jsonObject.getValue("foo"));
jsonObject.put("foo", "bar");
assertEquals("bar", jsonObject.getValue("foo"));
JsonObject obj = new JsonObject().put("blah", "wibble");
jsonObject.put("foo", obj);
assertEquals(obj, jsonObject.getValue("foo"));
JsonArray arr = new JsonArray().add("blah").add("wibble");
jsonObject.put("foo", arr);
assertEquals(arr, jsonObject.getValue("foo"));
byte[] bytes = TestUtils.randomByteArray(100);
jsonObject.put("foo", bytes);
assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo"))));
jsonObject.putNull("foo");
assertNull(jsonObject.getValue("foo"));
assertNull(jsonObject.getValue("absent"));
// JsonObject with inner Map
Map<String, Object> map = new HashMap<>();
Map<String, Object> innerMap = new HashMap<>();
innerMap.put("blah", "wibble");
map.put("foo", innerMap);
jsonObject = new JsonObject(map);
obj = (JsonObject) jsonObject.getValue("foo");
assertEquals("wibble", obj.getString("blah"));
// JsonObject with inner List
map = new HashMap<>();
List<Object> innerList = new ArrayList<>();
innerList.add("blah");
map.put("foo", innerList);
jsonObject = new JsonObject(map);
arr = (JsonArray) jsonObject.getValue("foo");
assertEquals("blah", arr.getString(0));
}
use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonObjectTest method assertNumberEquals.
private void assertNumberEquals(Number value1, Number value2) {
JsonObject o1 = new JsonObject().put("key", value1);
JsonObject o2 = new JsonObject().put("key", value2);
if (!o1.equals(o2)) {
fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
}
JsonArray a1 = new JsonArray().add(value1);
JsonArray a2 = new JsonArray().add(value2);
if (!a1.equals(a2)) {
fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
}
}
use of io.vertx.core.json.JsonArray in project vert.x by eclipse.
the class JsonObjectTest method testDecode.
@Test
public void testDecode() throws Exception {
byte[] bytes = TestUtils.randomByteArray(10);
String strBytes = Base64.getEncoder().encodeToString(bytes);
Instant now = Instant.now();
String strInstant = ISO_INSTANT.format(now);
String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}";
JsonObject obj = new JsonObject(json);
assertEquals(json, obj.encode());
assertEquals("foo", obj.getString("mystr"));
assertEquals(Integer.valueOf(123), obj.getInteger("myint"));
assertEquals(Long.valueOf(1234), obj.getLong("mylong"));
assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat"));
assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble"));
assertTrue(obj.getBoolean("myboolean"));
assertTrue(TestUtils.byteArraysEqual(bytes, obj.getBinary("mybinary")));
assertEquals(now, obj.getInstant("myinstant"));
assertTrue(obj.containsKey("mynull"));
JsonObject nestedObj = obj.getJsonObject("myobj");
assertEquals("bar", nestedObj.getString("foo"));
JsonArray nestedArr = obj.getJsonArray("myarr");
assertEquals("foo", nestedArr.getString(0));
assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1)));
}
Aggregations