Skip to content

Commit 460a04d

Browse files
committed
[MCTCommon] Add save to json to data file
1 parent bc5418c commit 460a04d

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

src/main/java/com/minecrafttas/mctcommon/file/AbstractDataFile.java

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.io.OutputStream;
7+
import java.lang.reflect.Type;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
10+
import java.nio.file.StandardOpenOption;
911
import java.util.InvalidPropertiesFormatException;
12+
import java.util.Map.Entry;
1013
import java.util.Properties;
1114

15+
import com.google.gson.Gson;
16+
import com.google.gson.GsonBuilder;
17+
import com.google.gson.JsonDeserializationContext;
18+
import com.google.gson.JsonDeserializer;
19+
import com.google.gson.JsonElement;
20+
import com.google.gson.JsonObject;
21+
import com.google.gson.JsonParseException;
22+
import com.google.gson.JsonSerializationContext;
23+
import com.google.gson.JsonSerializer;
1224
import com.minecrafttas.mctcommon.MCTCommon;
1325

1426
public abstract class AbstractDataFile {
@@ -117,6 +129,28 @@ public void loadFromXML(Path file) {
117129
this.properties = newProp;
118130
}
119131

132+
public void loadFromJson() {
133+
loadFromJson(file);
134+
}
135+
136+
public void loadFromJson(Path file) {
137+
//@formatter:off
138+
Gson json = new GsonBuilder()
139+
.registerTypeAdapter(Properties.class, new PropertiesDeserializer())
140+
.create();
141+
//@formatter:on
142+
143+
String in;
144+
try {
145+
in = new String(Files.readAllBytes(file));
146+
} catch (IOException e) {
147+
MCTCommon.LOGGER.catching(e);
148+
return;
149+
}
150+
151+
properties = json.fromJson(in, Properties.class);
152+
}
153+
120154
public void save() {
121155
this.save(file);
122156
}
@@ -127,7 +161,7 @@ public void save(Path file) {
127161
properties.store(fos, comment);
128162
fos.close();
129163
} catch (IOException e) {
130-
e.printStackTrace();
164+
MCTCommon.LOGGER.catching(e);
131165
}
132166
}
133167

@@ -148,7 +182,53 @@ public void saveToXML(Path file) {
148182
properties.storeToXML(fos, comment, "UTF-8");
149183
fos.close();
150184
} catch (IOException e) {
151-
e.printStackTrace();
185+
MCTCommon.LOGGER.catching(e);
186+
}
187+
}
188+
189+
public void saveToJson() {
190+
saveToJson(file);
191+
}
192+
193+
public void saveToJson(Path file) {
194+
//@formatter:off
195+
Gson json = new GsonBuilder()
196+
.registerTypeAdapter(Properties.class, new PropertiesSerializer())
197+
.setPrettyPrinting()
198+
.create();
199+
//@formatter:on
200+
try {
201+
String element = json.toJson(properties);
202+
Files.write(file, element.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.CREATE);
203+
} catch (IOException e) {
204+
MCTCommon.LOGGER.catching(e);
205+
}
206+
}
207+
208+
public class PropertiesSerializer implements JsonSerializer<Properties> {
209+
210+
@Override
211+
public JsonElement serialize(Properties src, Type typeOfSrc, JsonSerializationContext context) {
212+
JsonObject obj = new JsonObject();
213+
src.forEach((key, val) -> {
214+
obj.addProperty((String) key, (String) val);
215+
});
216+
return obj;
217+
}
218+
}
219+
220+
public class PropertiesDeserializer implements JsonDeserializer<Properties> {
221+
222+
@Override
223+
public Properties deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
224+
Properties properties = new Properties();
225+
JsonObject obj = json.getAsJsonObject();
226+
for (Entry<String, JsonElement> elem : obj.entrySet()) {
227+
String key = elem.getKey();
228+
String val = elem.getValue().getAsString();
229+
properties.put(key, val);
230+
}
231+
return properties;
152232
}
153233
}
154234
}

0 commit comments

Comments
 (0)