Skip to content

Commit 538013d

Browse files
committed
chg: globals - standardize json_dumps return type to bytes
1 parent 46054af commit 538013d

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

glances/exports/glances_json/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def export(self, name, columns, points):
4747
logger.debug(f"Exporting stats ({listkeys(self.buffer)}) to JSON file ({self.json_filename})")
4848

4949
# Export stats to JSON file
50-
with open(self.json_filename, "w") as self.json_file:
51-
self.json_file.write(f"{json_dumps(self.buffer)}\n")
50+
with open(self.json_filename, "wb") as self.json_file:
51+
self.json_file.write(json_dumps(self.buffer) + b'\n')
5252

5353
# Reset buffer
5454
self.buffer = {}

glances/exports/glances_kafka/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def init(self):
5252
try:
5353
s = KafkaProducer(
5454
bootstrap_servers=server_uri,
55-
value_serializer=lambda v: json_dumps(v).encode('utf-8'),
55+
value_serializer=lambda v: json_dumps(v),
5656
compression_type=self.compression,
5757
)
5858
except Exception as e:

glances/exports/glances_zeromq/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import sys
1212

1313
import zmq
14-
from zmq.utils.strtypes import asbytes
1514

1615
from glances.exports.export import GlancesExport
1716
from glances.globals import b, json_dumps
@@ -81,7 +80,7 @@ def export(self, name, columns, points):
8180
# - First frame containing the following prefix (STRING)
8281
# - Second frame with the Glances plugin name (STRING)
8382
# - Third frame with the Glances plugin stats (JSON)
84-
message = [b(self.prefix), b(name), asbytes(json_dumps(data))]
83+
message = [b(self.prefix), b(name), json_dumps(data)]
8584

8685
# Write data to the ZeroMQ bus
8786
# Result can be view: tcp://host:port

glances/globals.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,17 @@ def urlopen_auth(url, username, password):
322322
)
323323

324324

325-
def json_dumps(data) -> str:
325+
def json_dumps(data) -> bytes:
326326
"""Return the object data in a JSON format.
327327
328328
Manage the issue #815 for Windows OS with UnicodeDecodeError catching.
329329
"""
330330
try:
331-
return json.dumps(data)
331+
res = json.dumps(data)
332332
except UnicodeDecodeError:
333-
return json.dumps(data, ensure_ascii=False)
333+
res = json.dumps(data, ensure_ascii=False)
334+
# ujson & json libs return strings, but our contract expects bytes
335+
return b(res)
334336

335337

336338
def json_loads(data: Union[str, bytes, bytearray]) -> Union[Dict, List]:

0 commit comments

Comments
 (0)