I have a file system structure parsed as JSON:
{
"name": "rootf",
"type": "system",
"path": "Parsing/rootf",
"children": [{
"name": "f1",
"type": "folder",
"path": "Parsing/rootf/f1",
"children": [{
"name": "subf1",
"type": "folder",
"path": "Parsing/rootf/f1/subf1",
"children": [{
"name": "text1.txt",
"type": "file",
"path": "Parsing/rootf/folder1/subf1/text1.txt",
"children": ["a", "b", "c"]
}]
}, {
"name": "subf2",
"type": "folder",
"path": "Parsing/rootf/f1/subf2",
"children": []
}, {
"name": "text2.txt",
"type": "file",
"path": "TParsing/rootf/f1/text2.txt",
"children": ["d", "e", "f"]
}]
}, {
"name": "text1.txt",
"type": "file",
"path": "Parsing/rootd/text1.txt",
"children": ["aa", "bb"]
}],
"_id": "5ce47292d866fc2f40037a56"
}
As can be seen children of type system (denoting the root folder) and of type folder (denoting sub-folders of the root folder) can contain other folders and/or a file. The children of type file contains contents of the file.
I would later need to access individual objects of type folder and type file. What would be the most efficient way to map this to Java objects and how can I access them individually?
I initially tried to use GSON to map it to three classes, System.java, Folder.java and File.java with them containing private List<Folder> children, private List<File> children and private List<String> children respectively. This only works when I have a particular JSON structure (RootFolder->Sub-Folder->File). Is there any way I can make the mapping more generalized so that it includes the condition that a System can contain Folder and File and similarly, a Folder can contain a Folder and File?
Systemclass withList<Children>and a child can be a folder or file as it has a type parameter? If you really want to polymorphism, then you can refer to these links stackoverflow.com/questions/30362446/… stackoverflow.com/questions/5800433/polymorphism-with-gson stackoverflow.com/questions/15736654/…fileandfolderobjects from that? Actually, for aSystemI need to build a Set ofcomponentsobjects. ThiscomponentsSet should contain the files and folders objects inside theSystem. I am a bit confused as to how I can do this.