Skip to content

Commit 8335a81

Browse files
authored
Merge pull request #243 from simleo/read_value_objects
Read crates whose metadata contains JSON-LD value objects
2 parents 016ff40 + a504dff commit 8335a81

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

rocrate/model/entity.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ def __setitem__(self, key: str, value):
105105
if key.startswith("@"):
106106
raise KeyError(f"cannot set '{key}'")
107107
values = value if isinstance(value, list) else [value]
108-
for v in values:
108+
for i, v in enumerate(values):
109109
if isinstance(v, dict) and "@id" not in v:
110-
raise ValueError(f"no @id in {v}")
110+
# https://www.w3.org/TR/json-ld11/#value-objects
111+
if "@value" in v:
112+
values[i] = v["@value"]
113+
else:
114+
raise ValueError(f"no @id in {v}")
111115
ref_values = [{"@id": _.id} if isinstance(_, Entity) else _ for _ in values]
112116
self._jsonld[key] = ref_values if isinstance(value, list) else ref_values[0]
113117

test/test_model.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,41 @@ def test_entity_in_properties(tmpdir):
553553
assert out_authors[1] is out_bob
554554
assert out_alice == alice
555555
assert out_bob == bob
556+
557+
558+
def test_value_objects(tmpdir):
559+
description = "A collection of my pictures"
560+
date_published = "2024-05-17T01:04:52+01:00"
561+
metadata = {
562+
"@context": "https://w3id.org/ro/crate/1.2/context",
563+
"@graph": [
564+
{
565+
"@id": "ro-crate-metadata.json",
566+
"@type": "CreativeWork",
567+
"conformsTo": {"@id": "https://w3id.org/ro/crate/1.2"},
568+
"about": {"@id": "./"},
569+
},
570+
{
571+
"@id": "./",
572+
"@type": "Dataset",
573+
"name": "My pictures",
574+
"description": {
575+
"@value": description,
576+
"@language": "en"
577+
},
578+
"datePublished": {
579+
"@value": date_published,
580+
"@type": "http://www.w3.org/2001/XMLSchema#dateTime"
581+
},
582+
"license": "CC0-1.0",
583+
},
584+
]
585+
}
586+
crate = ROCrate(metadata)
587+
assert crate.root_dataset.get("description") == description
588+
assert crate.root_dataset.get("datePublished") == date_published
589+
out_path = tmpdir / "ro_crate_out"
590+
crate.write(out_path)
591+
rcrate = ROCrate(out_path)
592+
assert rcrate.root_dataset.get("description") == description
593+
assert rcrate.root_dataset.get("datePublished") == date_published

0 commit comments

Comments
 (0)