Skip to content

Commit 0cbf5ab

Browse files
committed
Improve JSON-LD context parsing
Following w3c/json-ld-syntax#329 and w3c/json-ld-syntax#329, this PR updates the processing of JSON-LD contexts to handle when there are dictionaries.
1 parent b5e3598 commit 0cbf5ab

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/curies/api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,15 @@ def from_jsonld(cls, data: LocationOr[Dict[str, Any]]) -> "Converter":
624624
>>> converter = Converter.from_jsonld(url)
625625
>>> "rdf" in converter.prefix_map
626626
"""
627-
return cls.from_prefix_map(_prepare(data)["@context"])
627+
prefix_map = {}
628+
for key, value in _prepare(data)["@context"].items():
629+
if isinstance(value, str):
630+
prefix_map[key] = value
631+
elif isinstance(value, dict) and value.get("@prefix") == True:
632+
prefix_map[key] = value["@id"]
633+
else:
634+
raise TypeError
635+
return cls.from_prefix_map(prefix_map)
628636

629637
@classmethod
630638
def from_jsonld_github(

tests/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ def test_bioregistry(self):
145145
self.assertIn("chebi", c.prefix_map)
146146
self.assertNotIn("CHEBI", c.prefix_map)
147147

148+
def test_jsonld(self):
149+
"""Test parsing JSON-LD context."""
150+
context = {
151+
"@context": {
152+
"hello": "https://example.org/hello:",
153+
"CHEBI": {
154+
"@prefix": True,
155+
"@id": "http://purl.obolibrary.org/CHEBI_",
156+
},
157+
}
158+
}
159+
converter = Converter.from_jsonld(context)
160+
self.assertIn("hello", converter.prefix_map)
161+
self.assertIn("CHEBI", converter.prefix_map)
162+
148163
def test_from_github(self):
149164
"""Test getting a JSON-LD map from GitHub."""
150165
with self.assertRaises(ValueError):

0 commit comments

Comments
 (0)