diff options
| author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2022-01-15 12:17:00 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-15 12:17:00 +1000 |
| commit | e8db150b5d4cc4fc065c92bafea45f08eb27ed91 (patch) | |
| tree | 1eccd02255f0c84287bff40c5219e00feb546283 /rdflib | |
| parent | a6c5169f948823f6651745ba2d2fe781d0d5ecd2 (diff) | |
| parent | 0c92b22b10c48a291c4a69e2e959b1494d3ae193 (diff) | |
| download | rdflib-e8db150b5d4cc4fc065c92bafea45f08eb27ed91.tar.gz | |
Merge pull request #1656 from RDFLib/roundtrip_hext
Allow hext to participate in RDF format roundtripping
Diffstat (limited to 'rdflib')
| -rw-r--r-- | rdflib/plugins/parsers/hext.py | 13 | ||||
| -rw-r--r-- | rdflib/plugins/serializers/hext.py | 14 |
2 files changed, 20 insertions, 7 deletions
diff --git a/rdflib/plugins/parsers/hext.py b/rdflib/plugins/parsers/hext.py index 59e045cf..fab70c78 100644 --- a/rdflib/plugins/parsers/hext.py +++ b/rdflib/plugins/parsers/hext.py @@ -24,14 +24,23 @@ class HextuplesParser(Parser): pass def _load_json_line(self, line: str): - return [x if x != "" else None for x in json.loads(line)] + # this complex handing is because the 'value' component is + # allowed to be "" but not None + # all other "" values are treated as None + ret1 = json.loads(line) + ret2 = [x if x != "" else None for x in ret1] + if ret1[2] == "": + ret2[2] = "" + return ret2 def _parse_hextuple(self, cg: ConjunctiveGraph, tup: List[Union[str, None]]): # all values check # subject, predicate, value, datatype cannot be None # language and graph may be None if tup[0] is None or tup[1] is None or tup[2] is None or tup[3] is None: - raise ValueError("subject, predicate, value, datatype cannot be None") + raise ValueError( + "subject, predicate, value, datatype cannot be None. Given: " + f"{tup}") # 1 - subject s: Union[URIRef, BNode] diff --git a/rdflib/plugins/serializers/hext.py b/rdflib/plugins/serializers/hext.py index c86882a2..3fdf3684 100644 --- a/rdflib/plugins/serializers/hext.py +++ b/rdflib/plugins/serializers/hext.py @@ -2,7 +2,8 @@ HextuplesSerializer RDF graph serializer for RDFLib. See <https://github.com/ontola/hextuples> for details about the format. """ -from typing import IO, Optional, Union +from typing import IO, Optional, Type, Union +import json from rdflib.graph import Graph, ConjunctiveGraph from rdflib.term import Literal, URIRef, Node, BNode from rdflib.serializer import Serializer @@ -19,7 +20,9 @@ class HextuplesSerializer(Serializer): def __init__(self, store: Union[Graph, ConjunctiveGraph]): self.default_context: Optional[Node] + self.graph_type: Type[Graph] if isinstance(store, ConjunctiveGraph): + self.graph_type = ConjunctiveGraph self.contexts = list(store.contexts()) if store.default_context: self.default_context = store.default_context @@ -27,6 +30,7 @@ class HextuplesSerializer(Serializer): else: self.default_context = None else: + self.graph_type = Graph self.contexts = [store] self.default_context = None @@ -101,14 +105,14 @@ class HextuplesSerializer(Serializer): else: language = "" - return '["%s", "%s", "%s", "%s", "%s", "%s"]\n' % ( + return json.dumps([ self._iri_or_bn(triple[0]), triple[1], value, datatype, language, - self._context(context), - ) + self._context(context) + ]) + "\n" else: # do not return anything for non-IRIs or BNs, e.g. QuotedGraph, Subjects return None @@ -121,7 +125,7 @@ class HextuplesSerializer(Serializer): return None def _context(self, context): - if self.default_context is None: + if self.graph_type == Graph: return "" if context.identifier == "urn:x-rdflib:default": return "" |
