Skip to content

Commit

Permalink
screw it, revert to Gson
Browse files Browse the repository at this point in the history
  • Loading branch information
Burchard36 committed Nov 20, 2021
1 parent ea599eb commit 0a1605e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 34 deletions.
18 changes: 3 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,10 @@
<scope>provided</scope>
</dependency>

<!--<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.12.0</version>
</dependency>-->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.13.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>
5 changes: 0 additions & 5 deletions src/main/java/com/burchard36/json/JsonDataFile.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.burchard36.json;

import com.burchard36.json.enums.FileFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class JsonDataFile {

@JsonIgnore
public JavaPlugin plugin;
@JsonIgnore
public FileFormat format;
@JsonIgnore
private final File file;

public JsonDataFile(final JavaPlugin plugin,
Expand All @@ -26,7 +22,6 @@ public JsonDataFile(final JavaPlugin plugin,
this.file = new File(this.plugin.getDataFolder(), pathToFile);
}

@JsonIgnore
public final File getFile() {
return this.file;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/burchard36/json/PluginDataManager.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.burchard36.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;

public class PluginDataManager {

private final ObjectMapper mapper;
private final Gson gson;
private final PluginJsonWriter jsonWriter;
public HashMap<Enum<?>, PluginDataMap> dataMap = new HashMap<>();

public PluginDataManager(final JavaPlugin plugin) {
this.mapper = new ObjectMapper();
this.jsonWriter = new PluginJsonWriter(this.mapper);
this.gson = new GsonBuilder().create();
this.jsonWriter = new PluginJsonWriter(this.gson);
}

/**
Expand Down Expand Up @@ -79,11 +80,11 @@ public final void saveAll() {
}

/**
* Returns the Jackson instance
* @return instance of Jackson
* Returns the Gson instance
* @return instance of Gson
*/
public final ObjectMapper getMapper() {
return this.mapper;
public final Gson getGson() {
return this.gson;
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/burchard36/json/PluginJsonWriter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.burchard36.json;

import com.burchard36.Logger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public record PluginJsonWriter(ObjectMapper mapper) {
public record PluginJsonWriter(Gson gson) {

public File createFile(final JsonDataFile config) {
final File file = config.getFile();
Expand All @@ -14,8 +16,7 @@ public File createFile(final JsonDataFile config) {
if (!file.getParentFile().exists())
if (file.getParentFile().mkdirs())
Logger.log("&aAPI :: Successfully created directories");
if (file.createNewFile()) this.mapper.writeValue(file, config);

if (file.createNewFile()) this.gson.toJson(config, new FileWriter(file));
} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -28,7 +29,7 @@ public void writeDataToFile(final JsonDataFile config) {
if (!file.exists()) file = this.createFile(config);

try {
this.mapper.writeValue(file, config);
this.gson.toJson(config, new FileWriter(file));
} catch (IOException ex) {
ex.printStackTrace();
}
Expand All @@ -40,7 +41,7 @@ public JsonDataFile getDataFromFile(final JsonDataFile config) {
if (!file.exists()) file = this.createFile(config);

try {
return this.mapper.readValue(file, config.getClass());
return this.gson.fromJson(Files.newBufferedReader(Paths.get(file.toURI())), config.getClass());
} catch (IOException ex) {
ex.printStackTrace();
return null;
Expand Down

0 comments on commit 0a1605e

Please sign in to comment.