baseimage-tutorial-framework/lib/tfw/networking/serialization.py

22 lines
452 B
Python
Raw Normal View History

import json
2018-02-02 16:30:26 +00:00
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(key, data):
return [encode_if_needed(frame) for frame in (key, json.dumps(data))]
2018-02-02 16:30:26 +00:00
def deserialize_all(key, data):
return decode_if_needed(key), json.loads(data)