Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1052 (closed enhancement: fixed)

Opened 13 years ago

Last modified 9 years ago

jsonify for SQLAlchemy mapped objects

Reported by: Arnar Owned by: anonymous
Priority: normal Milestone: 1.0
Component: SQLAlchemy Version: 0.9a6
Severity: normal Keywords:


The following jsonify-methods seem to work for SA-mapped objects.

from turbojson.jsonify import jsonify

import sqlalchemy

@jsonify.when("isinstance(obj, sqlalchemy.attributes.InstrumentedList)")
def jsonify_salist(obj):
   return list(obj)

@jsonify.when("hasattr(obj, 'c') and isinstance(obj.c,
def jsonify_saobject(obj):
   props = {}
   for key in obj.c.keys():
       props[key] = getattr(obj, key)
   return props

Change History

comment:1 Changed 13 years ago by jorge.vargas

  • Priority changed from normal to high
  • Component changed from TurboGears to SQLAlchemy
  • Milestone set to 1.0b2

this should go in ASAP

comment:2 Changed 13 years ago by alberto

The previous snippet within a patch (preferably with accompanying tests) would be great.

I might have some time to do it, however I have some questions:

  1. Where should I stuff this in? I vote for a new turbogears/json.py file which just gets imported by __init__.py (though I haven't tested it's viability regarding circular imports). This could be a nice place to stuff these general-purpose jsonify recipes that happen to come long and cover the use-cases of a wide enough user base.
  2. What's the intended behaviour for jsonify_salist? I guess it means to jsonify all "SA objects" in an object's relations, if this is the case then it should read:
        @jsonify.when("isinstance(obj, sqlalchemy.attributes.InstrumentedList)")
    def jsonify_salist(obj):
       return map(jsonify, obj)
    However, that would go berserk (read raise a "RuntimeError?: maximum recursion depth exceeded") if we run across circular dependencies.

Just speculating on 2, a passing test will surely prove me wrong ;)


comment:3 Changed 13 years ago by elvelind

This should go into TurboJson?. Since I haven't used SA much myself I also want a test for it before I commit it.

comment:4 Changed 13 years ago by jorge.vargas

  • Priority changed from high to normal
  • Milestone changed from 1.0b2 to 1.0b3

pardon my ignorance but what about the json.py in the quickstart templates? shouldn't you have jsonify there as the ones for SO ?

comment:5 Changed 13 years ago by groves

sqlalchemy.attributes moved to the sqlalchemy.orm module in SQLAlchemy 0.3.0 so sqlalchemy.attributes.InstrumentedList? needs to change to sqlalchemy.orm.attributes.InstrumentedList? if you upgrade to that version.

comment:6 Changed 13 years ago by alberto

  • Milestone changed from 1.0b3 to 1.1

comment:7 Changed 12 years ago by alberto

  • Milestone changed from 1.1 to __unclassified__

Batch moved into unclassified from 1.1 to properly track progress on the later

comment:8 Changed 12 years ago by abadger1999

This is another useful snippet for outputting SA SelectResults:

@jsonify.when("isinstance(obj, sqlalchemy.ext.selectresults.SelectResults)")
def jsonify_sa_select_results(obj):
    return list(obj)

comment:9 Changed 12 years ago by paj

  • Status changed from new to closed
  • Resolution set to fixed

Ok, in changeset [3418] I have added SQLAlchemy support to TurboJSON, using jsonify_saobject supplied in the description. I have included unit tests. There doesn't seem to be a need for jsonify_salist - relations seem to output ok. Also, I've held off on doing jsonify_sa_select_results as I think the user should be promoting that to a list themselves. We can always add this later if it turns out to be useful.

comment:10 Changed 9 years ago by chrisz

  • Milestone changed from __unclassified__ to 1.0
Note: See TracTickets for help on using tickets.