Better handle invalid json

This commit is contained in:
Magnus Ulf 2020-01-14 14:17:10 +01:00
parent df3a3569f5
commit c5871576ef
4 changed files with 19 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.Collection; import java.util.Collection;
public class MoneyMixinVault extends MoneyMixinAbstract public class MoneyMixinVault extends MoneyMixinAbstract
{ {
// -------------------------------------------- // // -------------------------------------------- //
// INSTANCE & CONSTRUCT // INSTANCE & CONSTRUCT
@ -255,8 +255,8 @@ public class MoneyMixinVault extends MoneyMixinAbstract
public static Object getEconomyObject(String accountId) public static Object getEconomyObject(String accountId)
{ {
if (null == accountId) return null; if (null == accountId) return null;
// Because of Factions we have this crazy-workaround. // Because of Factions we have this crazy-workaround
// Where offlineplayers will be used when possible // where offlineplayers will be used when possible
// but otherwise names will. // but otherwise names will.
OfflinePlayer ret = IdUtil.getOfflinePlayer(accountId); OfflinePlayer ret = IdUtil.getOfflinePlayer(accountId);
if (ret != null) return ret; if (ret != null) return ret;

View File

@ -14,7 +14,6 @@ import com.massivecraft.massivecore.mixin.MixinModification;
import com.massivecraft.massivecore.store.migrator.MigratorUtil; import com.massivecraft.massivecore.store.migrator.MigratorUtil;
import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
import com.mongodb.DB;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -384,6 +383,11 @@ public class Coll<E extends Entity<E>> extends CollAbstract<E>
this.logLoadError(id, "Raw data was JSON null. It seems you have a file containing just the word \"null\". Why would you do this?"); this.logLoadError(id, "Raw data was JSON null. It seems you have a file containing just the word \"null\". Why would you do this?");
return false; return false;
} }
if (raw == MStore.INVALID_JSON)
{
this.logLoadError(id, "Does not contain valid json.");
return false;
}
return true; return true;
} }

View File

@ -3,6 +3,7 @@ package com.massivecraft.massivecore.store;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveMap;
import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.collections.MassiveSet;
import com.massivecraft.massivecore.util.DiscUtil; import com.massivecraft.massivecore.util.DiscUtil;
@ -172,7 +173,14 @@ public class DriverFlatfile extends DriverAbstract
content = content.trim(); content = content.trim();
if (content.length() == 0) return null; if (content.length() == 0) return null;
return new JsonParser().parse(content); try
{
return new JsonParser().parse(content);
}
catch (JsonSyntaxException ex)
{
return MStore.INVALID_JSON;
}
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package com.massivecraft.massivecore.store; package com.massivecraft.massivecore.store;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.massivecraft.massivecore.ConfServer; import com.massivecraft.massivecore.ConfServer;
import com.massivecraft.massivecore.entity.MassiveCoreMConf; import com.massivecraft.massivecore.entity.MassiveCoreMConf;
@ -18,6 +19,7 @@ public class MStore
// This class also serves the purpose of containing database related constants. // This class also serves the purpose of containing database related constants.
public static final boolean DEBUG_ENABLED = false; public static final boolean DEBUG_ENABLED = false;
public static final JsonObject INVALID_JSON = new JsonObject();
// -------------------------------------------- // // -------------------------------------------- //
// DRIVER REGISTRY // DRIVER REGISTRY