mirror of
https://github.com/OMGeeky/ATCS.git
synced 2025-12-26 23:57:25 +01:00
improve json saving to file
This commit is contained in:
@@ -1211,22 +1211,9 @@ public class Project implements ProjectTreeNode, Serializable {
|
|||||||
}
|
}
|
||||||
for (String fName : dataToWritePerFilename.keySet()) {
|
for (String fName : dataToWritePerFilename.keySet()) {
|
||||||
File jsonFile = new File(targetFolder, fName);
|
File jsonFile = new File(targetFolder, fName);
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
|
||||||
try {
|
String textToWrite = FileUtils.toJsonString(dataToWritePerFilename.get(fName));
|
||||||
JSONArray.writeJSONString(dataToWritePerFilename.get(fName), writer);
|
FileUtils.writeStringToFile(textToWrite, jsonFile, "JSON file '"+jsonFile.getAbsolutePath()+"'", false);
|
||||||
} 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return filenamesToWrite;
|
return filenamesToWrite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package com.gpl.rpg.atcontentstudio.model;
|
package com.gpl.rpg.atcontentstudio.model;
|
||||||
|
|
||||||
import com.gpl.rpg.atcontentstudio.Notification;
|
import com.gpl.rpg.atcontentstudio.Notification;
|
||||||
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
|
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -108,22 +107,8 @@ public class WorkspaceSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
json.put(VERSION_KEY, SETTINGS_VERSION);
|
json.put(VERSION_KEY, SETTINGS_VERSION);
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
String toWrite = FileUtils.toJsonString(json);
|
||||||
try {
|
FileUtils.writeStringToFile(toWrite, file, "Workspace settings");
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetDefault() {
|
public void resetDefault() {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
|
|||||||
import com.gpl.rpg.atcontentstudio.model.*;
|
import com.gpl.rpg.atcontentstudio.model.*;
|
||||||
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
||||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||||
|
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
|
|
||||||
import javax.swing.tree.TreeNode;
|
import javax.swing.tree.TreeNode;
|
||||||
@@ -157,26 +158,13 @@ public class GameDataCategory<E extends JSONElement> extends ArrayList<E> implem
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
|
||||||
try {
|
String toWrite = FileUtils.toJsonString(dataToSave);
|
||||||
JSONArray.writeJSONString(dataToSave, writer);
|
if(FileUtils.writeStringToFile(toWrite, jsonFile, "JSON file '"+jsonFile.getAbsolutePath()+"'")){
|
||||||
} catch (IOException e) {
|
|
||||||
//Impossible with a StringWriter
|
|
||||||
}
|
|
||||||
String toWrite = writer.toString();
|
|
||||||
try {
|
|
||||||
FileWriter w = new FileWriter(jsonFile);
|
|
||||||
w.write(toWrite);
|
|
||||||
w.close();
|
|
||||||
for (E element : this) {
|
for (E element : this) {
|
||||||
element.state = GameDataElement.State.saved;
|
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import com.gpl.rpg.atcontentstudio.Notification;
|
|||||||
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
|
import com.gpl.rpg.atcontentstudio.io.JsonPrettyWriter;
|
||||||
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
import com.gpl.rpg.atcontentstudio.model.GameDataElement;
|
||||||
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
|
import com.gpl.rpg.atcontentstudio.model.SaveEvent;
|
||||||
|
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
@@ -73,15 +74,9 @@ public abstract class JSONElement extends GameDataElement {
|
|||||||
public abstract Map toJson();
|
public abstract Map toJson();
|
||||||
|
|
||||||
public String toJsonString() {
|
public String toJsonString() {
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
Map json = this.toJson();
|
||||||
try {
|
return FileUtils.toJsonString(json);
|
||||||
JSONObject.writeJSONString(this.toJson(), writer);
|
|
||||||
} catch (IOException e) {
|
|
||||||
//Impossible with a StringWriter
|
|
||||||
}
|
}
|
||||||
return writer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GameDataSet getDataSet() {
|
public GameDataSet getDataSet() {
|
||||||
|
|||||||
@@ -145,16 +145,12 @@ public class ResourcesCompactor {
|
|||||||
private Minify jsonMinifier = new Minify();
|
private Minify jsonMinifier = new Minify();
|
||||||
|
|
||||||
private void writeJson(List<Map> dataToSave, File target) {
|
private void writeJson(List<Map> dataToSave, File target) {
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
String toWrite = FileUtils.toJsonString(dataToSave);
|
||||||
try {
|
toWrite = jsonMinifier.minify(toWrite);
|
||||||
JSONArray.writeJSONString(dataToSave, writer);
|
FileUtils.writeStringToFile(toWrite, target, null);
|
||||||
} catch (IOException e) {
|
|
||||||
//Impossible with a StringWriter
|
|
||||||
}
|
|
||||||
String toWrite = writer.toString();
|
|
||||||
try {
|
try {
|
||||||
FileWriter w = new FileWriter(target);
|
FileWriter w = new FileWriter(target);
|
||||||
w.write(jsonMinifier.minify(toWrite));
|
w.write(toWrite);
|
||||||
w.close();
|
w.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import com.gpl.rpg.atcontentstudio.model.*;
|
|||||||
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
import com.gpl.rpg.atcontentstudio.model.GameSource.Type;
|
||||||
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
import com.gpl.rpg.atcontentstudio.model.gamedata.GameDataSet;
|
||||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||||
|
import com.gpl.rpg.atcontentstudio.utils.FileUtils;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.parser.JSONParser;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.ParseException;
|
||||||
@@ -155,24 +156,12 @@ public class WriterModeDataSet implements ProjectTreeNode, Serializable {
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
StringWriter writer = new JsonPrettyWriter();
|
|
||||||
try {
|
String toWrite = FileUtils.toJsonString(dataToSave);
|
||||||
JSONArray.writeJSONString(dataToSave, writer);
|
if(FileUtils.writeStringToFile(toWrite, writerFile, "Json file " + writerFile.getAbsolutePath())) {
|
||||||
} catch (IOException e) {
|
|
||||||
//Impossible with a StringWriter
|
|
||||||
}
|
|
||||||
String toWrite = writer.toString();
|
|
||||||
try {
|
|
||||||
FileWriter w = new FileWriter(writerFile);
|
|
||||||
w.write(toWrite);
|
|
||||||
w.close();
|
|
||||||
for (WriterModeData element : writerModeDataList) {
|
for (WriterModeData element : writerModeDataList) {
|
||||||
element.state = GameDataElement.State.saved;
|
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,62 @@
|
|||||||
package com.gpl.rpg.atcontentstudio.utils;
|
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.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.zip.ZipEntry;
|
import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
public class FileUtils {
|
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) {
|
public static void deleteDir(File dir) {
|
||||||
if (dir.exists()) {
|
if (dir.exists()) {
|
||||||
for (File f : dir.listFiles()) {
|
for (File f : dir.listFiles()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user