-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: alperozturk <[email protected]>
- Loading branch information
1 parent
145d6d4
commit 7150507
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |