Skip to content

Commit 4736e28

Browse files
committed
fix: replace get() with path() for safer JSON node access in CustomPreview
1 parent 5f16590 commit 4736e28

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

src/main/java/edu/kit/datamanager/ro_crate/preview/CustomPreview.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,51 +63,43 @@ private CustomPreviewModel mapFromJson(String metadata) throws IOException {
6363
if (graph.isArray()) {
6464

6565
for (JsonNode node : graph) {
66-
String id = node.get("@id").asText();
66+
String id = node.path("@id").asText();
6767
List<String> types = new LinkedList<>();
68-
if (node.get("@type").isArray()) {
68+
if (node.path("@type").isArray()) {
6969

70-
Collections.addAll(types, (String[]) mapper.convertValue(node.get("@type"), String[].class));
70+
Collections.addAll(types, mapper.convertValue(node.path("@type"), String[].class));
7171
} else {
72-
types.add(node.get("@type").asText());
72+
types.add(node.path("@type").asText());
7373
}
7474

7575
if (types.contains("Dataset") && "./".equals(id)) {
76-
crate.name = node.get("name").asText();
77-
crate.description = node.get("description") == null ? null : node.get("description").asText();
76+
crate.name = node.path("name").asText();
77+
crate.description = node.path("description").asText(null);
7878
crate.type = "Dataset";
79-
if (node.get("license") != null) {
80-
crate.license = node.get("license").isObject() ? node.get("license").get("@id").asText() : node.get("license").asText();
81-
}
82-
crate.datePublished = node.get("datePublished") == null ? null : node.get("datePublished").asText();
79+
crate.license = node.path("license").path("@id").asText(node.path("license").asText(null));
80+
crate.datePublished = node.path("datePublished").asText(null);
8381
crate.hasPart = new ArrayList<>();
8482

85-
if (node.has("hasPart")) {
86-
for (JsonNode part : node.get("hasPart")) {
87-
CustomPreviewModel.Part p = new CustomPreviewModel.Part();
88-
if (part.isObject()) {
89-
p.id = part.get("@id").asText();
90-
p.name = part.get("@id").asText(); // Name will be replaced later
91-
} else {
92-
p.id = part.asText();
93-
}
94-
95-
crate.hasPart.add(p);
96-
}
83+
for (JsonNode part : node.path("hasPart")) {
84+
CustomPreviewModel.Part p = new CustomPreviewModel.Part();
85+
String tmpId = part.path("@id").asText(part.asText());
86+
p.id = tmpId;
87+
p.name = tmpId; // Name will be replaced later
88+
crate.hasPart.add(p);
9789
}
9890
} else if (types.contains("Dataset")) {
9991
CustomPreviewModel.Dataset dataset = new CustomPreviewModel.Dataset();
10092
dataset.id = id;
101-
dataset.name = node.get("name").asText();
102-
dataset.description = node.get("description").asText();
93+
dataset.name = node.path("name").asText();
94+
dataset.description = node.path("description").asText();
10395
datasets.add(dataset);
10496
} else if (types.contains("File")) {
10597
CustomPreviewModel.File file = new CustomPreviewModel.File();
10698
file.id = id;
107-
file.name = node.get("name") == null ? null : node.get("name").asText();
108-
file.description = node.get("description") == null ? null : node.get("description").asText();
109-
file.contentSize = node.get("contentSize") == null ? null : node.get("contentSize").asText();
110-
file.encodingFormat = node.get("encodingFormat") == null ? null : node.get("encodingFormat").asText();
99+
file.name = node.path("name").asText(null);
100+
file.description = node.path("description").asText(null);
101+
file.contentSize = node.path("contentSize").asText(null);
102+
file.encodingFormat = node.path("encodingFormat").asText(null);
111103
files.add(file);
112104
}
113105
}

0 commit comments

Comments
 (0)