mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-09 01:47:16 +00:00
38 lines
812 B
Python
38 lines
812 B
Python
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
import json
|
|
|
|
|
|
def serialize_all(*args):
|
|
return tuple(_serialize_single(arg) for arg in args)
|
|
|
|
|
|
def deserialize_all(*args):
|
|
return tuple(_deserialize_single(arg) for arg in args)
|
|
|
|
|
|
def _serialize_single(data):
|
|
if not isinstance(data, str):
|
|
data = json.dumps(data)
|
|
return encode_if_needed(data)
|
|
|
|
|
|
def _deserialize_single(data):
|
|
try:
|
|
return json.loads(data)
|
|
except ValueError:
|
|
return decode_if_needed(data)
|
|
|
|
|
|
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
|