/** * Deserialize a {@link JSONObject} with a {@link JsonSerializer}. * * @param jsonObject the {@link JSONObject} * @param serializer the {@link JsonSerializer} * @param cls the {@link Class} of the deserialization product * @return the deserialization product */ public static <T> T deserialize(JSONObject jsonObject, JsonSerializer<T> serializer, Class<? extends T> cls) { if (jsonObject == null || jsonObject.equals(JSONObject.NULL)) return null; return serializer.deserialize(jsonObject, cls); }
@Override public JSONObject serialize(SimpleEstimationContext ctx) { return EstimationContext.defaultSerializer.serialize(ctx); }
/** * 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); }
/** * Serialize the given {@link Object} using a specific {@link JsonSerializer}. * * @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 serializable} * @param serializer the {@link JsonSerializer} * @return the serialization result */ public static <T> JSONObject serialize(T obj, boolean isPolymorph, JsonSerializer<T> serializer) { return addClassTag(obj, serializer.serialize(obj), isPolymorph); }
/** * 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); } }