Example usage for com.google.gson.reflect TypeToken TypeToken

List of usage examples for com.google.gson.reflect TypeToken TypeToken

Introduction

In this page you can find the example usage for com.google.gson.reflect TypeToken TypeToken.

Prototype

@SuppressWarnings("unchecked")
protected TypeToken() 

Source Link

Document

Constructs a new type literal.

Usage

From source file:ca.ualberta.app.ESmanager.QuestionListManager.java

License:Apache License

/**
 * Load a question form online server//from   ww  w  .ja v  a  2  s.com
 * 
 * @param response
 *            The online server connection response.
 * 
 * @return null.
 */
private SearchHit<Question> parseQuestionHit(HttpResponse response) {

    try {
        String json = getEntityContent(response);
        Type searchHitType = new TypeToken<SearchHit<Question>>() {
        }.getType();

        SearchHit<Question> sr = gson.fromJson(json, searchHitType);
        return sr;
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:ca.ualberta.app.ESmanager.QuestionListManager.java

License:Apache License

/**
 * Parses the response of a search/*from www . j  ava 2  s  .  c om*/
 * 
 * @param response
 *            The online server connection response.
 * 
 * @return esResponse The parsed response of a search.
 * 
 * @throws IOException
 */
private SearchResponse<Question> parseSearchResponse(HttpResponse response) throws IOException {
    String json;
    json = getEntityContent(response);

    Type searchResponseType = new TypeToken<SearchResponse<Question>>() {
    }.getType();

    SearchResponse<Question> esResponse = gson.fromJson(json, searchResponseType);

    return esResponse;
}

From source file:ca.ualberta.cmput301.as1.czervos_notes.CounterListActivity.java

License:Apache License

/**
 * Loads the stored array list and returns the counterListModel
 * with the loaded counters. If there is nothing saved, returns null.
 * @return counterListModel containing counters OR null
 *///from  w  w w  . j a  v a2 s  .  co  m
public CounterListModel loadCounterList() {
    Gson gson = new Gson();
    CounterListModel counterListModel = new CounterListModel();
    SharedPreferences savedList = getPreferences(MODE_PRIVATE);
    // Loads the JSON string object saved as "savedList" and returns
    // "noValue" if no JSON string object located 
    String json = savedList.getString("savedList", "noValue");
    if (json == "noValue") {
        return null;
    }

    // Sets collection type to ArrayList<CounterModel>
    java.lang.reflect.Type collectionType = new TypeToken<ArrayList<CounterModel>>() {
    }.getType();
    // Converts JSON string object to ArrayList<CounterModel>
    ArrayList<CounterModel> loadedList = gson.fromJson(json, collectionType);
    // Puts loaded list into the counter list model
    counterListModel.setCounterList(loadedList);
    return counterListModel;
}

From source file:ca.ualberta.cmput301f12t05.ufill.Webservicer.java

License:Apache License

/**
 * Consumes the REMOVE operation of the service
 * Once a local bin has been deleted, it is removed from the webservice as well.
 * //from   w w w . ja v  a  2 s  .c  o  m
 * @return void
 * @throws Exception
 */
public List<Map<String, String>> getBinList() throws Exception {
    String jsonEntryList = listBins();
    Type listType = new TypeToken<List<Map<String, String>>>() {
    }.getType();
    List<Map<String, String>> BinList = gson.fromJson(jsonEntryList, listType);
    //System.out.println("The bin list " + BinList);
    return BinList;
}

From source file:ca.ualberta.cmput301f13t13.storyhoard.serverClasses.ESRetrieval.java

License:Apache License

/**
 * Searches for a response on the server by id. If no response matching the
 * given id is found, the value returned is null. Note also that in this
 * application, the id's will be strings in the format of a UUID. The id's
 * will also be corresponding to story id's, and the responses' content
 * are story objects in JSON string format.
 * /*from  w  w  w.  j  a  v  a2s. co  m*/
 * </br></br>
 * An example call:
 * </br></br>
 *     *          String server = "http://cmput301.softwareprocess.es:8080/cmput301f13t13/stories/" </br>
 *  UUID id = 5231b533-ba17-4787-98a3-f2df37de2aD7; </br> 
 *  Story myStory = searchById(id.toString()); </br></br> 
 *  
 *  myStory will contain the story that was searched for, or null if it  
 *  didn't exist on the server.
 *  
 * @param server
 *          The location on elastic search to search for the responses.
 *          It expects this information as a String.</br>
 *          See above for an example of a valid server string.
 * @param id 
 *          Will be a 128-bit UUID value that was converted to a String.
 *          These are unique identifiers of stories.
 */
protected Story searchById(String id, String server) {
    Story story = null;
    try {
        HttpGet getRequest = new HttpGet(server + id + "?pretty=1");
        getRequest.addHeader("Accept", "application/json");
        HttpResponse response;
        response = httpclient.execute(getRequest);
        String status = response.getStatusLine().toString();
        System.out.println(status);
        String json = getEntityContent(response);
        Type simpleESResponseType = new TypeToken<SimpleESResponse<Story>>() {
        }.getType();
        SimpleESResponse<Story> esResponse = gson.fromJson(json, simpleESResponseType);
        story = esResponse.getSource();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return story;
}

From source file:ca.ualberta.cmput301f13t13.storyhoard.serverClasses.ESRetrieval.java

License:Apache License

/**
 * This retrieval method can retrieve multiple stories from the server.
 * It also allows for two different types of searching. The first, 
 * by using a provided query. The second, if no query is provided (in which
 * case the value for the argument will be null) is searching for all 
 * stories available on the server. </br></br>
 * //w  w  w  .  j  av a 2  s. c  o m
 * Although the query provided could be anything,and the following is
 * an example of a query that can be provided. This one searches for
 * all stories on the server who have contain the specified keywords in
 * their titles. </br></br>
 * 
 * {"query" : {"query_string" : {"default_field" : "title", "query" : "ugly AND red"}}}
 *    
 * </br></br>
 * Note that like in the method above, the string for the server we are
 * searching in must also be provided. A full example call would be: </br></br>
 * 
 * String server = "http://cmput301.softwareprocess.es:8080/cmput301f13t13/stories/" </br>
 * String query = "{\"query\" : {\"query_string\" : {\"default_field\""
    + " : \"title\",\"query\" : \"dog AND cat"\"}}}";   </br>
 * ArrayList<Story> stories = retrieve(query, server); </br></br>
 * 
 * stories will contain all stories that contained both the words "dog"
 * and "cat" in their titles. </br></br>
 * 
 * Here is an example of what you would call to retrieve all the stories on
 * the server.
 * 
 * String server = "http://cmput301.softwareprocess.es:8080/cmput301f13t13/stories/" </br>
 * ArrayList<Story> stories = retrieve(null, server); </br></br>
 * 
 * 
 * @param query 
 *          The query as a string. Watch out for escape characters. It can
 *          be null if you want to get all the stories on the server.
 * @param server
 *          The location on elastic search to search for the responses.
 *          It expects this information as a String.</br>
 */
protected ArrayList<Story> retrieve(String query, String server) throws ClientProtocolException, IOException {
    ArrayList<Story> stories = new ArrayList<Story>();
    HttpPost searchRequest = new HttpPost(server + "_search?pretty=1");
    if (query != null) {
        StringEntity stringentity = new StringEntity(query);
        searchRequest.setEntity(stringentity);
    }
    searchRequest.setHeader("Accept", "application/json");
    HttpResponse response;
    response = httpclient.execute(searchRequest);
    String status = response.getStatusLine().toString();
    System.out.println(status);
    String json = getEntityContent(response);
    Type elasticSearchSearchResponseType = new TypeToken<ElasticSearchResponse<Story>>() {
    }.getType();
    ElasticSearchResponse<Story> esResponse = gson.fromJson(json, elasticSearchSearchResponseType);
    System.err.println(esResponse);
    for (SimpleESResponse<Story> r : esResponse.getHits()) {
        Story story = r.getSource();
        stories.add(story);
    }
    return stories;
}

From source file:ca.ualberta.cmput301w13t11.FoodBook.model.ClientHelper.java

License:Creative Commons License

/**
 * After executing a search on the server, this method is called to 
 * transform the search results into a list of recipes.
 * @param response// w  ww .  ja v a 2s . co  m
 * @return 
 * @throws IOException
 */
public ArrayList<Recipe> toRecipeList(String json) throws IOException {

    ServerSearchResponse<ServerRecipe> search_response;
    ArrayList<Recipe> search_results = new ArrayList<Recipe>();

    Type serverSearchResponseType = new TypeToken<ServerSearchResponse<ServerRecipe>>() {
    }.getType();
    try {
        search_response = gson.fromJson(json, serverSearchResponseType);
    } catch (JsonSyntaxException jse) {
        return search_results;
    }

    for (ServerResponse<ServerRecipe> sr : search_response.getHits()) {
        ServerRecipe server_recipe = sr.getSource();
        search_results.add(ServerRecipe.toRecipe(server_recipe));
    }
    return search_results;

}

From source file:ca.ualberta.cmput301w14t08.geochan.managers.CacheManager.java

License:Apache License

/**
 * Deserializes the comment queue from JSON.
 * @return An ArrayList of the deserialized Comments.
 *///from   w  w  w . ja  v  a 2 s .  c o m
public ArrayList<Comment> deserializeCommentQueue() {
    ArrayList<Comment> list = new ArrayList<Comment>();
    try {
        FileInputStream f = context.openFileInput(FILENAME2);
        BufferedReader r = new BufferedReader(new InputStreamReader(f));
        String json = "";
        String temp = "";
        temp = r.readLine();
        while (temp != null) {
            json = json + temp;
            temp = r.readLine();
        }
        r.close();
        f.close();
        Type type = new TypeToken<ArrayList<Comment>>() {
        }.getType();
        list = onlineGson.fromJson(json, type);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return list;
}

From source file:ca.ualberta.cmput301w14t08.geochan.managers.CacheManager.java

License:Apache License

/**
 * Deserializes the ThreadComment queue from JSON.
 * @return An ArrayList of the deserialized ThreadComments.
 *//*w w w .  j  a va2s  .c o m*/
public ArrayList<ThreadComment> deserializeThreadCommentQueue() {
    ArrayList<ThreadComment> list = new ArrayList<ThreadComment>();
    try {
        FileInputStream f = context.openFileInput(FILENAME3);
        BufferedReader r = new BufferedReader(new InputStreamReader(f));
        String json = "";
        String temp = "";
        temp = r.readLine();
        while (temp != null) {
            json = json + temp;
            temp = r.readLine();
        }
        r.close();
        f.close();
        Type type = new TypeToken<ArrayList<ThreadComment>>() {
        }.getType();
        list = onlineGson.fromJson(json, type);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return list;
}

From source file:ca.ualberta.cmput301w14t08.geochan.managers.CacheManager.java

License:Apache License

/**
 * Deserialize a list of ThreadComment objects without comments.
 * /*from  w ww  .ja  v a 2  s  .  co m*/
 * @return The ArrayList of ThreadComments.
 */
public ArrayList<ThreadComment> deserializeThreadList() {
    ArrayList<ThreadComment> list = new ArrayList<ThreadComment>();
    try {
        FileInputStream f = context.openFileInput(FILENAME);
        BufferedReader r = new BufferedReader(new InputStreamReader(f));
        String json = "";
        String temp = "";
        temp = r.readLine();
        while (temp != null) {
            json = json + temp;
            temp = r.readLine();
        }
        r.close();
        f.close();
        Type type = new TypeToken<ArrayList<ThreadComment>>() {
        }.getType();
        list = onlineGson.fromJson(json, type);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return list;
}