commit ba73203a4276b661d99a5573ad75852802ebfe0e Author: JernejTDO Date: Sun Jun 8 10:44:03 2025 +0200 UTD diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f0e9804 --- /dev/null +++ b/.classpath @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..7df6f9d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + JsonManager + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..29abf99 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,6 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 +encoding//src/test/java=UTF-8 +encoding//src/test/resources=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2af1e7b --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..f897a7f --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b13bea8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + eu.triler.code + JsonManager + 1.0 + JsonManager + + 11 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + TrilerGit + https://git.triler.eu/api/packages/TrilerCode/maven + + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + com.google.code.gson + gson + 2.9.1 + + + \ No newline at end of file diff --git a/src/main/java/eu/triler/code/JsonManager/JsonManager.java b/src/main/java/eu/triler/code/JsonManager/JsonManager.java new file mode 100644 index 0000000..62d7cf0 --- /dev/null +++ b/src/main/java/eu/triler/code/JsonManager/JsonManager.java @@ -0,0 +1,99 @@ +package eu.triler.code.JsonManager; + +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class JsonManager { + + private static final String LOG_PREFIX = "[NetworkManager] "; + private static final String FOLDER_PREFIX = "[FOLDER MANAGER] "; + private static final String JSON_PREFIX = "[JSON FILE MANAGER] "; + + private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); + + private static void consoleLog(String message) { + String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")); + System.out.println(timestamp + " " + LOG_PREFIX + message); + } + + public static void createFolder(String basePath, String folderName) { + Path folderPath = Paths.get(basePath, folderName); + if (Files.notExists(folderPath)) { + try { + Files.createDirectories(folderPath); + consoleLog(FOLDER_PREFIX + folderName + " was created in [" + basePath + "]."); + } catch (IOException e) { + consoleLog(FOLDER_PREFIX + "Failed to create folder: " + e.getMessage()); + } + } + } + + public static void createJsonFile(String fileName, JSONObject jsonObject, String path) { + if (path == null || fileName == null || jsonObject == null) { + consoleLog(JSON_PREFIX + "Invalid input to createJsonFile."); + return; + } + + Path filePath = Paths.get(path, fileName + ".json"); + if (Files.notExists(filePath)) { + try (FileWriter writer = new FileWriter(filePath.toFile())) { + writer.write(gson.toJson(jsonObject)); + consoleLog(JSON_PREFIX + fileName + ".json was created."); + } catch (IOException e) { + consoleLog(JSON_PREFIX + "Error creating JSON file: " + e.getMessage()); + } + } + } + + public static JSONObject getJsonBody(String fileName, String path) { + if (path == null || fileName == null) { + consoleLog(JSON_PREFIX + "Invalid input to getJsonBody."); + return null; + } + + Path filePath = Paths.get(path, fileName + ".json"); + if (Files.exists(filePath)) { + try (Reader reader = new FileReader(filePath.toFile())) { + JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader); + consoleLog(JSON_PREFIX + fileName + ".json body was loaded."); + return jsonObject; + } catch (IOException | ParseException e) { + consoleLog(JSON_PREFIX + "Error reading JSON file: " + e.getMessage()); + } + } + return null; + } + + public static void updateJsonBody(String fileName, JSONObject jsonObject, String path) { + if (path == null || fileName == null || jsonObject == null) { + consoleLog(JSON_PREFIX + "Invalid input to updateJsonBody."); + return; + } + + Path filePath = Paths.get(path, fileName + ".json"); + if (Files.exists(filePath)) { + try (FileWriter writer = new FileWriter(filePath.toFile())) { + writer.write(gson.toJson(jsonObject)); + consoleLog(JSON_PREFIX + fileName + ".json body was updated."); + } catch (IOException e) { + consoleLog(JSON_PREFIX + "Error updating JSON file: " + e.getMessage()); + } + } else { + consoleLog(JSON_PREFIX + fileName + ".json does not exist to update."); + } + } +} diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 0000000..0e15486 --- /dev/null +++ b/target/classes/META-INF/MANIFEST.MF @@ -0,0 +1,4 @@ +Manifest-Version: 1.0 +Build-Jdk-Spec: 23 +Created-By: Maven Integration for Eclipse + diff --git a/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.properties b/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.properties new file mode 100644 index 0000000..6996ae5 --- /dev/null +++ b/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.properties @@ -0,0 +1,7 @@ +#Generated by Maven Integration for Eclipse +#Sun Jun 08 10:43:04 CEST 2025 +artifactId=JsonManager +groupId=eu.triler.code +m2e.projectLocation=D\:\\Codespace\\JernejTDO\\JsonManager +m2e.projectName=JsonManager +version=1.0 diff --git a/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.xml b/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.xml new file mode 100644 index 0000000..b13bea8 --- /dev/null +++ b/target/classes/META-INF/maven/eu.triler.code/JsonManager/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + eu.triler.code + JsonManager + 1.0 + JsonManager + + 11 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + + + + src/main/resources + true + + + + + + TrilerGit + https://git.triler.eu/api/packages/TrilerCode/maven + + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + com.google.code.gson + gson + 2.9.1 + + + \ No newline at end of file diff --git a/target/classes/eu/triler/code/JsonManager/JsonManager.class b/target/classes/eu/triler/code/JsonManager/JsonManager.class new file mode 100644 index 0000000..4908817 Binary files /dev/null and b/target/classes/eu/triler/code/JsonManager/JsonManager.class differ