Smart serialize of type object (#224)

* Smart serialize of type object

* Added decimal type as object (#225)

Co-authored-by: Bjerkelund <k.bjerkelund@wilhelmsen.com>

Co-authored-by: Kristoffer <43015594+ludokriss@users.noreply.github.com>
Co-authored-by: Bjerkelund <k.bjerkelund@wilhelmsen.com>
This commit is contained in:
Laurent Mazuel 2020-12-29 16:13:47 -08:00 коммит произвёл GitHub
Родитель c16e5218fe
Коммит 209fa61faf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 47 добавлений и 0 удалений

Просмотреть файл

@ -953,6 +953,16 @@ class Serializer(object):
return self.serialize_long(attr) return self.serialize_long(attr)
if obj_type is unicode_str: if obj_type is unicode_str:
return self.serialize_unicode(attr) return self.serialize_unicode(attr)
if obj_type is datetime.datetime:
return self.serialize_iso(attr)
if obj_type is datetime.date:
return self.serialize_date(attr)
if obj_type is datetime.time:
return self.serialize_time(attr)
if obj_type is datetime.timedelta:
return self.serialize_duration(attr)
if obj_type is decimal.Decimal:
return self.serialize_decimal(attr)
# If it's a model or I know this dependency, serialize as a Model # If it's a model or I know this dependency, serialize as a Model
elif obj_type in self.dependencies.values() or isinstance(attr, Model): elif obj_type in self.dependencies.values() or isinstance(attr, Model):

Просмотреть файл

@ -25,6 +25,7 @@
# #
#-------------------------------------------------------------------------- #--------------------------------------------------------------------------
from decimal import Decimal
import sys import sys
import json import json
import isodate import isodate
@ -1395,6 +1396,42 @@ class TestRuntimeSerialized(unittest.TestCase):
'data': {'id': u"\ua015"} 'data': {'id': u"\ua015"}
} }
def test_datetime_types_as_type_object(self):
"""https://github.com/Azure/msrest-for-python/issues/223
"""
class TestModel(Model):
_attribute_map = {'data': {'key': 'data', 'type': 'object'}}
m = TestModel(data = {
'datetime': isodate.parse_datetime('2012-02-24T00:53:52.780Z'),
'date': date(2019,5,1),
'time': time(11,12,13),
'timedelta': timedelta(56)
})
serialized = m.serialize()
assert serialized['data'] == {
'datetime': '2012-02-24T00:53:52.780Z',
'date': '2019-05-01',
'time': '11:12:13',
'timedelta': 'P56D'
}
def test_decimal_types_as_type_object(self):
"""https://github.com/Azure/msrest-for-python/issues/223
"""
class TestModel(Model):
_attribute_map = {'data': {'key': 'data', 'type': 'object'}}
m = TestModel(data = {
'decimal': Decimal('1.1'),
})
serialized = m.serialize()
assert serialized['data'] == {
'decimal': 1.1
}
def test_json_with_xml_map(self): def test_json_with_xml_map(self):
basic_json = {'age': 37, 'country': 'france'} basic_json = {'age': 37, 'country': 'france'}