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.marini_reflex.DataHandler.java

License:Open Source License

public void init(Context ctx) {
    try {/*www.j  a va2 s .  co m*/

        //create the input readers
        FileInputStream fis1 = ctx.openFileInput(ARRAYFILE);
        BufferedReader in1 = new BufferedReader(new InputStreamReader(fis1));
        FileInputStream fis2 = ctx.openFileInput(TWOPLAYERFILE);
        BufferedReader in2 = new BufferedReader(new InputStreamReader(fis2));
        FileInputStream fis3 = ctx.openFileInput(THREEPLAYERFILE);
        BufferedReader in3 = new BufferedReader(new InputStreamReader(fis3));
        FileInputStream fis4 = ctx.openFileInput(FOURPLAYERFILE);
        BufferedReader in4 = new BufferedReader(new InputStreamReader(fis4));

        //create the gsons
        Gson arrayGson = new Gson();
        Gson twoPlayerGson = new Gson();
        Gson threePlayerGson = new Gson();
        Gson fourPlayerGson = new Gson();

        ////http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/
        // com/google/gson/Gson.html, 03.10.2015
        //create the type tokens
        Type arrayListType = new TypeToken<ArrayList<ReflexTime>>() {
        }.getType();
        Type twoPlayerStandingType = new TypeToken<TwoPlayerStanding>() {
        }.getType();
        Type threePlayerStandingType = new TypeToken<ThreePlayerStanding>() {
        }.getType();
        Type fourPlayerStandingType = new TypeToken<FourPlayerStanding>() {
        }.getType();

        //write the gsons to the files
        twoPlayer = twoPlayerGson.fromJson(in1, twoPlayerStandingType);
        threePlayer = threePlayerGson.fromJson(in2, threePlayerStandingType);
        fourPlayer = fourPlayerGson.fromJson(in3, fourPlayerStandingType);
        reflexTimes = arrayGson.fromJson(in4, arrayListType);
    }
    //If the file doesn't exist, instantiate all of the variables.
    catch (FileNotFoundException e) {
        reflexTimes = new ArrayList<>();
        twoPlayer = new TwoPlayerStanding(0, 0);
        threePlayer = new ThreePlayerStanding(0, 0, 0);
        fourPlayer = new FourPlayerStanding(0, 0, 0, 0);
    }

}

From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.ClaimMapper.java

License:Apache License

/**
 * Loads claim data one field at a time. The field loaded is associated
 * with the key./*  w w  w.  j  a va2 s .c o m*/
 * @param claimId
 * @param key
 * @return Claim associated with claimId
 */
public Object loadClaimData(int claimId, String key) {

    Object data = 0;
    SharedPreferences claimFile = this.context.getSharedPreferences(getClaimFileName(claimId), 0);
    Gson gson = new Gson();

    if (key.equals("id")) {
        data = claimFile.getInt(key, -1);
    } else if (key.equals("description")) {
        data = claimFile.getString(key, "");
    } else if (key.equals("startDate")) {
        // CITATION http://stackoverflow.com/questions/7145606/how-android-sharedpreferences-save-store-object
        // 2015-03-12
        // MuhammadAamirALi's answer
        String startDateJson = claimFile.getString(key, "");
        data = gson.fromJson(startDateJson, Date.class);
    } else if (key.equals("endDate")) {
        String startDateJson = claimFile.getString(key, "");
        data = gson.fromJson(startDateJson, Date.class);
    } else if (key.equals("destinations")) {
        String destinationsJson = claimFile.getString(key, "");
        // http://stackoverflow.com/questions/14981233/android-arraylist-of-custom-objects-save-to-sharedpreferences-serializable
        // 2015-03-12
        // SpyZip's answer
        Type type = new TypeToken<ArrayList<Destination>>() {
        }.getType();
        data = gson.fromJson(destinationsJson, type);
    } else if (key.equals("tags")) {
        String tagsJson = claimFile.getString(key, "");
        if (tagsJson == "") {
            data = new ArrayList<String>();
        } else {
            data = gson.fromJson(tagsJson, ArrayList.class);
        }
    } else if (key.equals("status")) {
        data = claimFile.getString(key, "");
    } else if (key.equals("approverName")) {
        data = claimFile.getString(key, "");
    } else if (key.equals("approverComments")) {
        String commentsJson = claimFile.getString(key, "");
        data = gson.fromJson(commentsJson, ArrayList.class);
    } else if (key.equals("canEdit")) {
        data = claimFile.getBoolean(key, false);
    } else if (key.equals("expenses")) {
        String expensesJson = claimFile.getString(key, "");
        Type type = new TypeToken<ArrayList<Expense>>() {
        }.getType();
        data = gson.fromJson(expensesJson, type);
    } else if (key.equals("userId")) {
        data = claimFile.getInt("userId", 0);
    }

    return data;
}

From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.SyncQueueMapper.java

License:Apache License

/**
 * Loads the claims that still need to be synced to the online server
 * @return LinkedList of items tha need to be synced
 *//*from w  w w.  java 2  s .c o  m*/
@SuppressWarnings("rawtypes")
public LinkedList loadSyncQueue() {

    LinkedList syncQueue;
    SharedPreferences syncQueueFile = this.context.getSharedPreferences("syncQueue", 0);
    Gson gson = new Gson();

    String syncQueueJson = syncQueueFile.getString("syncQueue", "");
    // http://stackoverflow.com/questions/14981233/android-arraylist-of-custom-objects-save-to-sharedpreferences-serializable
    // 2015-03-12
    // SpyZip's answer
    Type type = new TypeToken<LinkedList<SyncQueueItem>>() {
    }.getType();
    syncQueue = gson.fromJson(syncQueueJson, type);

    return syncQueue;
}

From source file:ca.ualberta.cs.scandaloutraveltracker.mappers.UserMapper.java

License:Apache License

/**
 * Loads user data one field at a time. The field loaded is associated
 * with the key.//w  ww  .ja  v a2s  . c  om
 * @param userId
 * @param key
 * @return User associated with userId
 */
public Object loadUserData(int userId, String key) {

    Object data = 0;
    SharedPreferences userFile = this.context.getSharedPreferences(getUserFileName(userId), 0);
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Location.class, new LocationDeserializer());
    gsonBuilder.registerTypeAdapter(Location.class, new LocationSerializer());
    Gson gson = gsonBuilder.create();

    if (key.equals("id")) {
        data = userFile.getInt(key, -1);
    } else if (key.equals("name")) {
        data = userFile.getString(key, "");
    } else if (key.equals("location")) {
        String locationsJson = userFile.getString(key, "");
        Type type = new TypeToken<Location>() {
        }.getType();
        data = gson.fromJson(locationsJson, type);
    }

    return data;
}

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

License:Creative Commons License

private void loadFromFile() {
    try {//from   w  w w  .ja  va2 s . c om
        FileInputStream fis = openFileInput(FILENAME);
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));

        Gson gson = new Gson();

        // code from http://stackoverflow.com/questions/12384064/gson-convert-from-json-to-a-typed-arraylist
        Type listType = new TypeToken<WeeklySchedule>() {
        }.getType();

        WeeklyScheduleController.setWeeklySchedule((WeeklySchedule) gson.fromJson(in, listType));
    } catch (FileNotFoundException e) {
        // Do nothing, weeklySchedule was initialized to empty at the start of the file
    }
}

From source file:ca.ualberta.cs.swapmyride.Misc.RetrievePhotoRunnable.java

License:Apache License

public void run() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = null;/*  w  ww . ja  va2 s . co m*/

    SearchHit<Photo> hit = null;
    try {
        response = httpClient.execute(httpGet);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Type searchHitType = new TypeToken<SearchHit<Photo>>() {
    }.getType();

    try {
        hit = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
    } catch (JsonIOException e) {
        throw new RuntimeException(e);
    } catch (JsonSyntaxException e) {
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    this.photo = hit.getSource();
}

From source file:ca.ualberta.cs.swapmyride.Misc.RetrieveUserRunnable.java

License:Apache License

public void run() {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = null;//from  ww w.ja  va  2 s  .c o  m

    SearchHit<User> hit = null;
    try {
        response = httpClient.execute(httpGet);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Type searchHitType = new TypeToken<SearchHit<User>>() {
    }.getType();

    try {
        hit = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
    } catch (JsonIOException e) {
        throw new RuntimeException(e);
    } catch (JsonSyntaxException e) {
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    this.user = hit.getSource();
}

From source file:ca.ualberta.cs.swapmyride.Misc.SearchPhotoRunnable.java

License:Apache License

public void run() {
    HttpClient client = new DefaultHttpClient();
    HttpGet search = new HttpGet(url);
    search.setHeader("Accept", "application/json");
    HttpResponse response = null;//from  w  w  w .j a va 2s.c  o m
    SearchHit<Photo> hit;

    try {
        response = client.execute(search);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Type searchHitType = new TypeToken<SearchHit<Photo>>() {
    }.getType();
    try {
        hit = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
    } catch (JsonIOException e) {
        throw new RuntimeException(e);
    } catch (JsonSyntaxException e) {
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    exists = hit.isFound();
}

From source file:ca.ualberta.cs.swapmyride.Misc.SearchUserRunnable.java

License:Apache License

public void run() {
    HttpClient client = new DefaultHttpClient();
    HttpGet search = new HttpGet(url);
    search.setHeader("Accept", "application/json");
    HttpResponse response = null;//  www. j a v a 2  s  .  c  o  m
    SearchHit<User> hit;

    try {
        response = client.execute(search);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Type searchHitType = new TypeToken<SearchHit<User>>() {
    }.getType();
    try {
        hit = gson.fromJson(new InputStreamReader(response.getEntity().getContent()), searchHitType);
    } catch (JsonIOException e) {
        throw new RuntimeException(e);
    } catch (JsonSyntaxException e) {
        throw new RuntimeException(e);
    } catch (IllegalStateException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Log.i("ISFOUND", String.valueOf(hit.isFound()));
    exists = hit.isFound();
}

From source file:ca.ualberta.cs.team16app.elasitcSearch.ESClient.java

License:Open Source License

/**
 * Consumes the Get operation of the service
 * //from  w  w  w.  jav a  2  s. c o m
 * Given a claimId, It returns a claimObject from The Server.
 * 
 * @param claimID
 * @return claim -- Returns Null if no claim Found
 */
public Claim getClaim(String claimID) {

    Claim claim = null;
    try {
        HttpGet getRequest = new HttpGet(WEBSERVICE_URI + CLAIM_FOLDER + claimID);

        getRequest.addHeader("Accept", "application/json");

        HttpResponse response = httpclient.execute(getRequest);

        String status = response.getStatusLine().toString();
        System.out.println(status);

        String json = getEntityContent(response);

        // We have to tell GSON what type we expect
        Type elasticSearchResponseType = new TypeToken<ElasticSearchResponse<Claim>>() {
        }.getType();
        // Now we expect to get a claim response
        ElasticSearchResponse<Claim> esResponse = gson.fromJson(json, elasticSearchResponseType);

        // We get the claim from it
        claim = esResponse.getSource();

        // getRequest.releaseConnection();

    } catch (ClientProtocolException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();
    }

    return claim;
}