@@ -29,46 +29,46 @@ public class BungeeJsonFile {
2929 private Gson gson ;
3030
3131 public BungeeJsonFile (File file ) {
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- }
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+ }
5858 }
5959
6060 @ SuppressWarnings ("deprecation" )
6161 private JsonObject attemptPartialRecovery (File file ) {
62- JsonObject recoveredData = new JsonObject ();
63- try (FileReader fileReader = new FileReader (file );
64- com .google .gson .stream .JsonReader jsonReader = new com .google .gson .stream .JsonReader (fileReader )) {
65- jsonReader .setLenient (true ); // Allow lenient parsing
66- recoveredData = JsonParser .parseReader (jsonReader ).getAsJsonObject ();
67- System .err .println ("Partial recovery of JSON data succeeded." );
68- } catch (Exception ex ) {
69- System .err .println ("Failed to recover JSON data: " + ex .getMessage ());
70- }
71- return recoveredData ;
62+ JsonObject recoveredData = new JsonObject ();
63+ try (FileReader fileReader = new FileReader (file );
64+ com .google .gson .stream .JsonReader jsonReader = new com .google .gson .stream .JsonReader (fileReader )) {
65+ jsonReader .setLenient (true ); // Allow lenient parsing
66+ recoveredData = JsonParser .parseReader (jsonReader ).getAsJsonObject ();
67+ System .err .println ("Partial recovery of JSON data succeeded." );
68+ } catch (Exception ex ) {
69+ System .err .println ("Failed to recover JSON data: " + ex .getMessage ());
70+ }
71+ return recoveredData ;
7272 }
7373
7474 private JsonObject navigateToNode (String path ) {
@@ -160,23 +160,36 @@ public List<String> getStringList(String path, List<String> def) {
160160 }
161161
162162 public List <String > getKeys (String path ) {
163- JsonObject parentNode = navigateToNode (path );
164- String lastPart = getLastPathPart (path );
165- JsonObject node = null ;
166- if (path .split ("\\ ." ).length == 1 ) {
167- node = parentNode ;
168- } else if (parentNode != null ) {
169- node = parentNode .getAsJsonObject (lastPart );
163+ if (path == null || path .isEmpty ()) {
164+ return new ArrayList <>();
170165 }
171166
172- if (node != null ) {
173- List <String > keys = new ArrayList <>();
174- for (Map .Entry <String , JsonElement > entry : node .entrySet ()) {
175- keys .add (entry .getKey ());
167+ String [] parts = path .split ("\\ ." );
168+ String lastPart = parts [parts .length - 1 ];
169+
170+ // Single-part path: look directly under root
171+ if (parts .length == 1 ) {
172+ JsonElement el = conf .get (lastPart );
173+
174+ if (el == null || el .isJsonNull () || !el .isJsonObject ()) {
175+ return new ArrayList <>();
176176 }
177- return keys ;
177+
178+ return new ArrayList <>(el .getAsJsonObject ().keySet ());
179+ }
180+
181+ // Multi-part path: navigate to parent then resolve last part safely
182+ JsonObject parentNode = navigateToNode (path );
183+ if (parentNode == null ) {
184+ return new ArrayList <>();
185+ }
186+
187+ JsonElement el = parentNode .get (lastPart );
188+ if (el == null || el .isJsonNull () || !el .isJsonObject ()) {
189+ return new ArrayList <>();
178190 }
179- return new ArrayList <>();
191+
192+ return new ArrayList <>(el .getAsJsonObject ().keySet ());
180193 }
181194
182195 public JsonElement getNode (String path ) {
@@ -235,16 +248,16 @@ public synchronized void setStringList(String path, List<String> value) {
235248 }
236249
237250 public synchronized void remove (String path ) {
238- if (!path .contains ("." )) {
239- conf .remove (path );
240- return ;
241- }
242-
243- JsonObject node = navigateToNode (path );
244- if (node != null ) {
245- String lastPart = getLastPathPart (path );
246- node .remove (lastPart );
247- }
251+ if (!path .contains ("." )) {
252+ conf .remove (path );
253+ return ;
254+ }
255+
256+ JsonObject node = navigateToNode (path );
257+ if (node != null ) {
258+ String lastPart = getLastPathPart (path );
259+ node .remove (lastPart );
260+ }
248261 }
249262
250263 public void reload () {
0 commit comments