/** * Tag the {@link JSONObject} with the {@link Class} of the other {@link Object}. * * @param obj whose {@link Class} should be tagged * @param jsonObject that should be tagged * @return the {@code jsonObject} */ public static JSONObject addClassTag(Object obj, JSONObject jsonObject) { if (isJsonNull(jsonObject)) return jsonObject; return jsonObject.put("_class", obj.getClass().getCanonicalName()); }
/** * Read and load the specified {@link Class} in a {@link JSONObject}. * * @param jsonObject that contains the {@link Class} tag * @return the loaded {@link Class} or {@code null} if none exists * @throws ClassNotFoundException if the {@link Class} could not be loaded */ public static Class<?> getClassTag(JSONObject jsonObject) throws ClassNotFoundException { if (isJsonNull(jsonObject) || !jsonObject.has("_class")) return null; final String className = jsonObject.getString("_class"); return Class.forName(className); }
/** * Deserialize a ({@link Class}-tagged) {@link JSONObject} with a {@link JsonSerializer}. * * @param jsonObject the {@link JSONObject} * @param serializer the {@link JsonSerializer} * @return the deserialization product */ public static <T> T deserialize(JSONObject jsonObject, JsonSerializer<T> serializer) { if (isJsonNull(jsonObject)) return null; return serializer.deserialize(jsonObject); }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(JSONObject, JsonSerializer)}. * * @param jsonArray the {@link JSONArray} * @param serializer the {@link JsonSerializer} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray, JsonSerializer<T> serializer) { List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add(isJsonNull(jsonElement) ? null : deserialize((JSONObject) jsonElement, serializer)); } return result; }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(JSONObject, JsonSerializer, Class)}. * * @param jsonArray the {@link JSONArray} * @param serializer the {@link JsonSerializer} * @param cls the {@link Class} of the elements in the {@code jsonArray} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray, JsonSerializer<T> serializer, Class<T> cls) { List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add(isJsonNull(jsonElement) ? null : deserialize((JSONObject) jsonElement, serializer, cls)); } return result; }
/** * Deserialize a {@link JSONArray} according to the rules of {@link #deserialize(Object)}. * * @param jsonArray the {@link JSONArray} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> List<T> deserializeAllAsList(JSONArray jsonArray) { if (isJsonNull(jsonArray)) return null; List<T> result = new ArrayList<>(jsonArray.length()); for (Object jsonElement : jsonArray) { result.add((T) deserialize(jsonElement)); } return result; }
/** * Try to serialize the given {@link Object}s. They must be JSON-compatible or a {@link JsonSerializable}s. * * @param collection the {@link Object}s to serialize * @param isPolymorph in case a {@link JSONObject} is created, whether it should be tagged with the {@link Class} * of the according {@link Object} * @return the serialization result * @see #isJsonCompatible(Object) */ public static JSONArray serializeAll(Collection<?> collection, boolean isPolymorph) { if (isJsonNull(collection)) return null; JSONArray jsonArray = new JSONArray(); for (Object obj : collection) { jsonArray.put(serialize(obj, isPolymorph)); } return jsonArray; }
/** * Deserialize a {@link Class}-tagged {@link JSONObject} that should correspond to a {@link JsonSerializable}. * * @param jsonObject the {@link JSONObject} * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> T deserialize(JSONObject jsonObject) { if (isJsonNull(jsonObject)) return null; return deserialize(jsonObject, JsonSerializable.uncheckedSerializer()); }
/** * Deserialize a {@link JSONObject} that should correspond to a {@link JsonSerializable}. * * @param jsonObject the {@link JSONObject} * @param cls the {@link Class} of the deserialization product * @return the deserialization product */ @SuppressWarnings("unchecked") public static <T> T deserialize(JSONObject jsonObject, Class<? extends T> cls) { if (isJsonNull(jsonObject)) return null; return deserialize(jsonObject, (JsonSerializer<T>) JsonSerializable.uncheckedSerializer(), cls); }
/** * Deserializes an object. * * @param json that should be serialized * @return the deserialized object */ @SuppressWarnings("unchecked") default T deserialize(JSONObject json) { if (JsonSerializables.isJsonNull(json)) return null; try { final Class<?> classTag = JsonSerializables.getClassTag(json); if (classTag == null) { throw new IllegalArgumentException(String.format("Cannot determine class from %s.", json)); } return this.deserialize(json, (Class<? extends T>) classTag); } catch (ClassNotFoundException e) { throw new SerializationException("Could not load class.", e); } }
/** * 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)); }