-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom gson provider for Duration
Signed-off-by: applenick <[email protected]>
- Loading branch information
Showing
5 changed files
with
44 additions
and
3 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
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
13 changes: 13 additions & 0 deletions
13
src/main/java/dev/pgm/community/utils/gson/GsonProvider.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,13 @@ | ||
package dev.pgm.community.utils.gson; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import dev.pgm.community.utils.gson.types.DurationConverter; | ||
import java.time.Duration; | ||
|
||
public class GsonProvider { | ||
|
||
public static Gson get() { | ||
return new GsonBuilder().registerTypeAdapter(Duration.class, new DurationConverter()).create(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/dev/pgm/community/utils/gson/types/DurationConverter.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,25 @@ | ||
package dev.pgm.community.utils.gson.types; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import java.lang.reflect.Type; | ||
import java.time.Duration; | ||
|
||
public class DurationConverter implements JsonSerializer<Duration>, JsonDeserializer<Duration> { | ||
|
||
@Override | ||
public JsonElement serialize(Duration src, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(src.toString()); | ||
} | ||
|
||
@Override | ||
public Duration deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
return Duration.parse(json.getAsString()); | ||
} | ||
} |