From 16d98c75ca40a539b354f5c0edfe01f813b6897b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Sun, 15 Jul 2018 17:26:00 +0200 Subject: [PATCH] Add message_bytes() function to serialization module --- lib/tfw/networking/__init__.py | 4 ++-- lib/tfw/networking/serialization.py | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/tfw/networking/__init__.py b/lib/tfw/networking/__init__.py index c5879b7..c1f3eb4 100644 --- a/lib/tfw/networking/__init__.py +++ b/lib/tfw/networking/__init__.py @@ -1,9 +1,9 @@ # Copyright (C) 2018 Avatao.com Innovative Learning Kft. # All Rights Reserved. See LICENSE file for details. -from .serialization import serialize_tfw_msg, deserialize_tfw_msg, with_deserialize_tfw_msg +from .serialization import serialize_tfw_msg, deserialize_tfw_msg +from .serialization import with_deserialize_tfw_msg, message_bytes from .zmq_connector_base import ZMQConnectorBase -# from .controller_connector import ControllerConnector # TODO: readd once controller stuff is resolved from .message_sender import MessageSender from .event_handlers.server_connector import ServerUplinkConnector as TFWServerConnector from .server.tfw_server import TFWServer diff --git a/lib/tfw/networking/serialization.py b/lib/tfw/networking/serialization.py index c28eec0..c54609b 100644 --- a/lib/tfw/networking/serialization.py +++ b/lib/tfw/networking/serialization.py @@ -67,10 +67,14 @@ def _serialize_single(data): (serialize input if it is JSON) """ if not isinstance(data, str): - data = json.dumps(data) + data = message_bytes(data) return _encode_if_needed(data) +def message_bytes(message): + return json.dumps(message, sort_keys=True).encode() + + def _deserialize_single(data): """ Try parsing input as JSON, return it as