/** * Tells whether the given instance is a JSON datatype. * * @param obj the instance to test * @return whether {@code obj} is a JSON datatype */ public static boolean isJsonCompatible(Object obj) { return isUnconvertedInstance(obj) || obj == JSONObject.NULL || obj instanceof JSONObject || obj instanceof JSONArray; }
/** * Deserialize a given JSON datatype. The following cases are supported: * <ul> * <li>{@code json} is a (JSON) {@code null} value;</li> * <li>{@code json} is a basic (JSON) datatype;</li> * <li>{@code json} is a {@link Class}-tagged {@link JSONObject} that corresponds to a {@link JsonSerializable};</li> * <li>{@code json} is a {@link JSONArray} with {@link Class}-tagged {@link JSONObject}s that correspond to a * {@link JsonSerializable}s - in this case, the result type is a {@link List}.</li> * </ul> * * @param json the JSON data * @return the deserialization result */ public static Object deserialize(Object json) { if (isJsonNull(json)) return null; else if (isUnconvertedInstance(json)) return json; else if (json instanceof JSONObject) return deserialize((JSONObject) json); else if (json instanceof JSONArray) return deserializeAllAsList((JSONArray) json); throw new SerializationException(String.format("Don't know how to deserialize %s.", json)); }