canonicalize json
This commit is contained in:
parent
f406942984
commit
97ae800e6c
3 changed files with 36 additions and 16 deletions
|
@ -1,3 +1,6 @@
|
|||
import collections
|
||||
|
||||
|
||||
def get_app_url(config):
|
||||
""" Returns the application's URL, based on the given config. """
|
||||
return '%s://%s' % (config['PREFERRED_URL_SCHEME'], config['SERVER_HOSTNAME'])
|
||||
|
@ -16,3 +19,13 @@ def slash_join(*args):
|
|||
|
||||
args = [rmslash(path) for path in args]
|
||||
return '/'.join(args)
|
||||
|
||||
|
||||
def canonicalize(json_obj):
|
||||
""" Returns a JSON object sorted by key. """
|
||||
if isinstance(json_obj, collections.MutableMapping):
|
||||
sorted_obj = sorted({key: canonicalize(val) for key, val in json_obj}.items())
|
||||
return collections.OrderedDict(sorted_obj)
|
||||
elif isinstance(json_obj, (list, tuple)):
|
||||
return [canonicalize(val) for val in json_obj]
|
||||
return json_obj
|
||||
|
|
Reference in a new issue