def test_nested_serialization(self): """ Default with nested serializers is to include full serialization of child elements. """ expected = { 'first_name': 'john', 'last_name': 'doe', 'age': 42, 'siblings': [{ 'first_name': 'jane', 'last_name': 'doe', 'age': 44, 'partner': { 'first_name': 'fred', 'last_name': 'bloggs', 'age': 41, } }, { 'first_name': 'emily', 'last_name': 'doe', 'age': 37, }] } self.assertEquals( ObjectSerializer().serialize('python', self.obj, nested=True), expected)
def test_depth_one_serialization(self): """ If 'nested' is greater than 0 then nested objects should be serialized as flat values once the specified depth has been reached. """ expected = { 'first_name': 'john', 'last_name': 'doe', 'age': 42, 'siblings': [{ 'first_name': 'jane', 'last_name': 'doe', 'age': 44, 'partner': 'fred bloggs' }, { 'first_name': 'emily', 'last_name': 'doe', 'age': 37, }] } self.assertEquals( ObjectSerializer().serialize('python', self.obj, nested=1), expected)
def test_serialization_only_includes_instance_properties(self): """ By default only serialize instance properties, not class properties. """ expected = {'first_name': 'john', 'last_name': 'doe', 'age': 42} self.assertEquals(ObjectSerializer().serialize('python', self.obj), expected)
def test_serialize_basic_object(self): """ Objects are serialized by converting into dictionaries. """ expected = {'a': 1, 'b': 'foo', 'c': True} self.assertEquals(ObjectSerializer().serialize('python', self.obj), expected)
def test_recursive_serialization(self): """ If recursion occurs, serializer will fall back to flat values. """ expected = { 'first_name': 'john', 'last_name': 'doe', 'age': 42, 'daughter': { 'first_name': 'emily', 'last_name': 'doe', 'age': 37, 'father': 'john doe' } } self.assertEquals( ObjectSerializer().serialize('python', self.obj, nested=True), expected)
def list(self, request): products = Object.objects.all() serializer = ObjectSerializer(products, many=True) data = JSONRenderer().render(serializer.data) return Response(data)
def test_dict(self): obj = {} expected = '{}' output = ObjectSerializer().serialize('json', obj) self.assertEquals(output, expected)
def test_list(self): obj = [] expected = '[]' output = ObjectSerializer().serialize('json', obj) self.assertEquals(output, expected)
class CustomSerializer(ObjectSerializer): full_name = ObjectSerializer(label='Full name') details = DetailsSerializer(label='Details', source='*') class Meta: fields = ('full_name', 'details')
class DetailsSerializer(ObjectSerializer): first_name = ObjectSerializer(label='First name') last_name = ObjectSerializer(label='Last name') class Meta: fields = ('first_name', 'last_name')
class CustomSerializer(ObjectSerializer): full_name = ObjectSerializer(label='Full name') age = ObjectSerializer(label='Age') class Meta: fields = ('full_name', 'age')
class CustomSerializer(ObjectSerializer): full_name = ObjectSerializer()
def test_xml(self): expected = '<?xml version="1.0" encoding="utf-8"?>\n<object><a>1</a><b>foo</b><c>True</c></object>' output = ObjectSerializer().serialize('xml', self.obj) self.assertEquals(output, expected)
def test_yaml(self): expected = '{a: 1, b: foo, c: true}\n' output = ObjectSerializer().serialize('yaml', self.obj) self.assertEquals(output, expected)
def test_json(self): expected = '{"a": 1, "b": "foo", "c": true}' output = ObjectSerializer().serialize('json', self.obj) self.assertEquals(output, expected)