Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions simple_seismic_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ def do_GET(self):

def send_json(self, data):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(data, separators=(',', ':')).encode())
self.wfile.write(json.dumps(data, separators=(',', ':')).encode('utf-8'))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since you are explicitly setting the charset to UTF-8 in the headers and encoding the output as UTF-8, you should set ensure_ascii=False in the json.dumps call. By default, json.dumps escapes non-ASCII characters (e.g., \u1234), which increases payload size and negates the benefit of using a UTF-8 encoding for the transport. Setting this to False will result in smaller payloads for non-ASCII data and is more consistent with your goal of 'robust character serialization'. Additionally, note that .encode('utf-8') is the default for .encode() in Python 3, making that part of the change redundant but harmless.

Suggested change
self.wfile.write(json.dumps(data, separators=(',', ':')).encode('utf-8'))
self.wfile.write(json.dumps(data, separators=(',', ':'), ensure_ascii=False).encode('utf-8'))


def log_message(self, format, *args):
"""Override to customize logging"""
Expand Down
Loading