improve json saving to file

This commit is contained in:
OMGeeky
2025-06-24 21:50:52 +02:00
parent 1cbcd5b661
commit a3ffecfd23
7 changed files with 1644 additions and 1657 deletions

View File

@@ -1211,22 +1211,9 @@ public class Project implements ProjectTreeNode, Serializable {
}
for (String fName : dataToWritePerFilename.keySet()) {
File jsonFile = new File(targetFolder, fName);
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToWritePerFilename.get(fName), writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String textToWrite = writer.toString();
try {
FileWriter w = new FileWriter(jsonFile);
w.write(textToWrite);
w.close();
// Notification.addSuccess("Json file "+jsonFile.getAbsolutePath()+" saved.");
} catch (IOException e) {
Notification.addError("Error while writing json file " + jsonFile.getAbsolutePath() + " : " + e.getMessage());
e.printStackTrace();
}
String textToWrite = FileUtils.toJsonString(dataToWritePerFilename.get(fName));
FileUtils.writeStringToFile(textToWrite, jsonFile, "JSON file '"+jsonFile.getAbsolutePath()+"'", false);
}
return filenamesToWrite;
}

View File

@@ -1,8 +1,7 @@
package com.gpl.rpg.atcontentstudio.model;
import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import org.json.simple.JSONObject;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.parser.JSONParser;
import java.io.*;
@@ -108,22 +107,8 @@ public class WorkspaceSettings {
}
json.put(VERSION_KEY, SETTINGS_VERSION);
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(file);
w.write(toWrite);
w.close();
Notification.addSuccess("Workspace settings saved.");
} catch (IOException e) {
Notification.addError("Error while saving workspace settings : " + e.getMessage());
e.printStackTrace();
}
String toWrite = FileUtils.toJsonString(json);
FileUtils.writeStringToFile(toWrite, file, "Workspace settings");
}
public void resetDefault() {

View File

@@ -5,6 +5,7 @@ import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONArray;
import javax.swing.tree.TreeNode;
@@ -157,26 +158,13 @@ public class GameDataCategory<E extends JSONElement> extends ArrayList<E> implem
return;
}
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(jsonFile);
w.write(toWrite);
w.close();
String toWrite = FileUtils.toJsonString(dataToSave);
if(FileUtils.writeStringToFile(toWrite, jsonFile, "JSON file '"+jsonFile.getAbsolutePath()+"'")){
for (E element : this) {
element.state = GameDataElement.State.saved;
}
Notification.addSuccess("Json file " + jsonFile.getAbsolutePath() + " saved.");
} catch (IOException e) {
Notification.addError("Error while writing json file " + jsonFile.getAbsolutePath() + " : " + e.getMessage());
e.printStackTrace();
}
}

View File

@@ -4,6 +4,7 @@ import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@@ -73,16 +74,10 @@ public abstract class JSONElement extends GameDataElement {
public abstract Map toJson();
public String toJsonString() {
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(this.toJson(), writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
Map json = this.toJson();
return FileUtils.toJsonString(json);
}
@Override
public GameDataSet getDataSet() {
if (parent == null) {

View File

@@ -145,16 +145,12 @@ public class ResourcesCompactor {
private Minify jsonMinifier = new Minify();
private void writeJson(List<Map> dataToSave, File target) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
String toWrite = FileUtils.toJsonString(dataToSave);
toWrite = jsonMinifier.minify(toWrite);
FileUtils.writeStringToFile(toWrite, target, null);
try {
FileWriter w = new FileWriter(target);
w.write(jsonMinifier.minify(toWrite));
w.write(toWrite);
w.close();
} catch (IOException e) {
e.printStackTrace();

View File

@@ -6,6 +6,7 @@ import com.gpl.rpg.atcontentstudio.model.*;
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@@ -155,24 +156,12 @@ public class WriterModeDataSet implements ProjectTreeNode, Serializable {
return;
}
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(dataToSave, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
String toWrite = writer.toString();
try {
FileWriter w = new FileWriter(writerFile);
w.write(toWrite);
w.close();
String toWrite = FileUtils.toJsonString(dataToSave);
if(FileUtils.writeStringToFile(toWrite, writerFile, "Json file " + writerFile.getAbsolutePath())) {
for (WriterModeData element : writerModeDataList) {
element.state = GameDataElement.State.saved;
}
Notification.addSuccess("Json file " + writerFile.getAbsolutePath() + " saved.");
} catch (IOException e) {
Notification.addError("Error while writing json file " + writerFile.getAbsolutePath() + " : " + e.getMessage());
e.printStackTrace();
}
}

View File

@@ -1,15 +1,62 @@
package com.gpl.rpg.atcontentstudio.utils;
import com.gpl.rpg.atcontentstudio.Notification;
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static String toJsonString(Map json) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONObject.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
}
public static String toJsonString(List json) {
StringWriter writer = new JsonPrettyWriter();
try {
JSONArray.writeJSONString(json, writer);
} catch (IOException e) {
//Impossible with a StringWriter
}
return writer.toString();
}
public static boolean writeStringToFile(String toWrite, File file, String type) {
return writeStringToFile(toWrite, file, type, true);
}
public static boolean writeStringToFile(String toWrite, File file, String type, boolean notifyOnSuccess) {
try {
FileWriter w = new FileWriter(file);
w.write(toWrite);
w.close();
if(type != null) {
Notification.addSuccess(type + " saved.");
}
return true;
} catch (IOException e) {
if(type != null) {
Notification.addError("Error while saving " + type + " : " + e.getMessage());
}
e.printStackTrace();
return false;
}
}
public static void deleteDir(File dir) {
if (dir.exists()) {
for (File f : dir.listFiles()) {