ansible_mitogen: Replace use of `ansible.parsing.utils.jsonify.jsonify()`

The function is Ansible >= 12 (ansible-core >= 2.19). See #1274 for analysis
of `json.dumps()` vs `jsonify()` differences. This change is a middle ground
between full backward compatibility and using `json.dumps()` unadorned.

- if `data` is `None`, then it will still be transferred as `{}` on older
  versions of Ansible, but 'null' in newer releases. Cases where 'null'
  caused a problem are suspected/reported, but no reproducers are available.
- `ensure_ascii=True` will be still be tried, with fallback. I believe this
  is only relevant on Python 2.x.
- `sort_keys=True` will no longer be used.
- No indentation/pretty printing will be applied, this remains unchanged

fixes #1274
pull/1280/head
Alex Willmer 6 months ago
parent 0d6aa99370
commit 667dd4237a

@ -29,6 +29,7 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import json
import logging
import os
import pwd
@ -42,7 +43,6 @@ import ansible.vars.clean
from ansible.module_utils.common.text.converters import to_bytes, to_text
from ansible.module_utils.six.moves import shlex_quote
from ansible.parsing.utils.jsonify import jsonify
import mitogen.core
import mitogen.select
@ -219,8 +219,13 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase):
Used by the base _execute_module(), and in <2.4 also by the template
action module, and probably others.
"""
if data is None and ansible_mitogen.utils.ansible_version[:2] <= (2, 18):
data = '{}'
if isinstance(data, dict):
data = jsonify(data)
try:
data = json.dumps(data, ensure_ascii=False)
except UnicodeDecodeError:
data = json.dumps(data)
if not isinstance(data, bytes):
data = to_bytes(data, errors='surrogate_or_strict')

@ -32,6 +32,8 @@ Running Ansible 12 + Mitogen will currently print a deprecation warning
Ansible + Mitogen will still work for now. Mitogen is considering alternatives
to strategy plugins under :gh:issue:`1278`.
* :gh:issue:`1274` :mod:`ansible_mitogen`: Replace use of `jsonify()`, which
is deprecated form Ansible 12 (ansible-core 2.19)
v0.3.25a1 (2025-06-05)
----------------------

Loading…
Cancel
Save