forked from orbit-oss/flask
use dict instead of if/else logic (#2093)
This commit is contained in:
parent
7f28837167
commit
0e79aba40d
1 changed files with 14 additions and 10 deletions
|
|
@ -84,21 +84,25 @@ class TaggedJSONSerializer(object):
|
||||||
def dumps(self, value):
|
def dumps(self, value):
|
||||||
return json.dumps(_tag(value), separators=(',', ':'))
|
return json.dumps(_tag(value), separators=(',', ':'))
|
||||||
|
|
||||||
|
LOADS_MAP = {
|
||||||
|
' t': tuple,
|
||||||
|
' u': uuid.UUID,
|
||||||
|
' b': b64decode,
|
||||||
|
' m': Markup,
|
||||||
|
' d': parse_date,
|
||||||
|
}
|
||||||
|
|
||||||
def loads(self, value):
|
def loads(self, value):
|
||||||
def object_hook(obj):
|
def object_hook(obj):
|
||||||
if len(obj) != 1:
|
if len(obj) != 1:
|
||||||
return obj
|
return obj
|
||||||
the_key, the_value = next(iteritems(obj))
|
the_key, the_value = next(iteritems(obj))
|
||||||
if the_key == ' t':
|
# Check the key for a corresponding function
|
||||||
return tuple(the_value)
|
return_function = self.LOADS_MAP.get(the_key)
|
||||||
elif the_key == ' u':
|
if return_function:
|
||||||
return uuid.UUID(the_value)
|
# Pass the value to the function
|
||||||
elif the_key == ' b':
|
return return_function(the_value)
|
||||||
return b64decode(the_value)
|
# Didn't find a function for this object
|
||||||
elif the_key == ' m':
|
|
||||||
return Markup(the_value)
|
|
||||||
elif the_key == ' d':
|
|
||||||
return parse_date(the_value)
|
|
||||||
return obj
|
return obj
|
||||||
return json.loads(value, object_hook=object_hook)
|
return json.loads(value, object_hook=object_hook)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue