SOLUTION: How to save nested List<String> in RoomDB on Android
I have the following JSON file
[
{
"id":"some_string_id1",
"name": "some_name1".
"tags": [
"tag1",
"tag2"
]
},
{
"id":"some_string_id2",
"name": "some_name2".
"tags": []
},
]
The following is how it's being parsed:
@Entity(tableName = "node_table")
public class NodeItem {
@PrimaryKey (autoGenerate = false) @NonNull
public String id;
@NonNull
public String name,tags;
NodeItem(String id, String name, String tags){
this.id = id;
this.name = name;
this.tags = tags;
}
@Override
public String toString() {
return "NodeItem{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", tags='" + tags + '\'' +
'}';
}
public static List<NodeItem> loadJSON(Context context, String path){
try{
InputStream input = context.getAssets().open(path);
Reader reader = new InputStreamReader(input);
Gson gson = new Gson();
Type type = new TypeToken<List<NodeItem>>(){}.getType();
return gson.fromJson(reader,type);
}catch (IOException e){
e.printStackTrace();
return Collections.emptyList();
}
}
The error I get is:
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY
Is it possible to parse the list as a comma-separated string?
If that's not possible, then how can I save the list into the database?