JsonUtils { public static <T> JsonObject createObject(Map<String, T> map, Function<T, JsonValue> itemToJson) { JsonObject object = Json.createObject(); map.forEach((key, value) -> object.put(key, itemToJson.apply(value))); return object; } private  JsonUtils(); static boolean jsonEquals(JsonValue a, JsonValue b); static Stream<T> stream(JsonArray array); static Stream<JsonObject> objectStream(JsonArray array); static DoubleStream numberStream(JsonArray array); static Collector<JsonValue, JsonArray, JsonArray> asArray(); static JsonArray createArray(JsonValue... values); static JsonObject createObject(Map<String, T> map,
            Function<T, JsonValue> itemToJson); static JsonObject beanToJson(Object bean); static JsonArray listToJson(List<?> list); static JsonObject mapToJson(Map<String, ?> map);  }
@Test public void createObject() { HashMap<String, Object> map = new HashMap<>(); map.put("integer", Integer.valueOf(3)); map.put("string", "foo"); JsonObject object = JsonUtils.createObject(map, item -> { if (item instanceof Integer) { return Json.create(((Integer) item).doubleValue()); } else { return Json.create(String.valueOf(item)); } }); Assert.assertEquals(2, object.keys().length); Assert.assertEquals(3, object.getNumber("integer"), 0); Assert.assertEquals("foo", object.getString("string")); }
@Test public void testCreateEmptyObject() { JsonObject object = JsonUtils.createObject(Collections.emptyMap(), item -> { throw new AssertionError("Callback should not be called"); }); Assert.assertEquals(0, object.keys().length); }