<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pieceofpy.com &#187; sqlalchemy pylons json</title>
	<atom:link href="http://pieceofpy.com/tag/sqlalchemy-pylons-json/feed/" rel="self" type="application/rss+xml" />
	<link>http://pieceofpy.com</link>
	<description>Another python blog with a love for agile methods.</description>
	<lastBuildDate>Tue, 18 May 2010 15:31:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>SQLalchemy and JSON w/ Pylons &#8211; Best Practices</title>
		<link>http://pieceofpy.com/2009/08/17/sqlalchemy-and-json-w-pylons-best-practices/</link>
		<comments>http://pieceofpy.com/2009/08/17/sqlalchemy-and-json-w-pylons-best-practices/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 16:32:26 +0000</pubDate>
		<dc:creator>Wayne</dc:creator>
				<category><![CDATA[pylons]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[sqlalchemy pylons json]]></category>

		<guid isPermaLink="false">http://pieceofpy.com/?p=316</guid>
		<description><![CDATA[I asked the question I Stackoverflow and maybe it was too generic for the site, since it just got trolled with &#8220;Google keyword&#8221; by some d-bag. So I deleted it and figured I&#8217;d throw it up on my blog a see about getting some feedback from the people who read this pile about. The reason [...]]]></description>
			<content:encoded><![CDATA[<p>I asked the question I Stackoverflow and maybe it was too generic for the site, since it just got trolled with &#8220;Google keyword&#8221; by some d-bag. So I deleted it and figured I&#8217;d throw it up on my blog a see about getting some feedback from the people who read this pile about. The reason I ask this is mainly because I am preparing to do some updated screencasts for Pylons.</p>
<p>I&#8217;ve seen multiple ways referenced in official docs and I have done it a few different ways myself. I am using Pylons and I am curious what the best practices are for this common scenario?</p>
<p>I have used something similar to this for auto-magically making the conversion happen.</p>
<pre class="brush: php">
# The auto-magic version
# I pulled this off a blog, forget the source.
def _sa_to_dict(obj):
    for item in obj.__dict__.items():
        if item[0][0] is &#039;_&#039;:
            continue
        if isinstance(item[1], str):
            yield [item[0], item[1].decode()]
        else:
            yield item

def json(obj):
    if isinstance(obj, list):
        return dumps(map(dict, map(_sa_to_dict, obj)))
    else:
        return dumps(dict(_sa_to_dict(obj)))

# here is the controller
@jsonify
def index(self, format=&#039;html&#039;):
    templates = Session.query(Template).all()
    if format == &#039;json&#039;:
        return json(templates)
</pre>
<p>I have also done the version where you use the jsonify decorator and build your dictionary manually, something like this, which is ok if I need to define some custom behavior for my JSON, but as the default behavior seems excessive.</p>
<pre class="brush: php">
@jsonify
def index(self, format=&#039;html&#039;):
    if format == &#039;json&#039;:
        q = Session.query
        templates = [{&#039;id&#039;: t.id,
                      &#039;title&#039;: t.title,
                      &#039;body&#039;: t.body} for t in q(Template)]
        return templates
</pre>
<p>I&#8217;ve also created an inherited SA class which defines a json method and have used that on all my objects to convert them to JSON. Similar to the the fedora extensions.</p>
<p>Maybe I missed some obviously library out there or some obvious helper in the Pylons packages, but I feel like this is a very common task being done a dozen different ways between docs, source, and my own personal projects. Curious what others are doing / using.</p>
]]></content:encoded>
			<wfw:commentRss>http://pieceofpy.com/2009/08/17/sqlalchemy-and-json-w-pylons-best-practices/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
