Search in sources :

Example 91 with JsonObject

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) JsonParser(com.google.gson.JsonParser)

Example 92 with JsonObject

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;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 93 with JsonObject

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));
    }
}
Also used : JsonArray(com.google.gson.JsonArray) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 94 with JsonObject

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());
    }
}
Also used : JsonArray(com.google.gson.JsonArray) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException)

Example 95 with JsonObject

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"));
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (com.google.gson.JsonObject)1417 JsonElement (com.google.gson.JsonElement)389 JsonArray (com.google.gson.JsonArray)293 JsonParser (com.google.gson.JsonParser)285 JsonPrimitive (com.google.gson.JsonPrimitive)137 Gson (com.google.gson.Gson)91 Test (org.junit.Test)81 HashMap (java.util.HashMap)79 Map (java.util.Map)78 ArrayList (java.util.ArrayList)77 IOException (java.io.IOException)66 Test (org.testng.annotations.Test)61 InputStreamReader (java.io.InputStreamReader)38 JsonParseException (com.google.gson.JsonParseException)27 File (java.io.File)25 List (java.util.List)21 HttpResponse (org.apache.http.HttpResponse)21 JsonReader (com.google.gson.stream.JsonReader)19 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 InputStream (java.io.InputStream)19