use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JsonObjectTest method testCopy.
@Test
public void testCopy() {
jsonObject.put("foo", "bar");
jsonObject.put("quux", 123);
JsonObject obj = createJsonObject();
jsonObject.put("wibble", obj);
// CharSequence
jsonObject.put("eek", new StringBuilder("blah"));
JsonObject copy = jsonObject.copy();
assertNotSame(jsonObject, copy);
assertEquals(jsonObject, copy);
copy.put("blah", "flib");
assertFalse(jsonObject.containsKey("blah"));
copy.remove("foo");
assertFalse(copy.containsKey("foo"));
assertTrue(jsonObject.containsKey("foo"));
jsonObject.put("oob", "flarb");
assertFalse(copy.containsKey("oob"));
jsonObject.remove("quux");
assertFalse(jsonObject.containsKey("quux"));
assertTrue(copy.containsKey("quux"));
JsonObject nested = jsonObject.getJsonObject("wibble");
JsonObject nestedCopied = copy.getJsonObject("wibble");
assertNotSame(nested, nestedCopied);
assertEquals(nested, nestedCopied);
assertEquals("blah", copy.getString("eek"));
}
use of io.vertx.core.json.JsonObject in project vert.x by eclipse.
the class JsonObjectTest method testGetJsonObjectDefault.
@Test
public void testGetJsonObjectDefault() {
JsonObject obj = new JsonObject().put("blah", "wibble");
JsonObject def = new JsonObject().put("eek", "quuz");
jsonObject.put("foo", obj);
assertEquals(obj, jsonObject.getJsonObject("foo", def));
assertEquals(obj, jsonObject.getJsonObject("foo", null));
jsonObject.put("foo", "hello");
try {
jsonObject.getJsonObject("foo", def);
fail();
} catch (ClassCastException e) {
// Ok
}
jsonObject.putNull("foo");
assertNull(jsonObject.getJsonObject("foo", def));
assertEquals(def, jsonObject.getJsonObject("absent", def));
assertNull(jsonObject.getJsonObject("foo", null));
assertNull(jsonObject.getJsonObject("absent", null));
try {
jsonObject.getJsonObject(null, null);
fail();
} catch (NullPointerException e) {
// OK
}
}
use of io.vertx.core.json.JsonObject 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.JsonObject in project vert.x by eclipse.
the class JsonObjectTest method testIterator.
@Test
public void testIterator() {
jsonObject.put("foo", "bar");
jsonObject.put("quux", 123);
JsonObject obj = createJsonObject();
jsonObject.put("wibble", obj);
Iterator<Map.Entry<String, Object>> iter = jsonObject.iterator();
assertTrue(iter.hasNext());
Map.Entry<String, Object> entry = iter.next();
assertEquals("foo", entry.getKey());
assertEquals("bar", entry.getValue());
assertTrue(iter.hasNext());
entry = iter.next();
assertEquals("quux", entry.getKey());
assertEquals(123, entry.getValue());
assertTrue(iter.hasNext());
entry = iter.next();
assertEquals("wibble", entry.getKey());
assertEquals(obj, entry.getValue());
assertFalse(iter.hasNext());
iter.remove();
assertFalse(obj.containsKey("wibble"));
assertEquals(2, jsonObject.size());
}
use of io.vertx.core.json.JsonObject 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);
}
}
Aggregations