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
?