summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/sqlite
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-11-29 10:50:44 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2019-11-29 10:54:12 -0500
commitbb338b91752f4f758edd9b2549a228e891596ae0 (patch)
treee92e30b621c41bf4ce706cb04c90f99cc6fa8cd9 /lib/sqlalchemy/dialects/sqlite
parent06b0892da049a995ee4cd1e7cc3c1eb68f3dde64 (diff)
downloadsqlalchemy-bb338b91752f4f758edd9b2549a228e891596ae0.tar.gz
Gracefully degrade for SQLite JSON receiving direct numeric value
Fixed issue to workaround SQLite's behavior of assigning "numeric" affinity to JSON datatypes, first described at :ref:`change_3850`, which returns scalar numeric JSON values as a number and not as a string that can be JSON deserialized. The SQLite-specific JSON deserializer now gracefully degrades for this case as an exception and bypasses deserialization for single numeric values, as from a JSON perspective they are already deserialized. Also adds a combinatoric fixture for JSON single values within the dialect-general test suite. Fixes: #5014 Change-Id: Id38221dce1271fec527ca198b23908547b25d8a0
Diffstat (limited to 'lib/sqlalchemy/dialects/sqlite')
-rw-r--r--lib/sqlalchemy/dialects/sqlite/base.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py
index 2685a9243..f228c151c 100644
--- a/lib/sqlalchemy/dialects/sqlite/base.py
+++ b/lib/sqlalchemy/dialects/sqlite/base.py
@@ -570,6 +570,7 @@ When using the per-:class:`.Engine` execution option, note that
""" # noqa
import datetime
+import numbers
import re
from .json import JSON
@@ -599,6 +600,24 @@ from ...types import TIMESTAMP # noqa
from ...types import VARCHAR # noqa
+class _SQliteJson(JSON):
+ def result_processor(self, dialect, coltype):
+ default_processor = super(_SQliteJson, self).result_processor(
+ dialect, coltype
+ )
+
+ def process(value):
+ try:
+ return default_processor(value)
+ except TypeError:
+ if isinstance(value, numbers.Number):
+ return value
+ else:
+ raise
+
+ return process
+
+
class _DateTimeMixin(object):
_reg = None
_storage_format = None
@@ -899,7 +918,7 @@ class TIME(_DateTimeMixin, sqltypes.Time):
colspecs = {
sqltypes.Date: DATE,
sqltypes.DateTime: DATETIME,
- sqltypes.JSON: JSON,
+ sqltypes.JSON: _SQliteJson,
sqltypes.JSON.JSONIndexType: JSONIndexType,
sqltypes.JSON.JSONPathType: JSONPathType,
sqltypes.Time: TIME,