From 667dd4237afc8f930ea00d098ce95df95f51dfff Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Tue, 10 Jun 2025 14:39:56 +0100 Subject: [PATCH] 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 --- ansible_mitogen/mixins.py | 9 +++++++-- docs/changelog.rst | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index 18518d69..8ddbb437 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -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') diff --git a/docs/changelog.rst b/docs/changelog.rst index 5b2ec785..f0327e0f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) ----------------------