summaryrefslogtreecommitdiff
path: root/redis/commands/json/decoders.py
diff options
context:
space:
mode:
Diffstat (limited to 'redis/commands/json/decoders.py')
-rw-r--r--redis/commands/json/decoders.py65
1 files changed, 56 insertions, 9 deletions
diff --git a/redis/commands/json/decoders.py b/redis/commands/json/decoders.py
index 0ee102a..b19395c 100644
--- a/redis/commands/json/decoders.py
+++ b/redis/commands/json/decoders.py
@@ -1,12 +1,59 @@
-def int_or_list(b):
- if isinstance(b, int):
- return b
- else:
- return b
+from ..helpers import nativestr
+import re
+import copy
+
+def bulk_of_jsons(d):
+ """Replace serialized JSON values with objects in a
+ bulk array response (list).
+ """
-def int_or_none(b):
- if b is None:
- return None
- if isinstance(b, int):
+ def _f(b):
+ for index, item in enumerate(b):
+ if item is not None:
+ b[index] = d(item)
return b
+
+ return _f
+
+
+def decode_dict_keys(obj):
+ """Decode the keys of the given dictionary with utf-8."""
+ newobj = copy.copy(obj)
+ for k in obj.keys():
+ if isinstance(k, bytes):
+ newobj[k.decode("utf-8")] = newobj[k]
+ newobj.pop(k)
+ return newobj
+
+
+def unstring(obj):
+ """
+ Attempt to parse string to native integer formats.
+ One can't simply call int/float in a try/catch because there is a
+ semantic difference between (for example) 15.0 and 15.
+ """
+ floatreg = '^\\d+.\\d+$'
+ match = re.findall(floatreg, obj)
+ if match != []:
+ return float(match[0])
+
+ intreg = "^\\d+$"
+ match = re.findall(intreg, obj)
+ if match != []:
+ return int(match[0])
+ return obj
+
+
+def decode_list(b):
+ """
+ Given a non-deserializable object, make a best effort to
+ return a useful set of results.
+ """
+ if isinstance(b, list):
+ return [nativestr(obj) for obj in b]
+ elif isinstance(b, bytes):
+ return unstring(nativestr(b))
+ elif isinstance(b, str):
+ return unstring(b)
+ return b