0

I am new to the world of Generics and am trying to write a utility class that will take a list of Objects and persist it to the store and then retrieve it back.

This is what I wrote to save the list:

    public static void saveListToStore(Context ctx, String fileName, list<Object> listToStore) throws IOException
    {
       String elemValue = "";
       Gson gson = new Gson();

       try {
          FileOutputStream fileOutputStream = ctx.openFileOutput(fileName, ctx.MODE_PRIVATE);

          elemValue= gson.toJson(listToStore);
          fileOutputStream.write(elemValue.getBytes());
          objectOutputStream.close();

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

However when I try to retrieve, I will not be aware of the type of object that was there in the list and cannot rebuild it back. I do not want to put type comparisons as I would like to save any type of custom class and the list can be huge.

I want to deduce the type from the content itself. I was thinking of saving the type as the first line and then the data. So on retrieve I can get the type first and then typecast the objects. However is there any other cleaner way of achieving this ?

Hemraj
  • 1
  • try something like this objectA instanceOf String – Illegal Argument Sep 21 '14 at 05:15
  • Just to make sure we're on the same page, you posted the method that *does* work and the one that doesn't work you didn't ? Please post the problematic code, and examples of input, output and expected output. – Nir Alfasi Sep 21 '14 at 05:28

2 Answers2

1

Ur Object should implement Serializable and the below code can help you to read and write

public static void readListToStore(Context ctx, String fileName, List<Object> listToStore) throws IOException {
    SharedPreferences storeDataPref = ctx.getSharedPreferences("UR_KEY", Context.MODE_PRIVATE);
    String elemValue = storeDataPref.getString("UR_NAME", null);
    if (elemValue != null) {
        Type listType = new TypeToken<ArrayList<Object>>() {
        }.getType();
        listToStore = new Gson().fromJson(elemValue, listType);
    }
}

public static void saveListToStore(Context ctx, String fileName, List<Object> listToStore) throws IOException {
    String elemValue = "";
    Gson gson = new Gson();
    try {
        FileOutputStream fileOutputStream = ctx.openFileOutput(fileName, ctx.MODE_PRIVATE);
        elemValue = gson.toJson(listToStore);
        SharedPreferences storeDataPref = ctx.getSharedPreferences("UR_KEY", Context.MODE_PRIVATE);
        Editor storeDataEditor = storeDataPref.edit();
        storeDataEditor.clear();
        storeDataEditor.putString("UR_NAME", elemValue).apply();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
Harsha Vardhan
  • 3,324
  • 2
  • 17
  • 22
  • +1 because it's a useful answer, that said - the OP uses a customized list, not `java.util.List` – Nir Alfasi Sep 21 '14 at 05:39
  • The above didnt work as expected and I guess it was due my own misunderstanding. I expected List to allow me to pass a list of my own class. Tried the following and it didnt work List newCardList = ArrayList(); dataStore.saveListToStore(fn, newCardList); dataStore.readListToStore(fn,newCardList); – Hemraj Sep 21 '14 at 13:32
0

Taken from this question: Trouble with Gson serializing an ArrayList of POJO's

"You need to give Gson information on the specific generic type of List you're using (or any generic type you use with it). Particularly when deserializing JSON, it needs that information to be able to determine what type of object it should deserialize each array element to.

Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();
String s = gson.toJson(list, listOfTestObject);
List<TestObject> list2 = gson.fromJson(s, listOfTestObject);

This is documented in the Gson user guide."

You can write and read the string to a file. The List can be of any collection type implementing the List interface

Community
  • 1
  • 1
Bart Burg
  • 4,786
  • 7
  • 52
  • 87