jsonresults.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """A Serializer for SPARQL results in JSON:
  2. http://www.w3.org/TR/rdf-sparql-json-res/
  3. Bits and pieces borrowed from:
  4. http://projects.bigasterisk.com/sparqlhttp/
  5. Authors: Drew Perttula, Gunnar Aastrand Grimnes
  6. """
  7. from __future__ import annotations
  8. import json
  9. from typing import IO, Any, Dict, Mapping, MutableSequence, Optional
  10. from rdflib.query import Result, ResultException, ResultParser, ResultSerializer
  11. from rdflib.term import BNode, Identifier, Literal, URIRef, Variable
  12. try:
  13. import orjson
  14. _HAS_ORJSON = True
  15. except ImportError:
  16. orjson = None # type: ignore[assignment, unused-ignore]
  17. _HAS_ORJSON = False
  18. class JSONResultParser(ResultParser):
  19. """Parses SPARQL JSON results into a Result object."""
  20. # type error: Signature of "parse" incompatible with supertype "ResultParser"
  21. def parse(self, source: IO, content_type: Optional[str] = None) -> Result: # type: ignore[override]
  22. inp = source.read()
  23. if _HAS_ORJSON:
  24. try:
  25. loaded = orjson.loads(inp)
  26. except Exception as e:
  27. raise ResultException(f"Failed to parse result: {e}")
  28. else:
  29. if isinstance(inp, bytes):
  30. inp = inp.decode("utf-8")
  31. loaded = json.loads(inp)
  32. return JSONResult(loaded)
  33. class JSONResultSerializer(ResultSerializer):
  34. """Serializes SPARQL results to JSON format."""
  35. def __init__(self, result: Result):
  36. ResultSerializer.__init__(self, result)
  37. # type error: Signature of "serialize" incompatible with supertype "ResultSerializer"
  38. def serialize(self, stream: IO, encoding: str = None) -> None: # type: ignore[override]
  39. res: Dict[str, Any] = {}
  40. if self.result.type == "ASK":
  41. res["head"] = {}
  42. res["boolean"] = self.result.askAnswer
  43. else:
  44. # select
  45. res["results"] = {}
  46. res["head"] = {}
  47. res["head"]["vars"] = self.result.vars
  48. res["results"]["bindings"] = [
  49. self._bindingToJSON(x) for x in self.result.bindings
  50. ]
  51. if _HAS_ORJSON:
  52. try:
  53. r_bytes = orjson.dumps(res, option=orjson.OPT_NON_STR_KEYS)
  54. except Exception as e:
  55. raise ResultException(f"Failed to serialize result: {e}")
  56. if encoding is not None:
  57. # Note, orjson will always write utf-8 even if
  58. # encoding is specified as something else.
  59. try:
  60. stream.write(r_bytes)
  61. except (TypeError, ValueError):
  62. stream.write(r_bytes.decode("utf-8"))
  63. else:
  64. stream.write(r_bytes.decode("utf-8"))
  65. else:
  66. r_str = json.dumps(res, allow_nan=False, ensure_ascii=False)
  67. if encoding is not None:
  68. try:
  69. stream.write(r_str.encode(encoding))
  70. except (TypeError, ValueError):
  71. stream.write(r_str)
  72. else:
  73. stream.write(r_str)
  74. def _bindingToJSON(self, b: Mapping[Variable, Identifier]) -> Dict[Variable, Any]:
  75. res = {}
  76. for var in b:
  77. j = termToJSON(self, b[var])
  78. if j is not None:
  79. res[var] = termToJSON(self, b[var])
  80. return res
  81. class JSONResult(Result):
  82. def __init__(self, json: Dict[str, Any]):
  83. self.json = json
  84. if "boolean" in json:
  85. type_ = "ASK"
  86. elif "results" in json:
  87. type_ = "SELECT"
  88. else:
  89. raise ResultException("No boolean or results in json!")
  90. Result.__init__(self, type_)
  91. if type_ == "ASK":
  92. self.askAnswer = bool(json["boolean"])
  93. else:
  94. self.bindings = self._get_bindings()
  95. self.vars = [Variable(x) for x in json["head"]["vars"]]
  96. def _get_bindings(self) -> MutableSequence[Mapping[Variable, Identifier]]:
  97. ret: MutableSequence[Mapping[Variable, Identifier]] = []
  98. for row in self.json["results"]["bindings"]:
  99. outRow: Dict[Variable, Identifier] = {}
  100. for k, v in row.items():
  101. outRow[Variable(k)] = parseJsonTerm(v)
  102. ret.append(outRow)
  103. return ret
  104. def parseJsonTerm(d: Dict[str, str]) -> Identifier:
  105. """rdflib object (Literal, URIRef, BNode) for the given json-format dict.
  106. input is like:
  107. ```json
  108. { 'type': 'uri', 'value': 'http://famegame.com/2006/01/username' }
  109. { 'type': 'literal', 'value': 'drewp' }
  110. ```
  111. """
  112. t = d["type"]
  113. if t == "uri":
  114. return URIRef(d["value"])
  115. elif t == "literal":
  116. return Literal(d["value"], datatype=d.get("datatype"), lang=d.get("xml:lang"))
  117. elif t == "typed-literal":
  118. return Literal(d["value"], datatype=URIRef(d["datatype"]))
  119. elif t == "bnode":
  120. return BNode(d["value"])
  121. else:
  122. raise NotImplementedError("json term type %r" % t)
  123. def termToJSON(
  124. self: JSONResultSerializer, term: Optional[Identifier]
  125. ) -> Optional[Dict[str, str]]:
  126. if isinstance(term, URIRef):
  127. return {"type": "uri", "value": str(term)}
  128. elif isinstance(term, Literal):
  129. r = {"type": "literal", "value": str(term)}
  130. if term.datatype is not None:
  131. r["datatype"] = str(term.datatype)
  132. if term.language is not None:
  133. r["xml:lang"] = term.language
  134. return r
  135. elif isinstance(term, BNode):
  136. return {"type": "bnode", "value": str(term)}
  137. elif term is None:
  138. return None
  139. else:
  140. raise ResultException("Unknown term type: %s (%s)" % (term, type(term)))