diff options
author | Bruno Rocha <rochacbruno@users.noreply.github.com> | 2016-10-11 12:31:53 -0300 |
---|---|---|
committer | Toshio Kuratomi <a.badger@gmail.com> | 2016-10-11 08:33:47 -0700 |
commit | 3e58f0815502c2bec8aab9994cf29cf7a837a06a (patch) | |
tree | 3ebc9a4f5c2c474afce165114c2c0a0ac850f3af | |
parent | 972379c9072e1debeefce7a7bf99ffbe1e9217c4 (diff) | |
download | ansible-3e58f0815502c2bec8aab9994cf29cf7a837a06a.tar.gz |
Fix unbound method call for JSONEncoder (#17970)
* Fix unbound method call for JSONEncoder
The way it is currently it will lead to unbound method error
```python
In [1]: import json
In [2]: json.JSONEncoder.default('object_here')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-872fdacfda50> in <module>()
----> 1 json.JSONEncoder.default('object_here')
TypeError: unbound method default() must be called with JSONEncoder instance as first argument (got str instance instead)
```
But what is really wanted is to let the json module to raise the "is not serializable error" which demands a bounded instance of `JSONEncoder()`
```python
In [3]: json.JSONEncoder().default('object_here')
---------------------------------------------------------------------------
TypeError: 'object_here' is not JSON serializable
```
BTW: I think it would try to call `.to_json` of object before raising as it is a common pattern.
* Calling JSONEncoder bounded `default` method using super()
(cherry picked from commit b06fb2022c7534620238a7da00ad26018433156c)
-rw-r--r-- | lib/ansible/plugins/filter/core.py | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index 6222de5c75..ecff72da48 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -68,7 +68,7 @@ class AnsibleJSONEncoder(json.JSONEncoder): if isinstance(o, HostVars): return dict(o) else: - return json.JSONEncoder.default(o) + return super(AnsibleJSONEncoder, self).default(o) def to_yaml(a, *args, **kw): '''Make verbose, human readable yaml''' |