Use compact encoding for JSON websocket messages (#67148)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2022-02-23 20:15:20 -10:00 committed by GitHub
parent 5366c0e3e3
commit c9e46d360b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View file

@ -480,7 +480,9 @@ async def handle_subscribe_trigger(
msg["id"], {"variables": variables, "context": context} msg["id"], {"variables": variables, "context": context}
) )
connection.send_message( connection.send_message(
json.dumps(message, cls=ExtendedJSONEncoder, allow_nan=False) json.dumps(
message, cls=ExtendedJSONEncoder, allow_nan=False, separators=(",", ":")
)
) )
connection.subscriptions[msg["id"]] = ( connection.subscriptions[msg["id"]] = (

View file

@ -53,4 +53,6 @@ SIGNAL_WEBSOCKET_DISCONNECTED: Final = "websocket_disconnected"
# Data used to store the current connection list # Data used to store the current connection list
DATA_CONNECTIONS: Final = f"{DOMAIN}.connections" DATA_CONNECTIONS: Final = f"{DOMAIN}.connections"
JSON_DUMP: Final = partial(json.dumps, cls=JSONEncoder, allow_nan=False) JSON_DUMP: Final = partial(
json.dumps, cls=JSONEncoder, allow_nan=False, separators=(",", ":")
)

View file

@ -83,13 +83,13 @@ async def test_message_to_json(caplog):
json_str = message_to_json({"id": 1, "message": "xyz"}) json_str = message_to_json({"id": 1, "message": "xyz"})
assert json_str == '{"id": 1, "message": "xyz"}' assert json_str == '{"id":1,"message":"xyz"}'
json_str2 = message_to_json({"id": 1, "message": _Unserializeable()}) json_str2 = message_to_json({"id": 1, "message": _Unserializeable()})
assert ( assert (
json_str2 json_str2
== '{"id": 1, "type": "result", "success": false, "error": {"code": "unknown_error", "message": "Invalid JSON in response"}}' == '{"id":1,"type":"result","success":false,"error":{"code":"unknown_error","message":"Invalid JSON in response"}}'
) )
assert "Unable to serialize to JSON" in caplog.text assert "Unable to serialize to JSON" in caplog.text