JavaxJson { public static JsonValue wrap(final Object object) { if (object == null) { return JsonValue.NULL; } else if (object instanceof JsonValue) { return (JsonValue) object; } else if (object instanceof HasValue) { return ((HasValue) object).toValue().get(); } else if (object instanceof JsonObjectConvertible) { return ((JsonObjectConvertible) object).toJson(); } else if (object instanceof JsonArrayConvertible) { return ((JsonArrayConvertible) object).toJson(); } else if (object instanceof String) { return Json.createArrayBuilder().add((String) object).build().get(0); } else if (object instanceof Calendar) { return wrap(ISO8601.format((Calendar) object)); } else if (object instanceof Boolean) { return ((Boolean) object) ? JsonValue.TRUE : JsonValue.FALSE; } else if (object instanceof Number) { if (object instanceof Integer) { return Json.createArrayBuilder().add((Integer) object).build().get(0); } else if (object instanceof Long) { return Json.createArrayBuilder().add((Long) object).build().get(0); } else if (object instanceof Double) { return Json.createArrayBuilder().add((Double) object).build().get(0); } else if (object instanceof BigInteger) { return Json.createArrayBuilder().add((BigInteger) object).build().get(0); } else if (object instanceof BigDecimal) { return Json.createArrayBuilder().add((BigDecimal) object).build().get(0); } else { return Json.createArrayBuilder().add(new BigDecimal(object.toString())).build().get(0); } } else if (object instanceof Object[]) { return Arrays.stream((Object[]) object).map(JavaxJson::wrap) .collect(JsonCollectors.toJsonArray()); } else if (object.getClass().isArray() && object.getClass().getComponentType().isPrimitive()) { if (object instanceof int[]) { return Arrays.stream((int[]) object).mapToObj(JavaxJson::wrap).collect(JsonCollectors.toJsonArray()); } else if (object instanceof long[]) { return Arrays.stream((long[]) object).mapToObj(JavaxJson::wrap).collect(JsonCollectors.toJsonArray()); } else if (object instanceof double[]) { return Arrays.stream((double[]) object).mapToObj(JavaxJson::wrap).collect(JsonCollectors.toJsonArray()); } else if (object instanceof char[]) { return wrap(String.valueOf((char[]) object)); } else if (object instanceof byte[]) { return wrap(Base64.getUrlEncoder().encodeToString((byte[]) object)); } else if (object instanceof float[]) { final float[] floats = (float[]) object; final double[] doubles = new double[floats.length]; for (int i = 0; i < floats.length; i++) { doubles[i] = floats[i]; } return wrap(doubles); } else if (object instanceof boolean[]) { final boolean[] booleans = (boolean[]) object; final Boolean[] boxed = new Boolean[booleans.length]; for (int i = 0; i < booleans.length; i++) { boxed[i] = booleans[i]; } return wrap(boxed); } } else if (object instanceof Map) { return ((Map<?, ?>) object).entrySet().stream() .collect(Collectors.toMap( entry -> String.valueOf(entry.getKey()), entry -> wrap(entry.getValue()))).entrySet().stream() .collect(JsonCollectors.toJsonObject()); } else if (object instanceof Collection) { return ((Collection<?>) object).stream().map(JavaxJson::wrap) .collect(JsonCollectors.toJsonArray()); } return Json.createArrayBuilder().add(String.valueOf(object)).build().get(0); } private  JavaxJson(); static boolean nonEmptyValue(final @Nullable JsonValue value); static boolean nonDefaultValue(final @Nullable JsonValue value, final @NotNull JsonValue defaultValue); static JsonValue wrap(final Object object); @SuppressWarnings("WeakerAccess") static Object unwrap(final JsonValue jsonValue); @SuppressWarnings("WeakerAccess") static Map<String, Object> unwrapObject(final JsonObject jsonObject); @SuppressWarnings("WeakerAccess") static List<Object> unwrapArray(final JsonArray jsonArray); static @NotNull JsonObject shallowMergeObjects(final @Nullable JsonObject base,
                                                          final @Nullable JsonObject overlay); static Value val(Object value); static Obj obj(final Key... keys); static Obj obj(final JsonObject jsonObject); static Obj obj(final Map<?, ?> map); static Key key(final String key, final Object value); static KeyCursor key(final String key); static Arr arr(Object... values); @SuppressWarnings("WeakerAccess") static Optional<JsonObject> optObject(final JsonObject json, final String key); @SuppressWarnings("WeakerAccess") static JsonObject objectOrEmpty(final JsonObject json, final String key); @SuppressWarnings("WeakerAccess") static Optional<JsonArray> optArray(final JsonObject json, final String key); static JsonArray arrayOrEmpty(final JsonObject json, final String key); static List<String> mapArrayOfStrings(final JsonArray jsonArray); static List<R> mapArrayOfStrings(final JsonArray jsonArray,
                                                final Function<String, R> mapFunction); static List<R> mapArrayOfStrings(final JsonArray jsonArray,
                                                final Function<String, R> mapFunction,
                                                final boolean discardNulls); static List<R> mapArrayOfObjects(final JsonArray jsonArray,
                                                final Function<JsonObject, R> mapFunction); static List<R> mapArrayOfObjects(final JsonArray jsonArray,
                                                final Function<JsonObject, R> mapFunction,
                                                final boolean discardNulls); static List<R> mapObjectValues(final JsonObject jsonObject,
                                              final BiFunction<String, JsonObject, R> mapBiFunction,
                                              final boolean discardNulls); @SuppressWarnings("WeakerAccess") static List<R> parseFromArray(final @NotNull JsonArray jsonArray,
                                             final @NotNull Fun.ThrowingFunction<String, R> parser,
                                             final @Nullable BiConsumer<String, Exception> errorConsumer); static boolean hasNonNull(final @NotNull JsonObject json, final String key); @SuppressWarnings("WeakerAccess")
    static final Function<JsonValue, String> JSON_VALUE_STRING; }
@Test public void testWrap() { assertSame("null is JsonObject.NULL", JsonObject.NULL, wrap(null)); assertSame("same true", JsonObject.TRUE, wrap(true)); assertSame("same false", JsonObject.FALSE, wrap(false)); final JsonValue value = Json.createArrayBuilder().add("foo").build().get(0); assertSame("same value", value, wrap(value)); assertEquals("equal string value", value, wrap("foo")); final JsonObject object = key("foo", "bar").get(); final JsonObjectConvertible objectible = () -> object; assertSame("same value", object, wrap(objectible)); final JsonArray array = arr().val("foo1").val("foo2").val("foo3").get(); final JsonArrayConvertible arrayable = () -> array; assertSame("same value", array, wrap(arrayable)); final Calendar now = Calendar.getInstance(); assertEquals("same date", wrap(ISO8601.format(now)), wrap(now)); assertEquals("same int", 10, ((JsonNumber) wrap(10)).intValue()); assertEquals("same long", Integer.MAX_VALUE + 1L, ((JsonNumber) wrap(Integer.MAX_VALUE + 1L)).longValue()); assertEquals("same double", 42.0D, ((JsonNumber) wrap(42.0D)).doubleValue(), 1.0D); final BigInteger bigInteger = new BigInteger(Long.toString(Integer.MAX_VALUE + 1L)); assertEquals("same bigint", bigInteger, ((JsonNumber) wrap(bigInteger)).bigIntegerValue()); final BigDecimal bigDecimal = new BigDecimal(Double.toString(Integer.MAX_VALUE + 1L)); assertEquals("same bigd", bigDecimal, ((JsonNumber) wrap(bigDecimal)).bigDecimalValue()); final float fval = 4.0f; assertEquals("same float", Float.valueOf(fval).doubleValue(), ((JsonNumber) wrap(fval)).doubleValue(), 1.0D); final String[] foos = {"foo"}; assertEquals("strings should be wrapped inside of a JsonArray", Json.createArrayBuilder().add("foo").build(), wrap(foos)); assertEquals("ints should be wrapped inside of a JsonArray", Json.createArrayBuilder().add(42).build(), wrap(new int[]{42})); assertEquals("longs should be wrapped inside of a JsonArray", Json.createArrayBuilder().add(42L).build(), wrap(new long[]{42L})); assertEquals("doubles should be wrapped inside of a JsonArray", Json.createArrayBuilder().add(42.0D).build(), wrap(new double[]{42.0D})); assertEquals("chars should be wrapped inside of a String", Json.createArrayBuilder().add(String.valueOf(new char[]{'c', 'a', 't'})).build().get(0), wrap(new char[]{'c', 'a', 't'})); final byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8); final String bytesEncoded = Base64.getUrlEncoder().encodeToString(bytes); assertEquals("bytes should be Base64 encoded in a String", Json.createArrayBuilder().add(bytesEncoded).build().get(0), wrap(bytes)); assertEquals("floats should be converted to doubles and wrapped inside of a JsonArray", Json.createArrayBuilder().add(4.0D).build(), wrap(new float[]{4.0F})); assertEquals("booleans should be wrapped inside of a JsonArray", Json.createArrayBuilder().add(true).build(), wrap(new boolean[]{true})); final String prefix = "foo"; final String uri = "http: final Map<Integer, JsonNamespace> convMap = Collections .singletonMap(5, JsonNamespace.create(prefix, uri)); assertEquals("convert map to json obj", key("5", key(KEY_PREFIX, prefix).key(KEY_URI, uri)).get(), wrap(convMap)); final List<String> ordinals = Arrays.asList("one", "two", "three"); assertEquals("collection to json array of wrapped elements", Json.createArrayBuilder().add("one").add("two").add("three").build(), wrap(ordinals)); final String otherwiseString = "justAString"; final Object otherwise = new Object() { @Override public String toString() { return otherwiseString; } }; assertEquals("otherwise, just toString the object to a json string", Json.createArrayBuilder().add(otherwiseString).build().get(0), wrap(otherwise)); }