Thursday, April 7, 2011

import json

class Subscriber(object):
firstname
= None
lastname
= None


class Post(object):
author
= None
title
= None


def decode_from_dict(cls,vals):
obj
= cls()
for key, val in vals.items():
setattr
(obj, key, val)
return obj


SERIALIZABLE_CLASSES
= {'Subscriber': Subscriber,
'Post': Post}

def decode_object(d):
for field in d:
if field in SERIALIZABLE_CLASSES:
cls
= SERIALIZABLE_CLASSES[field]
return decode_from_dict(cls, d[field])
return d


results
= '''[{"Subscriber": {"firstname": "Neal", "lastname": "Walters"}},
{"Post": {"author": {"Subscriber": {"firstname": "Neal",
"lastname": "Walters"}}},
"title": "Decoding JSON Objects"}]'''