tools (common/data_format): serialise now check types and return a default value when empty string is parsed.
This commit is contained in:
parent
c18a40e7cc
commit
af64d24d52
1 changed files with 11 additions and 2 deletions
|
|
@ -133,10 +133,19 @@ def serialise(data):
|
|||
"""
|
||||
return json.dumps(data, ensure_ascii=False)
|
||||
|
||||
def deserialise(serialised_data):
|
||||
def deserialise(serialised_data, default=None, type_check=dict):
|
||||
"""Deserialize data from bridge
|
||||
|
||||
@param serialised_data(unicode): data to deserialise
|
||||
@default (object): value to use when serialised data is empty string
|
||||
@param type_check(type): if not None, the deserialised data must be of this type
|
||||
@return(object): deserialised data
|
||||
@raise ValueError: serialised_data is of wrong type
|
||||
"""
|
||||
return json.loads(serialised_data)
|
||||
if serialised_data == u"":
|
||||
return default
|
||||
ret = json.loads(serialised_data)
|
||||
if type_check is not None and not isinstance(ret, type_check):
|
||||
raise ValueError(u"Bad data type, was expecting {type_check}, got {real_type}"
|
||||
.format(type_check=type_check, real_type=type(ret)))
|
||||
return ret
|
||||
|
|
|
|||
Loading…
Reference in a new issue