/** * Try to serialize the given {@link Object}. It must be JSON-compatible or a {@link JsonSerializable}. * * @param obj the {@link Object} to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of {@code obj} * @return the serialization result * @see #isJsonCompatible(Object) */ public static Object serialize(Object obj, boolean isPolymorph) { if (obj == null) return null; if (isJsonCompatible(obj)) { return obj; } if (obj instanceof JsonSerializable) return serialize((JsonSerializable) obj, isPolymorph); throw new IllegalArgumentException(String.format("Cannot serialize %s.", obj)); }