Safer JSON Loading

By: Suddenly <suddenly@suddenly.coffee>
This commit is contained in:
CraftBukkit/Spigot 2014-07-08 09:44:18 +10:00
parent c4d2bdfd85
commit 0076297721
2 changed files with 24 additions and 2 deletions

View file

@ -53,7 +53,19 @@
Objects.requireNonNull(list); Objects.requireNonNull(list);
optional.ifPresent(list::add); optional.ifPresent(list::add);
@@ -261,7 +261,7 @@ @@ -250,6 +250,11 @@
}
} catch (FileNotFoundException filenotfoundexception) {
;
+ // Spigot Start
+ } catch (com.google.gson.JsonSyntaxException | NullPointerException ex) {
+ GameProfileCache.LOGGER.warn( "Usercache.json is corrupted or has bad formatting. Deleting it to prevent further issues." );
+ this.file.delete();
+ // Spigot End
} catch (JsonParseException | IOException ioexception) {
GameProfileCache.LOGGER.warn("Failed to load profile cache {}", this.file, ioexception);
}
@@ -261,7 +266,7 @@
JsonArray jsonarray = new JsonArray(); JsonArray jsonarray = new JsonArray();
DateFormat dateformat = GameProfileCache.createDateFormat(); DateFormat dateformat = GameProfileCache.createDateFormat();

View file

@ -44,7 +44,7 @@
JsonObject jsonobject = new JsonObject(); JsonObject jsonobject = new JsonObject();
Objects.requireNonNull(jsonlistentry); Objects.requireNonNull(jsonlistentry);
@@ -171,7 +172,7 @@ @@ -171,9 +172,17 @@
StoredUserEntry<K> jsonlistentry = this.createEntry(jsonobject); StoredUserEntry<K> jsonlistentry = this.createEntry(jsonobject);
if (jsonlistentry.getUser() != null) { if (jsonlistentry.getUser() != null) {
@ -52,4 +52,14 @@
+ this.map.put(this.getKeyForUser(jsonlistentry.getUser()), (V) jsonlistentry); // CraftBukkit - decompile error + this.map.put(this.getKeyForUser(jsonlistentry.getUser()), (V) jsonlistentry); // CraftBukkit - decompile error
} }
} }
+ // Spigot Start
+ } catch ( com.google.gson.JsonParseException | NullPointerException ex )
+ {
+ org.bukkit.Bukkit.getLogger().log( java.util.logging.Level.WARNING, "Unable to read file " + this.file + ", backing it up to {0}.backup and creating new copy.", ex );
+ File backup = new File( this.file + ".backup" );
+ this.file.renameTo( backup );
+ this.file.delete();
+ // Spigot End
} catch (Throwable throwable) { } catch (Throwable throwable) {
if (bufferedreader != null) {
try {