baseimage-tutorial-framework/lib/tfw/networking/serialization.py
2018-02-02 18:39:06 +01:00

22 lines
468 B
Python

import json
def encode_if_needed(value):
if isinstance(value, str):
value = value.encode('utf-8')
return value
def decode_if_needed(value):
if isinstance(value, (bytes, bytearray)):
value = value.decode('utf-8')
return value
def serialize_all(anchor, message):
return [encode_if_needed(a) for a in (anchor, json.dumps(message))]
def deserialize_all(anchor, message):
return decode_if_needed(anchor), json.loads(message)