Skip to content

Instantly share code, notes, and snippets.

@jstitch
Last active December 22, 2015 11:59
Show Gist options
  • Save jstitch/6469151 to your computer and use it in GitHub Desktop.
Save jstitch/6469151 to your computer and use it in GitHub Desktop.
Method to serialize the contents of data structures in Python
def serializeFields(data):
try:
it = iter(data)
except TypeError as terr:
return unicode(data)
if type(it) == type(iter([])):
l = []
for e in it:
l.append(serializeFields(e))
return l
elif type(it) == type(iter({})):
d = {}
for k in it:
d[k] = serializeFields(data[k])
return d
# elif ... tuples? sets?
return unicode(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment