From 646ddc54c0052ececfd40a18a968dda29fcf4617 Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Fri, 5 Sep 2025 15:43:51 +0200 Subject: [PATCH] first commit --- .gitignore | 38 +++++++++++++++++++++++++++++++++++ .idea/.gitignore | 8 ++++++++ .idea/encodings.xml | 7 +++++++ .idea/misc.xml | 14 +++++++++++++ pom.xml | 30 +++++++++++++++++++++++++++ src/main/java/JSONReader.java | 29 ++++++++++++++++++++++++++ src/main/java/Main.java | 10 +++++++++ src/main/resources/test.json | 6 ++++++ 8 files changed, 142 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 pom.xml create mode 100644 src/main/java/JSONReader.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/resources/test.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7ace097 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..fa403e0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.example + ReaderDemo + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + org.json + json + 20250517 + + + com.fasterxml.jackson.core + jackson-databind + 2.20.0 + + + + \ No newline at end of file diff --git a/src/main/java/JSONReader.java b/src/main/java/JSONReader.java new file mode 100644 index 0000000..c020ba8 --- /dev/null +++ b/src/main/java/JSONReader.java @@ -0,0 +1,29 @@ +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import org.json.JSONObject; + +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +public class JSONReader { + + public static String read(String filePath) { + Path path = Path.of(filePath); + try { + String content = Files.readString(path, StandardCharsets.UTF_8); + JSONObject jsonObject = new JSONObject(content); + return toPrettyJsonString(jsonObject); + } catch (Exception e) { + throw new RuntimeException("Erreur lors de la lecture du fichier JSON", e); + } + } + + public static String toPrettyJsonString(JSONObject json) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + Object jsonObject = mapper.readValue(json.toString(), Object.class); + ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); + return writer.writeValueAsString(jsonObject); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..fd9de03 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,10 @@ +import com.fasterxml.jackson.core.JsonProcessingException; + +public class Main { + + public static void main(String[] args) throws JsonProcessingException { + + String json = JSONReader.read("src/main/resources/test.json"); + System.out.println(json); + } +} diff --git a/src/main/resources/test.json b/src/main/resources/test.json new file mode 100644 index 0000000..32031da --- /dev/null +++ b/src/main/resources/test.json @@ -0,0 +1,6 @@ +{ + "nom": "Villageois", + "description": "Un villageois est un habitant d'un village, souvent impliqué dans des activités agricoles ou artisanales.", + "imgURL": "https://example.com/images/villageois.png", + "categorie": "Personnage" +} \ No newline at end of file