|
|
@ -21,6 +21,7 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Thin wrapper to adapt {@link Gson} to a {@link JsonParser}.
|
|
|
|
* Thin wrapper to adapt {@link Gson} to a {@link JsonParser}.
|
|
|
@ -35,24 +36,24 @@ public class GsonJsonParser implements JsonParser {
|
|
|
|
private Gson gson = new GsonBuilder().create();
|
|
|
|
private Gson gson = new GsonBuilder().create();
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
|
|
public Map<String, Object> parseMap(String json) {
|
|
|
|
public Map<String, Object> parseMap(String json) {
|
|
|
|
if (json != null) {
|
|
|
|
if (json != null) {
|
|
|
|
json = json.trim();
|
|
|
|
json = json.trim();
|
|
|
|
if (json.startsWith("{")) {
|
|
|
|
if (json.startsWith("{")) {
|
|
|
|
return this.gson.fromJson(json, Map.class);
|
|
|
|
TypeToken<Map<String, Object>> type = new TypeToken<Map<String, Object>>() { };
|
|
|
|
|
|
|
|
return this.gson.fromJson(json, type.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Cannot parse JSON");
|
|
|
|
throw new IllegalArgumentException("Cannot parse JSON");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
|
|
|
public List<Object> parseList(String json) {
|
|
|
|
public List<Object> parseList(String json) {
|
|
|
|
if (json != null) {
|
|
|
|
if (json != null) {
|
|
|
|
json = json.trim();
|
|
|
|
json = json.trim();
|
|
|
|
if (json.startsWith("[")) {
|
|
|
|
if (json.startsWith("[")) {
|
|
|
|
return this.gson.fromJson(json, List.class);
|
|
|
|
TypeToken<List<Object>> type = new TypeToken<List<Object>>() { };
|
|
|
|
|
|
|
|
return this.gson.fromJson(json, type.getType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Cannot parse JSON");
|
|
|
|
throw new IllegalArgumentException("Cannot parse JSON");
|
|
|
|