[storm] How to JSON encode a item from a ResultSet
Mario Zito
mazito at analyte.com
Fri May 28 00:42:12 BST 2010
Just for the record, this is the right way to do it, in case it may be of
help to someone else:
*
*
*def encode_storm_object(object):*
* ''' Serializes to JSON a Storm object*
* *
* Use:*
* from storm.info import get_cls_info*
* import json*
* ...*
* storm_object = get_storm_object()*
* print json.dumps(storm_object, default=encode_storm_object)*
* *
* Warnings:*
* Serializes objects containing Int, Date and Unicode data types*
* other datatypes are not tested. MUST be improved*
* '''*
* if not hasattr(object, "__storm_table__"):*
* raise TypeError(repr(object) + " is not JSON serializable")*
* result = {}*
* cls_info = get_cls_info(object.__class__)*
* for name in cls_info.attributes.iterkeys():*
* value= getattr(object, name)*
* if (isinstance(value, date)): *
* value= str(value)*
* result[name] = value*
* return result*
Mario
--
Mario A. Zito
ANALYTE SRL
www.analyte.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/storm/attachments/20100527/0a116171/attachment.htm
More information about the storm
mailing list