mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #22118 from surajssd/issue-21721
Datetime support for msgpack for issue-21721
This commit is contained in:
commit
adb50caba1
@ -10,6 +10,7 @@ from __future__ import absolute_import
|
||||
# import sys # Use if sys is commented out below
|
||||
import logging
|
||||
import gc
|
||||
import datetime
|
||||
|
||||
# Import salt libs
|
||||
import salt.log
|
||||
@ -140,7 +141,35 @@ class Serial(object):
|
||||
else:
|
||||
return obj
|
||||
return msgpack.dumps(verylong_encoder(msg))
|
||||
except TypeError:
|
||||
except TypeError as e:
|
||||
# msgpack doesn't support datetime.datetime datatype
|
||||
# So here we have converted datetime.datetime to custom datatype
|
||||
# This is msgpack Extended types numbered 78
|
||||
def default(obj):
|
||||
return msgpack.ExtType(78, obj)
|
||||
|
||||
def dt_encode(obj):
|
||||
datetime_str = obj.strftime("%Y%m%dT%H:%M:%S.%f")
|
||||
return msgpack.packb(datetime_str, default=default)
|
||||
|
||||
def datetime_encoder(obj):
|
||||
if isinstance(obj, dict):
|
||||
for key, value in six.iteritems(obj.copy()):
|
||||
obj[key] = datetime_encoder(value)
|
||||
return dict(obj)
|
||||
elif isinstance(obj, (list, tuple)):
|
||||
obj = list(obj)
|
||||
for idx, entry in enumerate(obj):
|
||||
obj[idx] = datetime_encoder(entry)
|
||||
return obj
|
||||
if isinstance(obj, datetime.datetime):
|
||||
return dt_encode(obj)
|
||||
else:
|
||||
return obj
|
||||
|
||||
if "datetime.datetime" in str(e):
|
||||
return msgpack.dumps(datetime_encoder(msg))
|
||||
|
||||
if msgpack.version >= (0, 2, 0):
|
||||
# Should support OrderedDict serialization, so, let's
|
||||
# raise the exception
|
||||
|
Loading…
Reference in New Issue
Block a user