In my app I use gson to save a list of custom objects List<AppBasicView> appViews. The objects are instances of AppBasicView (AppBasicView objects and child objects of that class).
The AppBasicView class is built like that:
public class AppBasicView {
enum BasicViewType {ImageView}
private BasicViewType mBasicViewType;
private LinearLayout.LayoutParams mLayoutParams;
public AppBasicView(BasicViewType basicViewType, LinearLayout.LayoutParams layoutParams) {
this.mBasicViewType = basicViewType;
this.mLayoutParams = layoutParams;
}
...
(getters&setters)
}
AppTextView - the child class of AppBasicView, is built like that:
public class AppTextView extends AppBasicView {
enum TextViewType {Button, TextView, EditText}
private TextViewType mTextViewType;
private String mText;
public AppTextView(TextViewType textViewType, LinearLayout.LayoutParams layoutParams, String mText) {
super(null, layoutParams);
this.mTextViewType = textViewType;
this.mText = mText;
}
...
(getters&setters)
}
I'm saving the list like that:
Gson gson = new Gson();
String json = gson.toJson(appViews);
spEditor.putString("app1objects", json);
spEditor.apply();
Problem 1
The json string I get when saving AppBasicView object contains only the mBasicViewType field (and doesn't contain mLayoutParams field).
And when I save AppBasicView child, the json string contains only child's additional fields and doesn't contain any of the parents (AppBasicView) fields (neither mBasicViewType nor mLayoutParams).
Can't understand why I'm not getting those fields serialized.
Problem 2
After deserialization I get the objects list with only AppBasicView views (even if they where AppTextView) that are not recognized as child objects of AppBasicView (for an AppBasicView v that was AppTextView, v instanceof AppTextView returns false).
This is the deserialization code:
String json = appDataPreferences.getString("app1objects", "");
Type listType = new TypeToken<ArrayList<AppBasicView>>(){}.getType();
if(!json.equals("null"))
appViews = new Gson().fromJson(json, listType);
How can I get the full children object and use them as they were and not as up-casted objects?
Thanks for any help in advance!
- This is how I add objects to the list (in case it could help to find the answer):
AppBasicView appBasicView;
if(...)
{
appBasicView = new AppTextView(...);
}
else if(...)
{
appBasicView = new AppBasicView(...);
}
else
throw new CustomExceptions.InvalidDroppingViewTag();
...
appViews.add(appBasicView);
...
Solution 1
Disappeared fields turned out to be all null so gson didn't mention them because:
While serializing, a null field is omitted from the output.
To still see those fields you need to create your Gson like that: Gson gson = new GsonBuilder().serializeNulls().create();