From 26d1e76fe16cce1e736565f20bcce96f8476aac3 Mon Sep 17 00:00:00 2001 From: Pahimar Date: Fri, 20 May 2016 23:12:40 -0400 Subject: [PATCH] Better protection of parsing values from json --- .../serialize/EnergyValueMapSerializer.java | 2 +- .../util/serialize/ItemStackSerializer.java | 39 ++++++++----------- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/pahimar/ee3/util/serialize/EnergyValueMapSerializer.java b/src/main/java/com/pahimar/ee3/util/serialize/EnergyValueMapSerializer.java index 102e1f70..c6e31d2e 100644 --- a/src/main/java/com/pahimar/ee3/util/serialize/EnergyValueMapSerializer.java +++ b/src/main/java/com/pahimar/ee3/util/serialize/EnergyValueMapSerializer.java @@ -42,7 +42,7 @@ public class EnergyValueMapSerializer implements JsonSerializer, JsonDeser int metaValue = 0; NBTTagCompound tagCompound = null; - try { - if (jsonObject.has(NAME) && jsonObject.get(NAME).getAsJsonPrimitive().isString()) { - name = jsonObject.get(NAME).getAsString(); + if (jsonObject.has(NAME) && jsonObject.get(NAME).isJsonPrimitive()) { + name = jsonObject.getAsJsonPrimitive(NAME).getAsString(); + } + + if (jsonObject.has(META_VALUE) && jsonObject.get(META_VALUE).isJsonPrimitive()) { + try { + metaValue = jsonObject.getAsJsonPrimitive(META_VALUE).getAsInt(); + } + catch (NumberFormatException e) { + // TODO Logging } } - catch (IllegalStateException exception) { - // TODO We could probably log here that an invalid piece of data was found - } - try { - if (jsonObject.has(META_VALUE) && jsonObject.get(META_VALUE).getAsJsonPrimitive().isNumber()) { - metaValue = jsonObject.get(META_VALUE).getAsInt(); - } - } - catch (IllegalStateException exception) { - // TODO We could probably log here that an invalid piece of data was found - } + if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).isJsonPrimitive()) { - try { - if (jsonObject.has(TAG_COMPOUND) && jsonObject.get(TAG_COMPOUND).getAsJsonPrimitive().isString()) { - - NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.get(TAG_COMPOUND).getAsString()); + try { + NBTBase nbtBase = JsonToNBT.func_150315_a(jsonObject.getAsJsonPrimitive(TAG_COMPOUND).getAsString()); if (nbtBase instanceof NBTTagCompound) { tagCompound = (NBTTagCompound) nbtBase; } } - } - catch (NBTException e) { - } - catch (IllegalStateException exception) { - // TODO We could probably log here that an invalid piece of data was found + catch (NBTException e) { + // TODO Logging + } } if (name != null) {