11package com .bencodez .simpleapi .file ;
22
3+ import java .io .File ;
4+ import java .io .FileReader ;
5+ import java .io .FileWriter ;
6+ import java .io .IOException ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
9+ import java .util .Map ;
10+
311import com .google .gson .Gson ;
412import com .google .gson .GsonBuilder ;
513import com .google .gson .JsonArray ;
614import com .google .gson .JsonElement ;
715import com .google .gson .JsonObject ;
816import com .google .gson .JsonParser ;
17+
918import lombok .Getter ;
1019import lombok .Setter ;
1120
12- import java .io .File ;
13- import java .io .FileReader ;
14- import java .io .FileWriter ;
15- import java .io .IOException ;
16- import java .util .ArrayList ;
17- import java .util .List ;
18- import java .util .Map ;
19-
2021public class BungeeJsonFile {
2122 @ Getter
2223 @ Setter
@@ -28,28 +29,45 @@ public class BungeeJsonFile {
2829 private Gson gson ;
2930
3031 public BungeeJsonFile (File file ) {
31- this .file = file ;
32- this .gson = new GsonBuilder ().setPrettyPrinting ().create ();
33-
34- if (!file .exists ()) {
35- try {
36- File parentDir = file .getParentFile ();
37- if (parentDir != null && !parentDir .exists ()) {
38- parentDir .mkdirs ();
39- }
40- file .createNewFile ();
41- conf = new JsonObject ();
42- save (); // Save file with empty JsonObject upon creation
43- } catch (IOException e ) {
44- e .printStackTrace ();
45- }
46- } else {
47- try (FileReader reader = new FileReader (file )) {
48- conf = JsonParser .parseReader (reader ).getAsJsonObject ();
49- } catch (IOException e ) {
50- e .printStackTrace ();
51- }
52- }
32+ this .file = file ;
33+ this .gson = new GsonBuilder ().setPrettyPrinting ().create ();
34+
35+ if (!file .exists ()) {
36+ try {
37+ File parentDir = file .getParentFile ();
38+ if (parentDir != null && !parentDir .exists ()) {
39+ parentDir .mkdirs ();
40+ }
41+ file .createNewFile ();
42+ conf = new JsonObject ();
43+ save (); // Save file with empty JsonObject upon creation
44+ } catch (IOException e ) {
45+ e .printStackTrace ();
46+ }
47+ } else {
48+ try (FileReader reader = new FileReader (file )) {
49+ conf = JsonParser .parseReader (reader ).getAsJsonObject ();
50+ } catch (com .google .gson .JsonSyntaxException e ) {
51+ System .err .println ("Error parsing JSON file: " + e .getMessage ());
52+ conf = attemptPartialRecovery (file ); // Attempt to recover as much as possible
53+ } catch (IOException e ) {
54+ e .printStackTrace ();
55+ conf = new JsonObject (); // Fallback to an empty JsonObject
56+ }
57+ }
58+ }
59+
60+ private JsonObject attemptPartialRecovery (File file ) {
61+ JsonObject recoveredData = new JsonObject ();
62+ try (FileReader fileReader = new FileReader (file );
63+ com .google .gson .stream .JsonReader jsonReader = new com .google .gson .stream .JsonReader (fileReader )) {
64+ jsonReader .setLenient (true ); // Allow lenient parsing
65+ recoveredData = JsonParser .parseReader (jsonReader ).getAsJsonObject ();
66+ System .err .println ("Partial recovery of JSON data succeeded." );
67+ } catch (Exception ex ) {
68+ System .err .println ("Failed to recover JSON data: " + ex .getMessage ());
69+ }
70+ return recoveredData ;
5371 }
5472
5573 private JsonObject navigateToNode (String path ) {
0 commit comments