/** * Prepares the JSON payload that carries on the token value. */ protected JsonResult tokenAsJson(final T authToken) { final JsonObject jsonObject = new JsonObject(); jsonObject.put("token", userAuth.tokenValue(authToken)); return JsonResult.of(jsonObject); }
/** * Like {@link #getBinary(String)} but specifies a default value to return if there is no entry. */ public byte[] getBinary(final String key, final byte[] def) { byte[] val = getBinary(key); if (val == null) { if (map.containsKey(key)) { return null; } return def; } return val; }
/** * Like {@link #getBoolean(String)} but specifies a default value to return if there is no entry. */ public Boolean getBoolean(final String key, final Boolean def) { Boolean val = getBoolean(key); if (val == null) { if (map.containsKey(key)) { return null; } return def; } return val; }
/** * Merges in another JSON object. * The merge is deep (recursive) to the specified level. If depth is 0, no merge is performed, * if depth is greater than the depth of one of the objects, a full deep merge is performed. */ @SuppressWarnings("unchecked") public JsonObject mergeIn(final JsonObject other, final int depth) { if (depth < 1) { return this; } if (depth == 1) { map.putAll(other.map); return this; } for (Map.Entry<String, Object> e : other.map.entrySet()) { map.merge(e.getKey(), e.getValue(), (oldVal, newVal) -> { if (oldVal instanceof Map) { oldVal = new JsonObject((Map) oldVal); } if (newVal instanceof Map) { newVal = new JsonObject((Map) newVal); } if (oldVal instanceof JsonObject && newVal instanceof JsonObject) { return ((JsonObject) oldVal).mergeIn((JsonObject) newVal, depth - 1); } return newVal; }); } return this; }
private void assertNumberNotEquals(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); } }
@Test void testGetString() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", "bar"); assertEquals("bar", jsonObject.getString("foo")); jsonObject.put("bar", 123); try { jsonObject.getString("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getString("foo")); assertNull(jsonObject.getString("absent")); }
@Test void testPutValue() { JsonObject jsonObject = new JsonObject(); jsonObject.put("str", (Object) "bar"); jsonObject.put("int", (Object) (Integer.valueOf(123))); jsonObject.put("long", (Object) (Long.valueOf(123L))); jsonObject.put("float", (Object) (Float.valueOf(1.23f))); jsonObject.put("double", (Object) (Double.valueOf(1.23d))); jsonObject.put("boolean", (Object) true); byte[] bytes = randomByteArray(10); jsonObject.put("binary", (Object) bytes); JsonObject obj = new JsonObject().put("foo", "blah"); JsonArray arr = new JsonArray().add("quux"); jsonObject.put("obj", (Object) obj); jsonObject.put("arr", (Object) arr); assertEquals("bar", jsonObject.getString("str")); assertEquals(Integer.valueOf(123), jsonObject.getInteger("int")); 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")); assertEquals(arr, jsonObject.getJsonArray("arr")); try { jsonObject.put("inv", new SomeClass()); fail("error"); jsonObject.put("inv", new BigDecimal(123)); try {
@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))); }); }
@Test void testMergeInFlat() { JsonParsers.forEachParser(jsonParser -> { JsonObject obj1 = jsonParser.parseAsJsonObject("{ \"foo\": { \"bar\": \"flurb\", \"eek\": 32 }}"); JsonObject obj2 = jsonParser.parseAsJsonObject("{ \"foo\": { \"bar\": \"eek\" }}"); obj1.mergeIn(obj2); assertEquals(1, obj1.size()); assertEquals(1, obj1.getJsonObject("foo").size()); assertEquals("eek", obj1.getJsonObject("foo").getString("bar")); }); }
@Test void testPutString() { JsonObject jsonObject = new JsonObject(); assertSame(jsonObject, jsonObject.put("foo", "bar")); assertEquals("bar", jsonObject.getString("foo")); jsonObject.put("quux", "wibble"); assertEquals("wibble", jsonObject.getString("quux")); assertEquals("bar", jsonObject.getString("foo")); jsonObject.put("foo", "blah"); assertEquals("blah", jsonObject.getString("foo")); jsonObject.put("foo", (String) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, "blah"); fail("error"); } catch (NullPointerException e) { // OK } }
@Test void testMergeIn1() { JsonObject jsonObject = new JsonObject(); JsonObject obj1 = new JsonObject().put("foo", "bar"); JsonObject obj2 = new JsonObject().put("eek", "flurb"); obj1.mergeIn(obj2); assertEquals(2, obj1.size()); assertEquals("bar", obj1.getString("foo")); assertEquals("flurb", obj1.getString("eek")); assertEquals(1, obj2.size()); assertEquals("flurb", obj2.getString("eek")); }
@Test void testCreateFromMap() { Map<String, Object> map = new HashMap<>(); map.put("foo", "bar"); map.put("quux", 123); JsonObject obj = new JsonObject(map); assertEquals("bar", obj.getString("foo")); assertEquals(Integer.valueOf(123), obj.getInteger("quux")); assertSame(map, obj.map()); }
@Test void testGetJsonObject() { JsonObject jsonObject = new JsonObject(); JsonObject obj = new JsonObject().put("blah", "wibble"); jsonObject.put("foo", obj); assertEquals(obj, jsonObject.getJsonObject("foo")); jsonObject.put("foo", "hello"); try { jsonObject.getJsonObject("foo"); fail("error"); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getJsonObject("foo")); assertNull(jsonObject.getJsonObject("absent")); }
@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 testGetBoolean() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", true); assertEquals(true, jsonObject.getBoolean("foo")); jsonObject.put("foo", false); assertEquals(false, jsonObject.getBoolean("foo")); jsonObject.put("bar", 123); try { jsonObject.getBoolean("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getBoolean("foo")); assertNull(jsonObject.getBoolean("absent")); }
@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 testGetInteger() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", 123); assertEquals(Integer.valueOf(123), jsonObject.getInteger("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getInteger("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123L); assertEquals(Integer.valueOf(123), jsonObject.getInteger("foo")); jsonObject.put("foo", 123d); assertEquals(Integer.valueOf(123), jsonObject.getInteger("foo")); jsonObject.put("foo", 123f); assertEquals(Integer.valueOf(123), jsonObject.getInteger("foo")); jsonObject.put("foo", Long.MAX_VALUE); assertEquals(Integer.valueOf(-1), jsonObject.getInteger("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getInteger("foo")); assertNull(jsonObject.getInteger("absent")); }
@Test void testGetFloat() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", 123f); assertEquals(Float.valueOf(123f), jsonObject.getFloat("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getFloat("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Float.valueOf(123f), jsonObject.getFloat("foo")); jsonObject.put("foo", 123d); assertEquals(Float.valueOf(123f), jsonObject.getFloat("foo")); jsonObject.put("foo", 123f); assertEquals(Float.valueOf(123L), jsonObject.getFloat("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getFloat("foo")); assertNull(jsonObject.getFloat("absent")); }
@Test void testGetLong() { JsonObject jsonObject = new JsonObject(); jsonObject.put("foo", 123L); assertEquals(Long.valueOf(123L), jsonObject.getLong("foo")); jsonObject.put("bar", "hello"); try { jsonObject.getLong("bar"); fail("error"); } catch (ClassCastException e) { // Ok } // Put as different Number types jsonObject.put("foo", 123); assertEquals(Long.valueOf(123L), jsonObject.getLong("foo")); jsonObject.put("foo", 123d); assertEquals(Long.valueOf(123L), jsonObject.getLong("foo")); jsonObject.put("foo", 123f); assertEquals(Long.valueOf(123L), jsonObject.getLong("foo")); jsonObject.put("foo", Long.MAX_VALUE); assertEquals(Long.valueOf(Long.MAX_VALUE), jsonObject.getLong("foo")); // Null and absent values jsonObject.putNull("foo"); assertNull(jsonObject.getLong("foo")); assertNull(jsonObject.getLong("absent")); }
@Test void testCreateFromMapNestedJsonObject() { Map<String, Object> map = new HashMap<>(); JsonObject nestedObj = new JsonObject().put("foo", "bar"); map.put("nested", nestedObj); JsonObject obj = new JsonObject(map); JsonObject nestedRetrieved = obj.getJsonObject("nested"); assertEquals("bar", nestedRetrieved.getString("foo")); }