_json.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """Implementation of the JSON adaptation objects
  2. This module exists to avoid a circular import problem: pyscopg2.extras depends
  3. on psycopg2.extension, so I can't create the default JSON typecasters in
  4. extensions importing register_json from extras.
  5. """
  6. # psycopg/_json.py - Implementation of the JSON adaptation objects
  7. #
  8. # Copyright (C) 2012-2019 Daniele Varrazzo <daniele.varrazzo@gmail.com>
  9. # Copyright (C) 2020 The Psycopg Team
  10. #
  11. # psycopg2 is free software: you can redistribute it and/or modify it
  12. # under the terms of the GNU Lesser General Public License as published
  13. # by the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # In addition, as a special exception, the copyright holders give
  17. # permission to link this program with the OpenSSL library (or with
  18. # modified versions of OpenSSL that use the same license as OpenSSL),
  19. # and distribute linked combinations including the two.
  20. #
  21. # You must obey the GNU Lesser General Public License in all respects for
  22. # all of the code used other than OpenSSL.
  23. #
  24. # psycopg2 is distributed in the hope that it will be useful, but WITHOUT
  25. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  26. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  27. # License for more details.
  28. import json
  29. from psycopg2._psycopg import ISQLQuote, QuotedString
  30. from psycopg2._psycopg import new_type, new_array_type, register_type
  31. from psycopg2.compat import PY2
  32. # oids from PostgreSQL 9.2
  33. JSON_OID = 114
  34. JSONARRAY_OID = 199
  35. # oids from PostgreSQL 9.4
  36. JSONB_OID = 3802
  37. JSONBARRAY_OID = 3807
  38. class Json(object):
  39. """
  40. An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
  41. :sql:`json` data type.
  42. `!Json` can be used to wrap any object supported by the provided *dumps*
  43. function. If none is provided, the standard :py:func:`json.dumps()` is
  44. used.
  45. """
  46. def __init__(self, adapted, dumps=None):
  47. self.adapted = adapted
  48. self._conn = None
  49. self._dumps = dumps or json.dumps
  50. def __conform__(self, proto):
  51. if proto is ISQLQuote:
  52. return self
  53. def dumps(self, obj):
  54. """Serialize *obj* in JSON format.
  55. The default is to call `!json.dumps()` or the *dumps* function
  56. provided in the constructor. You can override this method to create a
  57. customized JSON wrapper.
  58. """
  59. return self._dumps(obj)
  60. def prepare(self, conn):
  61. self._conn = conn
  62. def getquoted(self):
  63. s = self.dumps(self.adapted)
  64. qs = QuotedString(s)
  65. if self._conn is not None:
  66. qs.prepare(self._conn)
  67. return qs.getquoted()
  68. if PY2:
  69. def __str__(self):
  70. return self.getquoted()
  71. else:
  72. def __str__(self):
  73. # getquoted is binary in Py3
  74. return self.getquoted().decode('ascii', 'replace')
  75. def register_json(conn_or_curs=None, globally=False, loads=None,
  76. oid=None, array_oid=None, name='json'):
  77. """Create and register typecasters converting :sql:`json` type to Python objects.
  78. :param conn_or_curs: a connection or cursor used to find the :sql:`json`
  79. and :sql:`json[]` oids; the typecasters are registered in a scope
  80. limited to this object, unless *globally* is set to `!True`. It can be
  81. `!None` if the oids are provided
  82. :param globally: if `!False` register the typecasters only on
  83. *conn_or_curs*, otherwise register them globally
  84. :param loads: the function used to parse the data into a Python object. If
  85. `!None` use `!json.loads()`, where `!json` is the module chosen
  86. according to the Python version (see above)
  87. :param oid: the OID of the :sql:`json` type if known; If not, it will be
  88. queried on *conn_or_curs*
  89. :param array_oid: the OID of the :sql:`json[]` array type if known;
  90. if not, it will be queried on *conn_or_curs*
  91. :param name: the name of the data type to look for in *conn_or_curs*
  92. The connection or cursor passed to the function will be used to query the
  93. database and look for the OID of the :sql:`json` type (or an alternative
  94. type if *name* if provided). No query is performed if *oid* and *array_oid*
  95. are provided. Raise `~psycopg2.ProgrammingError` if the type is not found.
  96. """
  97. if oid is None:
  98. oid, array_oid = _get_json_oids(conn_or_curs, name)
  99. JSON, JSONARRAY = _create_json_typecasters(
  100. oid, array_oid, loads=loads, name=name.upper())
  101. register_type(JSON, not globally and conn_or_curs or None)
  102. if JSONARRAY is not None:
  103. register_type(JSONARRAY, not globally and conn_or_curs or None)
  104. return JSON, JSONARRAY
  105. def register_default_json(conn_or_curs=None, globally=False, loads=None):
  106. """
  107. Create and register :sql:`json` typecasters for PostgreSQL 9.2 and following.
  108. Since PostgreSQL 9.2 :sql:`json` is a builtin type, hence its oid is known
  109. and fixed. This function allows specifying a customized *loads* function
  110. for the default :sql:`json` type without querying the database.
  111. All the parameters have the same meaning of `register_json()`.
  112. """
  113. return register_json(conn_or_curs=conn_or_curs, globally=globally,
  114. loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)
  115. def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
  116. """
  117. Create and register :sql:`jsonb` typecasters for PostgreSQL 9.4 and following.
  118. As in `register_default_json()`, the function allows to register a
  119. customized *loads* function for the :sql:`jsonb` type at its known oid for
  120. PostgreSQL 9.4 and following versions. All the parameters have the same
  121. meaning of `register_json()`.
  122. """
  123. return register_json(conn_or_curs=conn_or_curs, globally=globally,
  124. loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb')
  125. def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
  126. """Create typecasters for json data type."""
  127. if loads is None:
  128. loads = json.loads
  129. def typecast_json(s, cur):
  130. if s is None:
  131. return None
  132. return loads(s)
  133. JSON = new_type((oid, ), name, typecast_json)
  134. if array_oid is not None:
  135. JSONARRAY = new_array_type((array_oid, ), "%sARRAY" % name, JSON)
  136. else:
  137. JSONARRAY = None
  138. return JSON, JSONARRAY
  139. def _get_json_oids(conn_or_curs, name='json'):
  140. # lazy imports
  141. from psycopg2.extensions import STATUS_IN_TRANSACTION
  142. from psycopg2.extras import _solve_conn_curs
  143. conn, curs = _solve_conn_curs(conn_or_curs)
  144. # Store the transaction status of the connection to revert it after use
  145. conn_status = conn.status
  146. # column typarray not available before PG 8.3
  147. typarray = conn.info.server_version >= 80300 and "typarray" or "NULL"
  148. # get the oid for the hstore
  149. curs.execute(
  150. "SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;"
  151. % typarray, (name,))
  152. r = curs.fetchone()
  153. # revert the status of the connection as before the command
  154. if conn_status != STATUS_IN_TRANSACTION and not conn.autocommit:
  155. conn.rollback()
  156. if not r:
  157. raise conn.ProgrammingError("%s data type not found" % name)
  158. return r