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.controller.CacheController.java

License:Apache License

/**
 * Load the question Map's from the file with given name.
 * /*from   w  w w.j  av  a2s.  c  o m*/
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the Map of the question(s).
 */
public Map<Long, Question> loadMapFromFile(Context context, String FILENAME) {
    Map<Long, Question> questionMap = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<Map<Long, Question>>() {
        }.getType();
        questionMap = gson.fromJson(in, listType);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (questionMap == null)
        return questionMap = new HashMap<Long, Question>();
    return questionMap;
}

From source file:ca.ualberta.app.controller.CacheController.java

License:Apache License

/**
 * Load the question Map's from the file with given name.
 * /*w ww.j a v a 2  s  . c  om*/
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the Map of the question(s).
 */
public String loadStringFromFile(Context context, String FILENAME) {
    String location = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<String>() {
        }.getType();
        location = gson.fromJson(in, listType);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (location == null)
        return location = new String();
    return location;
}

From source file:ca.ualberta.app.controller.PushController.java

License:Apache License

/**
 * Load the ID's from the file with given name.
 * /*from  www  .j a  v  a 2 s  . c  o m*/
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the ID.
 */
public ArrayList<Long> loadIdFromFile(Context context, String FILENAME) {
    ArrayList<Long> questionId = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<ArrayList<Long>>() {
        }.getType();
        questionId = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (questionId == null)
        return questionId = new ArrayList<Long>();
    return questionId;
}

From source file:ca.ualberta.app.controller.PushController.java

License:Apache License

/**
 * Load the question Map's from the file with given name.
 * //from  w  ww . j a  va 2 s.c o  m
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the Map of the question(s).
 */
public Map<Long, Question> loadMapFromFile(Context context, String FILENAME) {
    Map<Long, Question> questionMap = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<Map<Long, Question>>() {
        }.getType();
        questionMap = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (questionMap == null)
        return questionMap = new HashMap<Long, Question>();
    return questionMap;
}

From source file:ca.ualberta.app.controller.PushController.java

License:Apache License

/**
 * Load the answer Map's from the file with given name.
 * /*from   ww w.  j  a v a  2 s.  c o m*/
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the Map of the answer(s).
 */
public Map<Long, Answer> loadMapFromFile_A(Context context, String FILENAME) {
    Map<Long, Answer> answerMap = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<Map<Long, Answer>>() {
        }.getType();
        answerMap = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (answerMap == null)
        return answerMap = new HashMap<Long, Answer>();
    return answerMap;
}

From source file:ca.ualberta.app.controller.PushController.java

License:Apache License

/**
 * Load the reply Map's from the file with given name.
 * // w  w  w .  ja  v  a2s  .  c  om
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the Map of the reply(s).
 */
public Map<Long, Reply> loadMapFromFile_R(Context context, String FILENAME) {
    Map<Long, Reply> replyMap = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<Map<Long, Reply>>() {
        }.getType();
        replyMap = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (replyMap == null)
        return replyMap = new HashMap<Long, Reply>();
    return replyMap;
}

From source file:ca.ualberta.app.controller.PushController.java

License:Apache License

/**
 * Load the author from the file/*from  ww  w . j av  a2 s .c  o m*/
 * 
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of from the file
 * 
 * @return the Map of the reply(s).
 */
public Map<Long, Author> loadMapFromFile_Author(Context context, String FILENAME) {
    Map<Long, Author> authorMap = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<Map<Long, Author>>() {
        }.getType();
        authorMap = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (authorMap == null)
        return authorMap = new HashMap<Long, Author>();
    return authorMap;
}

From source file:ca.ualberta.app.controller.QuestionListController.java

License:Apache License

/**
 * Load the question list from the file with given name.
 * /*from  ww  w . java  2s. c  o  m*/
 * @param context
 *            The context.
 * @param FILENAME
 *            The name of the local file.
 * 
 * @return the question list.
 */
public static QuestionList loadFromFile(Context context, String FILENAME) {
    QuestionList question = null;
    try {
        FileInputStream fis = context.openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
        Gson gson = new Gson();
        // Following line from
        // https://sites.google.com/site/gson/gson-user-guide 2014-09-23
        Type listType = new TypeToken<QuestionList>() {
        }.getType();
        question = gson.fromJson(in, listType);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (question == null)
        return question = new QuestionList();
    return question;
}

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

License:Apache License

/**
 * Load an author form online server/* w  w  w  .  jav a2 s. c  om*/
 * 
 * @param response
 *            The online server connection response.
 * 
 * @return null.
 */
private SearchHit<Author> parseAuthorHit(HttpResponse response) {

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

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

    return null;
}

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

License:Apache License

/**
 * Parses the response of a search//  w  ww . j  a va  2 s . com
 * 
 * @param response
 *            The online server connection response.
 * 
 * @return esResponse The parsed response of a search.
 * 
 * @throws IOException
 */
private SearchResponse<Author> parseSearchResponse(HttpResponse response) throws IOException {
    String json;
    json = getEntityContent(response);

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

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

    return esResponse;
}