summaryrefslogtreecommitdiff
path: root/jsonpatch.py
diff options
context:
space:
mode:
authorStefan Kögl <stefan@skoegl.net>2013-10-09 18:55:22 +0200
committerStefan Kögl <stefan@skoegl.net>2013-10-09 18:55:22 +0200
commit4d0389238e1d963f859af42a9a0fa38483a7fa41 (patch)
tree5bf298b1e6fda6bb159767729e1a94584feb552e /jsonpatch.py
parentbd90a7a39d1307f78ad89103e960d249aafacacb (diff)
downloadpython-json-patch-4d0389238e1d963f859af42a9a0fa38483a7fa41.tar.gz
handle duplicate JSON keys only when possible
Diffstat (limited to 'jsonpatch.py')
-rw-r--r--jsonpatch.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index 7601248..b1c01a6 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -85,6 +85,27 @@ def multidict(ordered_pairs):
return dict(d)
+def get_loadjson():
+ """ adds the object_pairs_hook parameter to json.load when possible
+
+ The "object_pairs_hook" parameter is used to handle duplicate keys when
+ loading a JSON object. This parameter does not exist in Python 2.6. This
+ methods returns an unmodified json.load for Python 2.6 and a partial
+ function with object_pairs_hook set to multidict for Python versions that
+ support the parameter. """
+
+ import inspect
+ import functools
+
+ argspec = inspect.getargspec(json.load)
+ if 'object_pairs_hook' not in argspec.args:
+ return json.load
+
+ return functools.partial(json.load, object_pairs_hook=multidict)
+
+json.load = get_loadjson()
+
+
def apply_patch(doc, patch, in_place=False):
"""Apply list of patches to specified json document.
@@ -231,7 +252,7 @@ class JsonPatch(object):
:return: :class:`JsonPatch` instance.
"""
- patch = json.loads(patch_str, object_pairs_hook=multidict)
+ patch = json.loads(patch_str)
return cls(patch)
@classmethod