/** * Like {@link #getDouble(String)} but specifies a default value to return if there is no entry. */ public Double getDouble(final String key, final Double def) { Double val = getDouble(key); if (val == null) { if (map.containsKey(key)) { return null; } return def; } return val; }
@Test void testGetDoubleDefault() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(123), jsonObject.getDouble("foo", null)); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar", 123d); fail("error"); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); jsonObject.put("foo", 123L); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo", 321d)); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getDouble("foo", 321d)); assertEquals(Double.valueOf(321d), jsonObject.getDouble("absent", 321d)); assertNull(jsonObject.getDouble("foo", null)); assertNull(jsonObject.getDouble("absent", null)); }
@Test void testGetDouble() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", 123d); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getDouble("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123L); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 123f); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getDouble("foo")); assertNull(jsonObject.getDouble("absent")); }
@Test void testPutDouble() { JsonObject jsonObject = new JsonObject(); assertSame(jsonObject, jsonObject.put("foo", 123d)); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("quux", 321d); assertEquals(Double.valueOf(321d), jsonObject.getDouble("quux")); assertEquals(Double.valueOf(123d), jsonObject.getDouble("foo")); jsonObject.put("foo", 456d); assertEquals(Double.valueOf(456d), jsonObject.getDouble("foo")); jsonObject.put("foo", (Double) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, 1.23d); 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))); }); }
assertEquals(Long.valueOf(123L), jsonObject.getLong("long")); assertEquals(Float.valueOf(1.23f), jsonObject.getFloat("float")); assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(obj, jsonObject.getJsonObject("obj"));