added json member type checking for json utils, won't die if a string is found where an array was expected

closes #637
pull/653/head
Justin Richer 2014-07-20 09:42:57 -07:00
parent 325a200f16
commit 62a43165f0
1 changed files with 12 additions and 2 deletions

View File

@ -24,6 +24,8 @@ import java.util.Date;
import java.util.List;
import java.util.Set;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -141,7 +143,11 @@ public class JsonUtils {
*/
public static Set<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
if (o.has(member)) {
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
if (o.get(member).isJsonArray()) {
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
} else {
return Sets.newHashSet(o.get(member).getAsString());
}
} else {
return null;
}
@ -152,7 +158,11 @@ public class JsonUtils {
*/
public static List<String> getAsStringList(JsonObject o, String member) throws JsonSyntaxException {
if (o.has(member)) {
return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.getType());
if (o.get(member).isJsonArray()) {
return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.getType());
} else {
return Lists.newArrayList(o.get(member).getAsString());
}
} else {
return null;
}