mirror of
https://github.com/nicolargo/glances.git
synced 2024-11-23 20:45:33 +03:00
chg: globals - standardize json_dumps return type to bytes
This commit is contained in:
parent
46054afd22
commit
538013d192
@ -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 = {}
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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]:
|
||||
|
Loading…
Reference in New Issue
Block a user