/** * Like {@link #getJsonArray(String)} but specifies a default value to return if there is no entry. */ public JsonArray getJsonArray(final String key, final JsonArray def) { JsonArray val = getJsonArray(key); if (val == null) { if (map.containsKey(key)) { return null; } return def; } return val; }
@Test void testCreateFromMapNestedList() { Map<String, Object> map = new HashMap<>(); List<String> nestedArr = Arrays.asList("foo"); map.put("nested", nestedArr); JsonObject obj = new JsonObject(map); JsonArray nestedRetrieved = obj.getJsonArray("nested"); assertEquals("foo", nestedRetrieved.getString(0)); }
@Test void testCreateFromMapNestedJsonArray() { Map<String, Object> map = new HashMap<>(); JsonArray nestedArr = new JsonArray().add("foo"); map.put("nested", nestedArr); JsonObject obj = new JsonObject(map); JsonArray nestedRetrieved = obj.getJsonArray("nested"); assertEquals("foo", nestedRetrieved.getString(0)); }
@Test void testGetJsonArrayDefault() { JsonObject jsonObject = new JsonObject(); JsonArray arr = new JsonArray().add("blah").add("wibble"); JsonArray def = new JsonArray().add("quux").add("eek"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getJsonArray("foo", def)); assertEquals(arr, jsonObject.getJsonArray("foo", null)); jsonObject.put("foo", "hello"); try { jsonObject.getJsonArray("foo", def); fail("error"); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getJsonArray("foo", def)); assertEquals(def, jsonObject.getJsonArray("absent", def)); assertNull(jsonObject.getJsonArray("foo", null)); assertNull(jsonObject.getJsonArray("absent", null)); }
@Test void testGetJsonArray() { JsonObject jsonObject = new JsonObject(); JsonArray arr = new JsonArray().add("blah").add("wibble"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getJsonArray("foo")); jsonObject.put("foo", "hello"); try { jsonObject.getJsonArray("foo"); fail("error"); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getJsonArray("foo")); assertNull(jsonObject.getJsonArray("absent")); }
@Test void testPutJsonArray() { JsonObject jsonObject = new JsonObject(); JsonArray obj1 = new JsonArray().add("parp"); JsonArray obj2 = new JsonArray().add("fleep"); JsonArray obj3 = new JsonArray().add("woob"); assertSame(jsonObject, jsonObject.put("foo", obj1)); assertEquals(obj1, jsonObject.getJsonArray("foo")); jsonObject.put("quux", obj2); assertEquals(obj2, jsonObject.getJsonArray("quux")); assertEquals(obj1, jsonObject.getJsonArray("foo")); jsonObject.put("foo", obj3); assertEquals(obj3, jsonObject.getJsonArray("foo")); jsonObject.put("foo", (JsonArray) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, new JsonArray()); fail("error"); } catch (NullPointerException e) { // OK } }
@Test void testDecode() { JsonParsers.forEachParser(jsonParser -> { byte[] bytes = randomByteArray(10); String strBytes = Base64.getEncoder().encodeToString(bytes); String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}"; JsonObject obj = new JsonObject(jsonParser.parse(json)); 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")); assertArrayEquals(bytes, obj.getBinary("mybinary")); 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))); }); }
assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(obj, jsonObject.getJsonObject("obj")); assertEquals(arr, jsonObject.getJsonArray("arr")); try { jsonObject.put("inv", new SomeClass());