use of com.google.gson.JsonObject in project iosched by google.
the class RemoteJsonHelper method fetchJsonFromPublicURL.
public static JsonObject fetchJsonFromPublicURL(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 30 seconds
connection.setReadTimeout(1000 * 30);
int response = connection.getResponseCode();
if (response < 200 || response >= 300) {
throw new IllegalArgumentException("Unexpected HTTP response [" + response + "] at URL: " + urlStr);
}
InputStream stream = connection.getInputStream();
JsonReader reader = new JsonReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
return (JsonObject) new JsonParser().parse(reader);
}
use of com.google.gson.JsonObject in project iosched by google.
the class DataCheck method clone.
private JsonObject clone(JsonObject source) {
JsonObject dest = new JsonObject();
for (Map.Entry<String, JsonElement> entry : source.entrySet()) {
JsonArray values = entry.getValue().getAsJsonArray();
JsonArray cloned = new JsonArray();
cloned.addAll(values);
dest.add(entry.getKey(), cloned);
}
return dest;
}
use of com.google.gson.JsonObject in project iosched by google.
the class DataCheck method checkUsingPredicator.
public void checkUsingPredicator(CheckResult result, JsonObject oldData, JsonObject newData, Enum<?> entityType, Enum<?> entityKey, EntityValidator predicate) {
HashMap<String, JsonObject> oldMap = new HashMap<String, JsonObject>();
HashMap<String, JsonObject> newMap = new HashMap<String, JsonObject>();
JsonArray oldArray = getAsArray(oldData, entityType);
JsonArray newArray = getAsArray(newData, entityType);
if (oldArray != null)
for (JsonElement el : oldArray) {
JsonObject obj = (JsonObject) el;
oldMap.put(get(obj, entityKey).getAsString(), obj);
}
if (newArray != null)
for (JsonElement el : newArray) {
JsonObject obj = (JsonObject) el;
newMap.put(get(obj, entityKey).getAsString(), obj);
}
for (String id : oldMap.keySet()) {
predicate.evaluate(result, entityType.name(), oldMap.get(id), newMap.get(id));
}
}
use of com.google.gson.JsonObject in project iosched by google.
the class VendorDynamicInput method fetchArray.
public JsonArray fetchArray(InputJsonKeys.VendorAPISource.MainTypes entityType, int page) throws IOException {
HashMap<String, String> params = null;
if (entityType.equals(InputJsonKeys.VendorAPISource.MainTypes.topics) || entityType.equals(InputJsonKeys.VendorAPISource.MainTypes.speakers)) {
params = new HashMap<String, String>();
// Topics and speakers require param "includeinfo=true" to bring extra data
params.put("includeinfo", "true");
if (entityType.equals(InputJsonKeys.VendorAPISource.MainTypes.topics)) {
if (extractUnpublished) {
params.put("minpublishstatus", "0");
}
}
}
if (page == 0) {
page = 1;
} else if (page > 1) {
if (params == null) {
params = new HashMap<String, String>();
}
params.put("page", Integer.toString(page));
}
JsonElement element = getFetcher().fetch(entityType, params);
if (element.isJsonArray()) {
return element.getAsJsonArray();
} else if (element.isJsonObject()) {
// check if there are extra pages requiring further fetching
JsonObject obj = element.getAsJsonObject();
checkPagingConsistency(entityType, page, obj);
int pageSize = obj.get("pagesize").getAsInt();
int totalEntities = obj.get("total").getAsInt();
JsonArray elements = getEntities(obj);
if (page * pageSize < totalEntities) {
// fetch the next page
elements.addAll(fetchArray(entityType, page + 1));
}
return elements;
} else {
throw new JsonParseException("Invalid response from Vendor API. Request should return " + "either a JsonArray or a JsonObject, but returned " + element.getClass().getName() + ". Entity fetcher is " + getFetcher());
}
}
use of com.google.gson.JsonObject in project iosched by google.
the class DataExtractorTest method testHasMainTag.
@Test
public void testHasMainTag() {
JsonObject newData = new DataExtractor(false).extractFromDataSources(sources);
JsonElement mainTag = newData.get(OutputJsonKeys.MainTypes.sessions.name()).getAsJsonArray().get(0).getAsJsonObject().get(OutputJsonKeys.Sessions.mainTag.name());
assertNotNull(mainTag);
assertTrue(mainTag.getAsString().startsWith("TOPIC"));
}
Aggregations