default JsonArray getArray(@Nonnull String... labels) { return getJsonObject().getArray(labels); }
public void shouldConcatenate() { String hocon="path : [ \"/bin\" ]\n" + "path : ${path} [ \"/usr/bin\" ]"; JsonObject parsed = parser.parseObject(hocon); assertThat(parsed.getArray("path").get(0).asString()).isEqualTo("/bin"); assertThat(parsed.getArray("path").get(1).asString()).isEqualTo("/usr/bin"); }
public void shouldConcatenateShortHand() { String hocon="path : [ \"/bin\" ]\n" + "path += \"/usr/bin\""; JsonObject parsed = parser.parseObject(hocon); assertThat(parsed.getArray("path").get(0).asString()).isEqualTo("/bin"); assertThat(parsed.getArray("path").get(1).asString()).isEqualTo("/usr/bin"); }
@Test(dataProvider="objectConstructors") public void shouldCreateArray(Supplier<JsonObject> supplier) { JsonObject object = supplier.get(); JsonArray createdArray = object.getOrCreateArray("a","b","c"); createdArray.add("1"); Assert.assertTrue(object.getArray("a","b","c").contains("1"), "array should have been added to the object"); }
public void shouldConvertFieldToSet() { JsonObject object = object(field("f", array(1,1,1,1,1))); assertThat(object.getOrCreateArray("f").size(), is(5)); assertThat(object.getOrCreateSet("f").size(), is(1)); assertThat(object.getArray("f").size(), is(1)); object.getArray("f").add(1); assertThat(object.getArray("f").size(), is(1)); }
public void shouldHandleJsonNullsOnGet() { JsonObject o = object().put("x", JsonPrimitive.JSON_NULL).get(); // should return the json null assertThat((JsonPrimitive)o.get("x"), CoreMatchers.notNullValue()); // these should all return a java null assertThat(o.getInt("x"), CoreMatchers.nullValue()); assertThat(o.getLong("x"), CoreMatchers.nullValue()); assertThat(o.getFloat("x"), CoreMatchers.nullValue()); assertThat(o.getDouble("x"), CoreMatchers.nullValue()); assertThat(o.getBoolean("x"), CoreMatchers.nullValue()); assertThat(o.getString("x"), CoreMatchers.nullValue()); assertThat(o.getArray("x"), CoreMatchers.nullValue()); assertThat(o.getObject("x"), CoreMatchers.nullValue()); }
public void shouldAddFields() { JsonObject object = object(field("meaningoflife", 42), field("foo", primitive("bar")), field("list",array("stuff"))); assertThat(object.getInt("meaningoflife"), is(42)); assertThat(object.getString("foo"), is("bar")); assertThat(object.getArray("list").get(0).asString(), is("stuff")); }
@Test(expectedExceptions=IllegalStateException.class) public void shouldNotAllowMutationsOnArrayInObject() { JsonObject object = object(field("f", array(1,1,1,1,1))); JsonObject clone = object.immutableClone(); assertThat(clone).isEqualTo(object); clone.getArray("f").add(1); }
"or a string"); assertTrue(object.getArray("array").isArray(), "works for arrays as well"); assertTrue(object.getObject("a","b").isObject(), "yep, we just added a string value 5 levels deep that did not exist so far"); object.getOrCreateArray("5","4","3","2","1").add("xxx"); assertTrue(object.getArray("5","4","3","2","1").contains("xxx"), "naturally it works for arrays too");