diff --git a/src/curies/api.py b/src/curies/api.py index 0e41b6f..8fdb070 100644 --- a/src/curies/api.py +++ b/src/curies/api.py @@ -5,6 +5,7 @@ import csv import itertools as itt import json +import logging from collections import defaultdict from functools import partial from pathlib import Path @@ -58,6 +59,8 @@ "write_shacl", ] +logger = logging.getLogger(__name__) + X = TypeVar("X") LocationOr = Union[str, Path, X] @@ -824,9 +827,21 @@ def from_jsonld(cls, data: LocationOr[Dict[str, Any]], **kwargs: Any) -> "Conver >>> url = f"{base}/biopragmatics/bioregistry/main/exports/contexts/semweb.context.jsonld" >>> converter = Converter.from_jsonld(url) >>> "rdf" in converter.prefix_map + + .. seealso:: https://www.w3.org/TR/json-ld11/#the-context defines the ``@context`` aspect of JSON-LD """ prefix_map = {} for key, value in _prepare(data)["@context"].items(): + # TODO how to handle key == "@base"? + if not key: + logger.warning( + "The JSON-LD specification says in https://www.w3.org/TR/json-ld/#terms that " + "keys are not allowed to be empty strings. The given @context object contained " + "an empty string as one of its keys" + ) + continue + if key.startswith("@"): + continue if isinstance(value, str): prefix_map[key] = value elif isinstance(value, dict) and value.get("@prefix") is True: diff --git a/tests/test_api.py b/tests/test_api.py index 96e2331..d9b6758 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -325,6 +325,8 @@ def test_jsonld(self): """Test parsing JSON-LD context.""" context = { "@context": { + "@version": "1.0.0", # should skip this + "": "", # should skip this "hello": "https://example.org/hello:", "CHEBI": { "@prefix": True,