<div>Jamu:</div>Ok, it works with VERY minor corrections:<div><br></div><div>The resulting code is:</div><div><br></div><div><div><b>def encode_storm_object(object):</b></div><div><b> if not hasattr(object, "__storm_table__"):</b></div>
<div><b> raise TypeError(repr(object) + " is not JSON serializable")</b></div><div><b><br></b></div><div><b> result = {}</b></div><div><b> cls_info = get_cls_info(object.__class__)</b></div><div>
<b> for name in cls_info.attributes.iterkeys():</b></div><div><b> result[name] = getattr(object, name)</b></div><div><b> return result</b></div><div><br></div>Only errors were "hassattr" with two "ss" and "__storm_table" corrected to "__storm_table__" </div>
<div><br></div><div>Thanks again !</div><div>Mario</div><div><br><div class="gmail_quote">2010/5/27 Jamu Kakar <span dir="ltr"><<a href="mailto:jkakar@kakar.ca">jkakar@kakar.ca</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Mario,<br>
<div class="im"><br>
On Thu, May 27, 2010 at 10:14 PM, Mario Zito <<a href="mailto:mazito@analyte.com">mazito@analyte.com</a>> wrote:<br>
> I would like to iterate over all items of a returned ResultSet encoding each<br>
> one using JSON. Example code follows:<br>
> class Topic(object):<br>
> __storm_table__= "Trancos.topic"<br>
> id = Int(primary=True)<br>
> account_id= Int()<br>
> title= Unicode()<br>
> properties= Unicode()<br>
> notes= Unicode()<br>
> # and other attrs not important here ...<br>
<br>
</div>8< snip<br>
<div class="im"><br>
> rs= store.find(Topic)<br>
> for t in rs:<br>
> print <a href="http://t.id" target="_blank">t.id</a>, json.dumps(t)<br>
> But I get the error:<br>
> File "/usr/lib/python2.6/json/encoder.py", line 344, in default<br>
> raise TypeError(repr(o) + " is not JSON serializable")<br>
> TypeError: <__main__.Topic object at 0x26f3090> is not JSON serializable<br>
><br>
> Any ideas ? Is there a way to serialize a Storm object (or a full ResultSet)<br>
> to JSON ?<br>
<br>
</div>You need to create a custom encoder for use with simplejson. One<br>
way forward is to create a custom encoder for each of your Storm<br>
objects. See the 'Specializing JSON object encoding' section in the<br>
simplejson documentation for more information [1]. Something like<br>
this should work (untested):<br>
<br>
import simplejson<br>
<br>
def encode_topic(topic):<br>
if isinstance(topic, Topic):<br>
return {"id": <a href="http://topic.id" target="_blank">topic.id</a>, "account_id": topic.account_id,<br>
"title": topic.title, "properties": topic.properties,<br>
"notes": topic.notes}<br>
raise TypeError(repr(topic) + " is not JSON serializable")<br>
<br>
print simplejson.dumps(topic, default=encode_complex)<br>
<br>
This will get tedious if you have many different kinds of objects to<br>
convert, so you might want to create a generic encoder function that<br>
will work with any Storm object, for example (also untested):<br>
<br>
import simplejson as json<br>
from <a href="http://storm.info" target="_blank">storm.info</a> import get_cls_info<br>
<br>
def encode_storm_object(object):<br>
if not hassattr(object, "__storm_table"):<br>
raise TypeError(repr(object) + " is not JSON serializable")<br>
<br>
result = {}<br>
cls_info = get_cls_info(object.__class__)<br>
for name in cls_info.attributes.iterkeys():<br>
result[name] = getattr(object, name)<br>
return result<br>
<br>
storm_object = get_storm_object()<br>
print simplejson.dumps(storm_object, default=encode_storm_object)<br>
<br>
This won't dump non-Storm fields though, which may or may not be a<br>
problem depending on your needs. Hopefully it's enough to get you<br>
started doing what you want.<br>
<br>
Thanks,<br>
J.<br>
<br>
[1] <a href="http://simplejson.googlecode.com/svn/tags/simplejson-2.1.1/docs/index.html" target="_blank">http://simplejson.googlecode.com/svn/tags/simplejson-2.1.1/docs/index.html</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Mario A. Zito<br>ANALYTE SRL<br>Parana 457, piso 2, of. 'A'<br>(C1033AAI) Buenos Aires, Argentina<br>tel: (54-11) 5258-0205 int 138<br><a href="mailto:mazito@analyte.com">mazito@analyte.com</a><br>
<a href="http://www.analyte.com">www.analyte.com</a><br>
</div>