added json member type checking for json utils, won't die if a string is found where an array was expected
closes #637pull/653/head
parent
325a200f16
commit
62a43165f0
|
@ -24,6 +24,8 @@ import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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.Gson;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
@ -141,7 +143,11 @@ public class JsonUtils {
|
||||||
*/
|
*/
|
||||||
public static Set<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
|
public static Set<String> getAsStringSet(JsonObject o, String member) throws JsonSyntaxException {
|
||||||
if (o.has(member)) {
|
if (o.has(member)) {
|
||||||
|
if (o.get(member).isJsonArray()) {
|
||||||
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
|
return gson.fromJson(o.get(member), new TypeToken<Set<String>>(){}.getType());
|
||||||
|
} else {
|
||||||
|
return Sets.newHashSet(o.get(member).getAsString());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +158,11 @@ public class JsonUtils {
|
||||||
*/
|
*/
|
||||||
public static List<String> getAsStringList(JsonObject o, String member) throws JsonSyntaxException {
|
public static List<String> getAsStringList(JsonObject o, String member) throws JsonSyntaxException {
|
||||||
if (o.has(member)) {
|
if (o.has(member)) {
|
||||||
|
if (o.get(member).isJsonArray()) {
|
||||||
return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.getType());
|
return gson.fromJson(o.get(member), new TypeToken<List<String>>(){}.getType());
|
||||||
|
} else {
|
||||||
|
return Lists.newArrayList(o.get(member).getAsString());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue