|
@@ -0,0 +1,56 @@
|
|
|
+/*
|
|
|
+ * Nextcloud Android client application
|
|
|
+ *
|
|
|
+ * @author Alper Ozturk
|
|
|
+ * Copyright (C) 2023 Alper Ozturk
|
|
|
+ * Copyright (C) 2023 Nextcloud GmbH
|
|
|
+ *
|
|
|
+ * This program is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU Affero General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU Affero General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU Affero General Public License
|
|
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
+ */
|
|
|
+
|
|
|
+package com.nextcloud.utils;
|
|
|
+
|
|
|
+import com.google.gson.JsonDeserializationContext;
|
|
|
+import com.google.gson.JsonDeserializer;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonSerializationContext;
|
|
|
+import com.google.gson.JsonSerializer;
|
|
|
+
|
|
|
+import java.lang.reflect.Type;
|
|
|
+
|
|
|
+final public class InterfaceSerializer<T> implements JsonSerializer<T>, JsonDeserializer<T> {
|
|
|
+
|
|
|
+ private final Class<T> implementationClass;
|
|
|
+
|
|
|
+ private InterfaceSerializer(final Class<T> implementationClass) {
|
|
|
+ this.implementationClass = implementationClass;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> InterfaceSerializer<T> interfaceSerializer(final Class<T> implementationClass) {
|
|
|
+ return new InterfaceSerializer<>(implementationClass);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JsonElement serialize(final T value, final Type type, final JsonSerializationContext context) {
|
|
|
+ final Type targetType = value != null
|
|
|
+ ? value.getClass()
|
|
|
+ : type;
|
|
|
+ return context.serialize(value, targetType);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public T deserialize(final JsonElement jsonElement, final Type typeOfT, final JsonDeserializationContext context) {
|
|
|
+ return context.deserialize(jsonElement, implementationClass);
|
|
|
+ }
|
|
|
+}
|