-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
87 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
osgan-files/src/main/java/dev/httpmarco/osgon/files/Document.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,40 @@ | ||
package dev.httpmarco.osgon.files; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.SneakyThrows; | ||
import lombok.experimental.Accessors; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@Getter | ||
@Accessors(fluent = true) | ||
public abstract class Document<T> { | ||
|
||
private final Path path; | ||
private T value; | ||
|
||
public Document(T defaultValue, Path path) { | ||
this.value = defaultValue; | ||
this.path = path; | ||
|
||
this.updateDocument(); | ||
} | ||
|
||
public void value(T value) { | ||
this.value = value; | ||
} | ||
|
||
public void updateWithValue(T value) { | ||
this.value = value; | ||
this.updateDocument(); | ||
} | ||
|
||
public abstract void updateDocument(); | ||
|
||
@SneakyThrows | ||
public void delete() { | ||
Files.delete(this.path); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...on/files/configuration/ConfigExclude.java → ...sgon/files/annotations/ConfigExclude.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
66 changes: 0 additions & 66 deletions
66
osgan-files/src/main/java/dev/httpmarco/osgon/files/configuration/gson/JsonUtils.java
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...es/configuration/gson/haste/HasteAPI.java → ...httpmarco/osgon/files/haste/HasteAPI.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
6 changes: 3 additions & 3 deletions
6
...gson/exclusion/ByteExclusionStrategy.java → ...files/json/JsonByteExclusionStrategy.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
20 changes: 20 additions & 0 deletions
20
osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonDocument.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,20 @@ | ||
package dev.httpmarco.osgon.files.json; | ||
|
||
import dev.httpmarco.osgon.files.Document; | ||
import lombok.SneakyThrows; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class JsonDocument<T> extends Document<T> { | ||
|
||
public JsonDocument(T defaultValue, Path path) { | ||
super(defaultValue, path); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public void updateDocument() { | ||
Files.writeString(path(), JsonUtils.toPrettyJson(value())); | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
osgan-files/src/main/java/dev/httpmarco/osgon/files/json/JsonUtils.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,54 @@ | ||
package dev.httpmarco.osgon.files.json; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
|
||
public class JsonUtils { | ||
|
||
private static final Gson JSON = new GsonBuilder() | ||
.disableHtmlEscaping() | ||
.setExclusionStrategies(new JsonByteExclusionStrategy()) | ||
.create(); | ||
private static final Gson PRETTY_JSON = new GsonBuilder() | ||
.setPrettyPrinting() | ||
.disableHtmlEscaping() | ||
.setExclusionStrategies(new JsonByteExclusionStrategy()) | ||
.create(); | ||
|
||
|
||
public static Gson getGson() { | ||
return JSON; | ||
} | ||
|
||
public static Gson getPrettyGson() { | ||
return PRETTY_JSON; | ||
} | ||
|
||
public static String toJson(Object object) { | ||
return JSON.toJson(object); | ||
} | ||
|
||
public static String toPrettyJson(Object object) { | ||
return PRETTY_JSON.toJson(object); | ||
} | ||
|
||
public static void writeJson(Object object, FileWriter fileWriter) { | ||
JSON.toJson(object, fileWriter); | ||
} | ||
|
||
public static void writePrettyJson(Object object, FileWriter fileWriter) { | ||
PRETTY_JSON.toJson(object, fileWriter); | ||
} | ||
|
||
public static <T> T fromJson(String string, Class<T> tClass) { | ||
return JSON.fromJson(string, tClass); | ||
} | ||
|
||
public static <T> T fromJson(FileReader fileReader, Class<T> tClass) { | ||
return JSON.fromJson(fileReader, tClass); | ||
} | ||
} | ||
|