mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-09-08 17:44:11 +00:00
Document serialization module
This commit is contained in:
parent
d30226d55b
commit
293d7972f1
@ -29,10 +29,16 @@ def validate_message(message):
|
|||||||
|
|
||||||
|
|
||||||
def serialize_tfw_msg(message):
|
def serialize_tfw_msg(message):
|
||||||
|
"""
|
||||||
|
Create TFW multipart data from message dict
|
||||||
|
"""
|
||||||
return _serialize_all(message['key'], message)
|
return _serialize_all(message['key'], message)
|
||||||
|
|
||||||
|
|
||||||
def deserialize_tfw_msg(*args):
|
def deserialize_tfw_msg(*args):
|
||||||
|
"""
|
||||||
|
Return message from TFW multipart data
|
||||||
|
"""
|
||||||
return _deserialize_all(*args)[1]
|
return _deserialize_all(*args)[1]
|
||||||
|
|
||||||
|
|
||||||
@ -45,12 +51,20 @@ def _deserialize_all(*args):
|
|||||||
|
|
||||||
|
|
||||||
def _serialize_single(data):
|
def _serialize_single(data):
|
||||||
|
"""
|
||||||
|
Return input as bytes
|
||||||
|
(serialize input if it is JSON)
|
||||||
|
"""
|
||||||
if not isinstance(data, str):
|
if not isinstance(data, str):
|
||||||
data = json.dumps(data)
|
data = json.dumps(data)
|
||||||
return _encode_if_needed(data)
|
return _encode_if_needed(data)
|
||||||
|
|
||||||
|
|
||||||
def _deserialize_single(data):
|
def _deserialize_single(data):
|
||||||
|
"""
|
||||||
|
Try parsing input as JSON, return it as
|
||||||
|
string if parsing fails.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
return json.loads(data)
|
return json.loads(data)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
@ -58,12 +72,20 @@ def _deserialize_single(data):
|
|||||||
|
|
||||||
|
|
||||||
def _encode_if_needed(value):
|
def _encode_if_needed(value):
|
||||||
|
"""
|
||||||
|
Return input as bytes
|
||||||
|
(encode if input is string)
|
||||||
|
"""
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
value = value.encode('utf-8')
|
value = value.encode('utf-8')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def _decode_if_needed(value):
|
def _decode_if_needed(value):
|
||||||
|
"""
|
||||||
|
Return input as string
|
||||||
|
(decode if input is bytes)
|
||||||
|
"""
|
||||||
if isinstance(value, (bytes, bytearray)):
|
if isinstance(value, (bytes, bytearray)):
|
||||||
value = value.decode('utf-8')
|
value = value.decode('utf-8')
|
||||||
return value
|
return value
|
||||||
|
Loading…
Reference in New Issue
Block a user