1
+ from __future__ import annotations
2
+
1
3
import json
2
- from typing import IO , Any , Dict
4
+ from typing import IO , Any , Dict , Mapping , MutableSequence , Optional
3
5
4
- from rdflib import BNode , Literal , URIRef , Variable
5
6
from rdflib .query import Result , ResultException , ResultParser , ResultSerializer
7
+ from rdflib .term import BNode , Identifier , Literal , URIRef , Variable
6
8
7
9
"""A Serializer for SPARQL results in JSON:
8
10
17
19
18
20
19
21
class JSONResultParser (ResultParser ):
20
- def parse (self , source , content_type = None ):
22
+ # type error: Signature of "parse" incompatible with supertype "ResultParser"
23
+ def parse (self , source : IO , content_type : Optional [str ] = None ) -> Result : # type: ignore[override]
21
24
inp = source .read ()
22
25
if isinstance (inp , bytes ):
23
26
inp = inp .decode ("utf-8" )
24
27
return JSONResult (json .loads (inp ))
25
28
26
29
27
30
class JSONResultSerializer (ResultSerializer ):
28
- def __init__ (self , result ):
31
+ def __init__ (self , result : Result ):
29
32
ResultSerializer .__init__ (self , result )
30
33
31
- def serialize (self , stream : IO , encoding : str = None ): # type: ignore[override]
34
+ # type error: Signature of "serialize" incompatible with supertype "ResultSerializer"
35
+ def serialize (self , stream : IO , encoding : str = None ) -> None : # type: ignore[override]
32
36
33
37
res : Dict [str , Any ] = {}
34
38
if self .result .type == "ASK" :
@@ -49,7 +53,7 @@ def serialize(self, stream: IO, encoding: str = None): # type: ignore[override]
49
53
else :
50
54
stream .write (r )
51
55
52
- def _bindingToJSON (self , b ) :
56
+ def _bindingToJSON (self , b : Mapping [ Variable , Identifier ]) -> Dict [ Variable , Any ] :
53
57
res = {}
54
58
for var in b :
55
59
j = termToJSON (self , b [var ])
@@ -59,7 +63,7 @@ def _bindingToJSON(self, b):
59
63
60
64
61
65
class JSONResult (Result ):
62
- def __init__ (self , json ):
66
+ def __init__ (self , json : Dict [ str , Any ] ):
63
67
self .json = json
64
68
if "boolean" in json :
65
69
type_ = "ASK"
@@ -76,17 +80,17 @@ def __init__(self, json):
76
80
self .bindings = self ._get_bindings ()
77
81
self .vars = [Variable (x ) for x in json ["head" ]["vars" ]]
78
82
79
- def _get_bindings (self ):
80
- ret = []
83
+ def _get_bindings (self ) -> MutableSequence [ Mapping [ Variable , Identifier ]] :
84
+ ret : MutableSequence [ Mapping [ Variable , Identifier ]] = []
81
85
for row in self .json ["results" ]["bindings" ]:
82
- outRow = {}
86
+ outRow : Dict [ Variable , Identifier ] = {}
83
87
for k , v in row .items ():
84
88
outRow [Variable (k )] = parseJsonTerm (v )
85
89
ret .append (outRow )
86
90
return ret
87
91
88
92
89
- def parseJsonTerm (d ) :
93
+ def parseJsonTerm (d : Dict [ str , str ]) -> Identifier :
90
94
"""rdflib object (Literal, URIRef, BNode) for the given json-format dict.
91
95
92
96
input is like:
@@ -107,7 +111,9 @@ def parseJsonTerm(d):
107
111
raise NotImplementedError ("json term type %r" % t )
108
112
109
113
110
- def termToJSON (self , term ):
114
+ def termToJSON (
115
+ self : JSONResultSerializer , term : Optional [Identifier ]
116
+ ) -> Optional [Dict [str , str ]]:
111
117
if isinstance (term , URIRef ):
112
118
return {"type" : "uri" , "value" : str (term )}
113
119
elif isinstance (term , Literal ):
0 commit comments