Skip to content

Commit

Permalink
Handle keywords and empty strings in JSON-LD (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Nov 1, 2023
1 parent aa16104 commit 670ea5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,6 +59,8 @@
"write_shacl",
]

logger = logging.getLogger(__name__)

X = TypeVar("X")
LocationOr = Union[str, Path, X]

Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 670ea5d

Please sign in to comment.