default JsonObject getOrCreateObject(final String... labels) { return getJsonObject().getOrCreateObject(labels); } @JsonIgnore
@Test(dataProvider="objectConstructors") public void shouldCreateObject(Supplier<JsonObject> supplier) { JsonObject object = supplier.get(); JsonObject createdObject = object.getOrCreateObject("a","b","c"); createdObject.put("foo", "bar"); Assert.assertTrue(object.getString("a","b","c", "foo").equals("bar"), "object should have been added"); }
@Test(expectedExceptions=JsonTypeMismatchException.class) public void shouldThrowExceptionOnElementThatIsNotAnObject() { JsonObject object = object().put("a", object().put("b", 42).get()).get(); object.getOrCreateObject("a","b"); }
public void shouldReturnExistingObject() { JsonObject object = object().put("a", object().put("b", object().put("foo","bar").get()).get()).get(); JsonObject orCreateObject = object.getOrCreateObject("a","b"); Assert.assertTrue(orCreateObject.getString("foo").equals("bar"), "return the object with foo=bar"); }
object.getOrCreateObject("1","2","3","4").put("5", "xxx"); assertTrue(object.getString("1","2","3","4","5").equals("xxx"), "yep, we just added a string value 5 levels deep that did not exist so far");