chg: globals - standardize json_dumps return type to bytes

pull/2938/head
RazCrimson 2024-09-08 13:12:28 +05:30
parent 46054afd22
commit 538013d192
4 changed files with 9 additions and 8 deletions

View File

@ -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 = {}

View File

@ -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:

View File

@ -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

View File

@ -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]: