Source code

Download source archive ↓

src/rdflib/plugins/serializers/jsonld.py

1"""
2This serialiser will output an RDF Graph as a JSON-LD formatted document. See http://json-ld.org/
3
4Example:
5    ```python
6    >>> from rdflib import Graph
7    >>> testrdf = '''
8    ... @prefix dc: <http://purl.org/dc/terms/> .
9    ... <http://example.org/about>
10    ...     dc:title "Someone's Homepage"@en .
11    ... '''
12
13    >>> g = Graph().parse(data=testrdf, format='n3')
14
15    >>> print(g.serialize(format='json-ld', indent=2))
16    [
17      {
18        "@id": "http://example.org/about",
19        "http://purl.org/dc/terms/title": [
20          {
21            "@language": "en",
22            "@value": "Someone's Homepage"
23          }
24        ]
25      }
26    ]
27
28    ```
29"""
30
31# From: https://github.com/RDFLib/rdflib-jsonld/blob/feature/json-ld-1.1/rdflib_jsonld/serializer.py
32
33# NOTE: This code writes the entire JSON object into memory before serialising,
34# but we should consider streaming the output to deal with arbitrarily large
35# graphs.
36
37from __future__ import annotations
38
39import warnings
40from typing import IO, Any, Dict, List, Optional
41
42from rdflib.graph import DATASET_DEFAULT_GRAPH_ID, Graph, _ObjectType
43from rdflib.namespace import RDF, XSD
44from rdflib.serializer import Serializer
45from rdflib.term import BNode, IdentifiedNode, Identifier, Literal, URIRef
46
47from ..shared.jsonld.context import UNDEF, Context
48from ..shared.jsonld.keys import CONTEXT, GRAPH, ID, LANG, LIST, SET, VOCAB
49from ..shared.jsonld.util import _HAS_ORJSON, json, orjson
50
51__all__ = ["JsonLDSerializer", "from_rdf"]
52
53
54PLAIN_LITERAL_TYPES = {XSD.boolean, XSD.integer, XSD.double, XSD.string}
55
56
57class JsonLDSerializer(Serializer):
58    """JSON-LD RDF graph serializer."""
59
60    def __init__(self, store: Graph):
61        super(JsonLDSerializer, self).__init__(store)
62
63    def serialize(
64        self,
65        stream: IO[bytes],
66        base: Optional[str] = None,
67        encoding: Optional[str] = None,
68        **kwargs: Any,
69    ) -> None:
70        # TODO: docstring w. args and return value
71        encoding = encoding or "utf-8"
72        if encoding not in ("utf-8", "utf-16"):
73            warnings.warn(
74                "JSON should be encoded as unicode. " f"Given encoding was: {encoding}"
75            )
76
77        context_data = kwargs.get("context")
78        use_native_types = (kwargs.get("use_native_types", False),)
79        use_rdf_type = kwargs.get("use_rdf_type", False)
80        auto_compact = kwargs.get("auto_compact", False)
81
82        indent = kwargs.get("indent", 2)
83        separators = kwargs.get("separators", (",", ": "))
84        sort_keys = kwargs.get("sort_keys", True)
85        ensure_ascii = kwargs.get("ensure_ascii", False)
86
87        obj = from_rdf(
88            self.store,
89            context_data,
90            base,
91            use_native_types,
92            use_rdf_type,
93            auto_compact=auto_compact,
94        )
95        if _HAS_ORJSON:
96            option: int = orjson.OPT_NON_STR_KEYS
97            if indent is not None:
98                option |= orjson.OPT_INDENT_2
99            if sort_keys:
100                option |= orjson.OPT_SORT_KEYS
101            if ensure_ascii:
102                warnings.warn("Cannot use ensure_ascii with orjson")
103            data_bytes = orjson.dumps(obj, option=option)
104            stream.write(data_bytes)
105        else:
106            data = json.dumps(
107                obj,
108                indent=indent,
109                separators=separators,
110                sort_keys=sort_keys,
111                ensure_ascii=ensure_ascii,
112            )
113            stream.write(data.encode(encoding, "replace"))
114
115
116def from_rdf(
117    graph,
118    context_data=None,
119    base=None,
120    use_native_types=False,
121    use_rdf_type=False,
122    auto_compact=False,
123    startnode=None,
124    index=False,
125):
126    # TODO: docstring w. args and return value
127    # TODO: support for index and startnode
128
129    if not context_data and auto_compact:
130        context_data = dict(
131            (pfx, str(ns))
132            for (pfx, ns) in graph.namespaces()
133            if pfx and str(ns) != "http://www.w3.org/XML/1998/namespace"
134        )
135
136    if isinstance(context_data, Context):
137        context = context_data
138        context_data = context.to_dict()
139    else:
140        context = Context(context_data, base=base)
141
142    converter = Converter(context, use_native_types, use_rdf_type)
143    result = converter.convert(graph)
144
145    if converter.context.active:
146        if isinstance(result, list):
147            result = {context.get_key(GRAPH): result}
148        result[CONTEXT] = context_data
149
150    return result
151
152
153class Converter:
154    def __init__(self, context: Context, use_native_types: bool, use_rdf_type: bool):
155        self.context = context
156        self.use_native_types = context.active or use_native_types
157        self.use_rdf_type = use_rdf_type
158
159    def convert(self, graph: Graph):
160        # TODO: bug in rdflib dataset parsing (nquads et al):
161        # plain triples end up in separate unnamed graphs (rdflib issue #436)
162        if graph.context_aware:
163            # type error: "Graph" has no attribute "contexts"
164            all_contexts = list(graph.contexts())  # type: ignore[attr-defined]
165            has_dataset_default_id = any(
166                c.identifier == DATASET_DEFAULT_GRAPH_ID for c in all_contexts
167            )
168            if (
169                has_dataset_default_id
170                # # type error: "Graph" has no attribute "contexts"
171                and graph.default_context.identifier == DATASET_DEFAULT_GRAPH_ID  # type: ignore[attr-defined]
172            ):
173                default_graph = graph.default_context  # type: ignore[attr-defined]
174            else:
175                default_graph = Graph()
176            graphs = [default_graph]
177            default_graph_id = default_graph.identifier
178
179            for g in all_contexts:
180                if g in graphs:
181                    continue
182                if isinstance(g.identifier, URIRef):
183                    graphs.append(g)
184                else:
185                    default_graph += g
186        else:
187            graphs = [graph]
188            default_graph_id = graph.identifier
189
190        context = self.context
191
192        objs: List[Any] = []
193        for g in graphs:
194            obj = {}
195            graphname = None
196
197            if isinstance(g.identifier, URIRef):
198                if g.identifier != default_graph_id:
199                    graphname = context.shrink_iri(g.identifier)
200                    obj[context.id_key] = graphname
201
202            nodes = self.from_graph(g)
203
204            if not graphname and len(nodes) == 1:
205                obj.update(nodes[0])
206            else:
207                if not nodes:
208                    continue
209                obj[context.graph_key] = nodes
210
211            if objs and objs[0].get(context.get_key(ID)) == graphname:
212                objs[0].update(obj)
213            else:
214                objs.append(obj)
215
216        if len(graphs) == 1 and len(objs) == 1 and not self.context.active:
217            default = objs[0]
218            items = default.get(context.graph_key)
219            if len(default) == 1 and items:
220                objs = items
221        elif len(objs) == 1 and self.context.active:
222            objs = objs[0]
223
224        return objs
225
226    def from_graph(self, graph: Graph):
227        nodemap: Dict[Any, Any] = {}
228
229        for s in set(graph.subjects()):
230            ## only iri:s and unreferenced (rest will be promoted to top if needed)
231            if isinstance(s, URIRef) or (
232                isinstance(s, BNode) and not any(graph.subjects(None, s))
233            ):
234                self.process_subject(graph, s, nodemap)
235
236        return list(nodemap.values())
237
238    def process_subject(self, graph: Graph, s: IdentifiedNode, nodemap):
239        if isinstance(s, URIRef):
240            node_id = self.context.shrink_iri(s)
241        elif isinstance(s, BNode):
242            node_id = s.n3()
243        else:
244            # This does not seem right, this probably should be an error.
245            node_id = None
246
247        # used_as_object = any(graph.subjects(None, s))
248        if node_id in nodemap:
249            return None
250
251        node = {}
252        node[self.context.id_key] = node_id
253        nodemap[node_id] = node
254
255        for p, o in graph.predicate_objects(s):
256            # type error: Argument 3 to "add_to_node" of "Converter" has incompatible type "Node"; expected "IdentifiedNode"
257            # type error: Argument 4 to "add_to_node" of "Converter" has incompatible type "Node"; expected "Identifier"
258            self.add_to_node(graph, s, p, o, node, nodemap)  # type: ignore[arg-type]
259
260        return node
261
262    def add_to_node(
263        self,
264        graph: Graph,
265        s: IdentifiedNode,
266        p: IdentifiedNode,
267        o: Identifier,
268        s_node: Dict[str, Any],
269        nodemap,
270    ):
271        context = self.context
272
273        if isinstance(o, Literal):
274            datatype = str(o.datatype) if o.datatype else None
275            language = o.language
276            term = context.find_term(str(p), datatype, language=language)
277        else:
278            containers = [LIST, None] if graph.value(o, RDF.first) else [None]
279            for container in containers:
280                for coercion in (ID, VOCAB, UNDEF):
281                    # type error: Argument 2 to "find_term" of "Context" has incompatible type "object"; expected "Union[str, Defined, None]"
282                    # type error: Argument 3 to "find_term" of "Context" has incompatible type "Optional[str]"; expected "Union[Defined, str]"
283                    term = context.find_term(str(p), coercion, container)  # type: ignore[arg-type]
284                    if term:
285                        break
286                if term:
287                    break
288
289        node = None
290        use_set = not context.active
291
292        if term:
293            p_key = term.name
294
295            if term.type:
296                node = self.type_coerce(o, term.type)
297            # type error: "Identifier" has no attribute "language"
298            elif term.language and o.language == term.language:  # type: ignore[attr-defined]
299                node = str(o)
300            # type error: Right operand of "and" is never evaluated
301            elif context.language and (term.language is None and o.language is None):  # type: ignore[unreachable]
302                node = str(o)  # type: ignore[unreachable]
303
304            if LIST in term.container:
305                node = [
306                    self.type_coerce(v, term.type)
307                    or self.to_raw_value(graph, s, v, nodemap)
308                    for v in self.to_collection(graph, o)
309                ]
310            elif LANG in term.container and language:
311                value = s_node.setdefault(p_key, {})
312                values = value.get(language)
313                node = str(o)
314                if values or SET in term.container:
315                    if not isinstance(values, list):
316                        value[language] = values = [values]
317                    values.append(node)
318                else:
319                    value[language] = node
320                return
321            elif SET in term.container:
322                use_set = True
323
324        else:
325            p_key = context.to_symbol(p)
326            # TODO: for coercing curies - quite clumsy; unify to_symbol and find_term?
327            key_term = context.terms.get(p_key)
328            if key_term and (key_term.type or key_term.container):
329                p_key = p
330            if not term and p == RDF.type and not self.use_rdf_type:
331                if isinstance(o, URIRef):
332                    node = context.to_symbol(o)
333                p_key = context.type_key
334
335        if node is None:
336            node = self.to_raw_value(graph, s, o, nodemap)
337
338        value = s_node.get(p_key)
339        if value:
340            if not isinstance(value, list):
341                value = [value]
342            value.append(node)
343        elif use_set:
344            value = [node]
345        else:
346            value = node
347        s_node[p_key] = value
348
349    def type_coerce(self, o: Identifier, coerce_type: str):
350        if coerce_type == ID:
351            if isinstance(o, URIRef):
352                return self.context.shrink_iri(o)
353            elif isinstance(o, BNode):
354                return o.n3()
355            else:
356                return o
357        elif coerce_type == VOCAB and isinstance(o, URIRef):
358            return self.context.to_symbol(o)
359        elif isinstance(o, Literal) and str(o.datatype) == coerce_type:
360            return o
361        else:
362            return None
363
364    def to_raw_value(
365        self, graph: Graph, s: IdentifiedNode, o: Identifier, nodemap: Dict[str, Any]
366    ):
367        context = self.context
368        coll = self.to_collection(graph, o)
369        if coll is not None:
370            coll = [
371                self.to_raw_value(graph, s, lo, nodemap)
372                for lo in self.to_collection(graph, o)
373            ]
374            return {context.list_key: coll}
375        elif isinstance(o, BNode):
376            embed = (
377                False  # TODO: self.context.active or using startnode and only one ref
378            )
379            onode = self.process_subject(graph, o, nodemap)
380            if onode:
381                if embed and not any(s2 for s2 in graph.subjects(None, o) if s2 != s):
382                    return onode
383                else:
384                    nodemap[onode[context.id_key]] = onode
385            return {context.id_key: o.n3()}
386        elif isinstance(o, URIRef):
387            # TODO: embed if o != startnode (else reverse)
388            return {context.id_key: context.shrink_iri(o)}
389        elif isinstance(o, Literal):
390            # TODO: if compact
391            native = self.use_native_types and o.datatype in PLAIN_LITERAL_TYPES
392            if native:
393                v = o.toPython()
394            else:
395                v = str(o)
396            if o.datatype:
397                if native and self.context.active:
398                    return v
399                return {
400                    context.type_key: context.to_symbol(o.datatype),
401                    context.value_key: v,
402                }
403            elif o.language and o.language != context.language:
404                return {context.lang_key: o.language, context.value_key: v}
405            # type error: Right operand of "and" is never evaluated
406            elif not context.active or context.language and not o.language:  # type: ignore[unreachable]
407                return {context.value_key: v}
408            else:
409                return v
410
411    def to_collection(self, graph: Graph, l_: Identifier):
412        if l_ != RDF.nil and not graph.value(l_, RDF.first):
413            return None
414        list_nodes: List[Optional[_ObjectType]] = []
415        chain = set([l_])
416        while l_:
417            if l_ == RDF.nil:
418                return list_nodes
419            if isinstance(l_, URIRef):
420                return None
421            first, rest = None, None
422            for p, o in graph.predicate_objects(l_):
423                if not first and p == RDF.first:
424                    first = o
425                elif not rest and p == RDF.rest:
426                    rest = o
427                elif p != RDF.type or o != RDF.List:
428                    return None
429            list_nodes.append(first)
430            # type error: Incompatible types in assignment (expression has type "Optional[Node]", variable has type "Identifier")
431            l_ = rest  # type: ignore[assignment]
432            if l_ in chain:
433                return None
434            chain.add(l_)