From 538013d19242741258874773f606b15383e41e94 Mon Sep 17 00:00:00 2001 From: RazCrimson <52282402+RazCrimson@users.noreply.github.com> Date: Sun, 8 Sep 2024 13:12:28 +0530 Subject: [PATCH] chg: globals - standardize json_dumps return type to bytes --- glances/exports/glances_json/__init__.py | 4 ++-- glances/exports/glances_kafka/__init__.py | 2 +- glances/exports/glances_zeromq/__init__.py | 3 +-- glances/globals.py | 8 +++++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/glances/exports/glances_json/__init__.py b/glances/exports/glances_json/__init__.py index 1a2aa6df..c12784f4 100644 --- a/glances/exports/glances_json/__init__.py +++ b/glances/exports/glances_json/__init__.py @@ -47,8 +47,8 @@ class Export(GlancesExport): logger.debug(f"Exporting stats ({listkeys(self.buffer)}) to JSON file ({self.json_filename})") # Export stats to JSON file - with open(self.json_filename, "w") as self.json_file: - self.json_file.write(f"{json_dumps(self.buffer)}\n") + with open(self.json_filename, "wb") as self.json_file: + self.json_file.write(json_dumps(self.buffer) + b'\n') # Reset buffer self.buffer = {} diff --git a/glances/exports/glances_kafka/__init__.py b/glances/exports/glances_kafka/__init__.py index 783a4ad1..11ceace9 100644 --- a/glances/exports/glances_kafka/__init__.py +++ b/glances/exports/glances_kafka/__init__.py @@ -52,7 +52,7 @@ class Export(GlancesExport): try: s = KafkaProducer( bootstrap_servers=server_uri, - value_serializer=lambda v: json_dumps(v).encode('utf-8'), + value_serializer=lambda v: json_dumps(v), compression_type=self.compression, ) except Exception as e: diff --git a/glances/exports/glances_zeromq/__init__.py b/glances/exports/glances_zeromq/__init__.py index 6bc2eaf5..5f5924cd 100644 --- a/glances/exports/glances_zeromq/__init__.py +++ b/glances/exports/glances_zeromq/__init__.py @@ -11,7 +11,6 @@ import sys import zmq -from zmq.utils.strtypes import asbytes from glances.exports.export import GlancesExport from glances.globals import b, json_dumps @@ -81,7 +80,7 @@ class Export(GlancesExport): # - First frame containing the following prefix (STRING) # - Second frame with the Glances plugin name (STRING) # - Third frame with the Glances plugin stats (JSON) - message = [b(self.prefix), b(name), asbytes(json_dumps(data))] + message = [b(self.prefix), b(name), json_dumps(data)] # Write data to the ZeroMQ bus # Result can be view: tcp://host:port diff --git a/glances/globals.py b/glances/globals.py index 86e4f6b2..111a1715 100644 --- a/glances/globals.py +++ b/glances/globals.py @@ -322,15 +322,17 @@ def urlopen_auth(url, username, password): ) -def json_dumps(data) -> str: +def json_dumps(data) -> bytes: """Return the object data in a JSON format. Manage the issue #815 for Windows OS with UnicodeDecodeError catching. """ try: - return json.dumps(data) + res = json.dumps(data) except UnicodeDecodeError: - return json.dumps(data, ensure_ascii=False) + res = json.dumps(data, ensure_ascii=False) + # ujson & json libs return strings, but our contract expects bytes + return b(res) def json_loads(data: Union[str, bytes, bytearray]) -> Union[Dict, List]: