Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add serializer for json object parameters #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/unit/base/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,18 @@ def test_list(self):
]
actual = serialize.prefixed_collapsible_map(value, 'Prefix')
self.assertEqual({}, actual)


class ObjectTestCase(unittest.TestCase):
def test_object(self):
actual = serialize.object({'twilio': 'rocks'})
self.assertEqual('{"twilio": "rocks"}', actual)

def test_list(self):
actual = serialize.object(['twilio', 'rocks'])
self.assertEqual('["twilio", "rocks"]', actual)

def test_does_not_change_other_types(self):
actual = serialize.object('{"attribute":"value"}')
self.assertEqual('{"attribute":"value"}', actual)

11 changes: 11 additions & 0 deletions twilio/base/serialize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import json

from twilio.base import values

Expand Down Expand Up @@ -52,3 +53,13 @@ def flatten_dict(d, result={}, prv_keys=[]):
return {'{}.{}'.format(prefix, k): v for k, v in flattened.items()}

return {}


def object(obj):
"""
Return a jsonified string represenation of obj if obj is jsonifiable else
return obj untouched
"""
if isinstance(obj, dict) or isinstance(obj, list):
return json.dumps(obj)
return obj