<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, &quot;__storm_table__&quot;):</b></div>
<div><b>         raise TypeError(repr(object) + &quot; is not JSON serializable&quot;)</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 &quot;hassattr&quot; with two &quot;ss&quot; and &quot;__storm_table&quot; corrected to &quot;__storm_table__&quot; </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">&lt;<a href="mailto:jkakar@kakar.ca">jkakar@kakar.ca</a>&gt;</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 &lt;<a href="mailto:mazito@analyte.com">mazito@analyte.com</a>&gt; wrote:<br>
&gt; I would like to iterate over all items of a returned ResultSet encoding each<br>
&gt; one using JSON. Example code follows:<br>
&gt; class Topic(object):<br>
&gt;     __storm_table__= &quot;Trancos.topic&quot;<br>
&gt;     id = Int(primary=True)<br>
&gt;     account_id= Int()<br>
&gt;     title= Unicode()<br>
&gt;     properties= Unicode()<br>
&gt;     notes= Unicode()<br>
&gt;     # and other attrs not important here ...<br>
<br>
</div>8&lt; snip<br>
<div class="im"><br>
&gt;     rs= store.find(Topic)<br>
&gt;     for t in rs:<br>
&gt;         print <a href="http://t.id" target="_blank">t.id</a>, json.dumps(t)<br>
&gt; But I get the error:<br>
&gt;   File &quot;/usr/lib/python2.6/json/encoder.py&quot;, line 344, in default<br>
&gt;     raise TypeError(repr(o) + &quot; is not JSON serializable&quot;)<br>
&gt; TypeError: &lt;__main__.Topic object at 0x26f3090&gt; is not JSON serializable<br>
&gt;<br>
&gt; Any ideas ? Is there a way to serialize a Storm object (or a full ResultSet)<br>
&gt; 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 &#39;Specializing JSON object encoding&#39; 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 {&quot;id&quot;: <a href="http://topic.id" target="_blank">topic.id</a>, &quot;account_id&quot;: topic.account_id,<br>
                  &quot;title&quot;: topic.title, &quot;properties&quot;: topic.properties,<br>
                  &quot;notes&quot;: topic.notes}<br>
      raise TypeError(repr(topic) + &quot; is not JSON serializable&quot;)<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, &quot;__storm_table&quot;):<br>
          raise TypeError(repr(object) + &quot; is not JSON serializable&quot;)<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&#39;t dump non-Storm fields though, which may or may not be a<br>
problem depending on your needs.  Hopefully it&#39;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. &#39;A&#39;<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>