[storm] How to JSON encode a item from a ResultSet

Mario Zito mazito at analyte.com
Thu May 27 23:19:49 BST 2010


Jamu:

Yes, testing also indicates that Date attrs were not serialized.
I will test other datatypes while I work on the appl code.
So this is the corrected code.

*def encode_storm_object(object):*
*     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 ("<type 'datetime.date'>" == str(value.__class__)): *
*             value= str(value)*
*         result[name] = value*
*     return result*

*json_str = json.dumps(storm_obj, default=encode_storm_object)*

Sorry, but the only way that worked was using the string "*<type
'datetime.date'>*" to test the object class.
I tried using *isinstance(obj, "datetime.date")* but did not work.
Maybe you have a better solution here to check the attr class ... I am new
in Python :-(

Mario

2010/5/27 Jamu Kakar <jkakar at kakar.ca>

> Hi,
>
> On Thu, May 27, 2010 at 11:08 PM, Mario Zito <mazito at analyte.com> wrote:
> > Ok, it works with VERY minor corrections:
>
> Yeah, not testing is a great way to write code that doesn't
> work. ;)  Thanks for posting the fixes.
>
> Take care,
> J.
>



-- 
Mario A. Zito
ANALYTE SRL
Parana 457, piso 2, of. 'A'
(C1033AAI) Buenos Aires, Argentina
tel: (54-11) 5258-0205 int 138
mazito at analyte.com
www.analyte.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/storm/attachments/20100527/41b1faf4/attachment.htm 


More information about the storm mailing list