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.cs.asn1.CounterSaveModel.java

License:Apache License

public void saveData(List<CounterModel> list) {
    try {//from ww  w  .  jav  a2 s .c o  m
        FileOutputStream fos = fileContext.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        Gson gson = new Gson();
        Type fooType = new TypeToken<ArrayList<CounterModel>>() {
        }.getType();
        gson.toJson(list, fooType, osw);
        osw.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:ca.ualberta.cs.asn1.CounterSaveModel.java

License:Apache License

public List<CounterModel> loadData() {
    FileInputStream fis;/*from  ww w  .j a v  a2  s  .  co m*/
    List<CounterModel> list = new ArrayList<CounterModel>();
    try {
        fis = fileContext.openFileInput(FILE_NAME);
        InputStreamReader isr = new InputStreamReader(fis);
        Gson gson = new Gson();

        Type fooType = new TypeToken<ArrayList<CounterModel>>() {
        }.getType();
        List<CounterModel> list_temp = gson.fromJson(isr, fooType);
        if (list_temp != null)
            list = list_temp;
        isr.close();
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return list;
}

From source file:ca.ualberta.cs.assign1.HabitActivity.java

License:Open Source License

private void loadFromFile() {
        try {//w  w  w. ja  v a 2  s . co  m
            FileInputStream fis = openFileInput(FILENAME);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            Gson gson = new Gson();
            //Code taken from http://stackoverflow.com/questions/12384064/gson-convert-from-json-to-a-typed-arraylistt Sept.22,2016
            Type listType = new TypeToken<ArrayList<Habit>>() {
            }.getType();
            myHabitList = gson.fromJson(in, listType);
            deleteFile(FILENAME);
        } catch (FileNotFoundException e) {
            myHabitList = new ArrayList<Habit>();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }
    }

From source file:ca.ualberta.cs.c301_crowdclient.CrowdClient.java

/**
 * Gets the list of entries using just the LIST operation of the service.
 * The entries in the list do not contain the content field.
 * @return A list of name/value pairs./*from   w  w w .j a v  a 2s  . com*/
 * @throws Exception
 */
public List<Map<String, String>> getShallowList() throws Exception {
    String jsonEntryList = listEntrys();
    Type listType = new TypeToken<List<Map<String, String>>>() {
    }.getType();
    List<Map<String, String>> shallowEntryList = gson.fromJson(jsonEntryList, listType);
    return shallowEntryList;
}

From source file:ca.ualberta.cs.cmput301w15t04team04project.CLmanager.CLmanager.java

License:Apache License

/**
 * get a claim from server by given string
 * /*from  ww  w.  j  a  v  a  2s  .  com*/
 * @author youdong
 * @param string
 * @return
 */
public Claim getClaim(String string, Context context, String name) {
    checker = new InternetChecker(context);
    if (checker.isNetworkAvailable() == false) {
        ClaimList claimList = MyLocalClaimListManager.loadClaimList(context, name);
        controller = new MyLocalClaimListController(claimList);
        Claim claim = controller.getClaim(string);
        return claim;
    } else {

        HttpGet getRequest = new HttpGet(RESOURCE_URL + string);
        HttpResponse getResponse;
        try {
            getResponse = httpClient.execute(getRequest);
            String json = getEntityContent(getResponse);
            // We have to tell GSON what type we expect
            Type searchHitType = new TypeToken<SearchHit<Claim>>() {
            }.getType();
            // Now we expect to get a Recipe response
            SearchHit<Claim> esResponse = gson.fromJson(json, searchHitType);
            // We get the recipe from it!
            return esResponse.getSource();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

From source file:ca.ualberta.cs.cmput301w15t04team04project.CLmanager.CLmanager.java

License:Apache License

/**
 * use gson to convert a HttpResponse to a SearchResponse
 * //from w w  w .  j  av a2s  .  c  o m
 * @author youdong
 * @param response
 * @return
 * @throws IOException
 */
private SearchResponse<Claim> parseSearchResponse(HttpResponse response) throws IOException {
    String json;
    json = getEntityContent(response);

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

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

    return esResponse;
}

From source file:ca.ualberta.cs.cmput301w15t04team04project.CLmanager.MyLocalClaimListManager.java

License:Apache License

/**
 * load claimList form certain file//from w ww  . ja v  a 2  s.  c  o m
 * 
 * @author Youdong Ma
 * @param context
 * @return
 */
public static ClaimList loadClaimList(Context context, String name) {
    ClaimList claimList = new ClaimList();
    Gson gson = new Gson();
    try {
        FileInputStream fis = context.openFileInput(name);
        Type listType = new TypeToken<ClaimList>() {
        }.getType();
        InputStreamReader isr = new InputStreamReader(fis);
        claimList = gson.fromJson(isr, listType);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (claimList == null) {
        claimList = new ClaimList();
    }
    return claimList;

}

From source file:ca.ualberta.cs.cmput301w15t04team04project.CLmanager.SignInManager.java

License:Apache License

/**
 * load User profile from a local storage
 * //from  w  w  w . j a va2  s.co  m
 * @param context the view that pass in
 * @param FILENAME the filename of the file that need to load from
 * @return the User that be stored in FILENAME
 */
public static User loadFromFile(Context context) {
    User user = new User(null);

    Gson gson = new Gson();
    try {
        FileInputStream fis = context.openFileInput(FILENAME);

        Type listType = new TypeToken<User>() {
        }.getType();
        InputStreamReader isr = new InputStreamReader(fis);
        user = gson.fromJson(isr, listType);
        fis.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (user == null) {
        user = new User(null);
    }
    return user;
}

From source file:ca.ualberta.cs.expensetracker.CreatNewClaimActivity.java

License:Apache License

private ArrayList<String> loadFromFile() {
    // TODO Auto-generated method stub
    Gson gson = new Gson();
    ArrayList<String> claimlist = new ArrayList<String>();
    try {//from   w ww  .j  a  v a 2 s  .  c om
        FileInputStream fis = openFileInput(FILENAME);
        //https://sites.google.com/site/gson/gson-user-guide 2015-01-30
        Type dataType = new TypeToken<ArrayList<String>>() {
        }.getType();
        InputStreamReader isr = new InputStreamReader(fis);
        claimlist = gson.fromJson(isr, dataType);
        fis.close();

    } catch (FileNotFoundException e) {
        // catch FileNotFoundException error
        e.printStackTrace();

    } catch (IOException e) {
        // catch IOException error
        e.printStackTrace();
    }
    // make sure when list is empty, App. does not crash.
    if (claimlist == null) {
        claimlist = new ArrayList<String>();
    }
    return claimlist;

}

From source file:ca.ualberta.cs.MainActivity.java

License:Open Source License

private ArrayList<Claim> loadFromFile() {
    Gson gson = new Gson();
    ArrayList<Claim> claims = null;
    try {//from  w w  w  . ja v a 2  s.  c om
        FileInputStream fis = openFileInput(FILENAME);
        InputStreamReader isr = new InputStreamReader(fis);

        // http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/Gson.html on Jan 30/15
        Type listType = new TypeToken<ArrayList<Claim>>() {
        }.getType();
        claims = gson.fromJson(isr, listType);
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (claims == null) {
        claims = new ArrayList<Claim>();
    }

    return claims;
}