From 66c92205f7bac75aa03bc03d1267a42779d34309 Mon Sep 17 00:00:00 2001 From: Jonathan LaCour Date: Fri, 19 Nov 2010 09:16:19 -0500 Subject: [PATCH] Fixing a few issues in the jsonify library, namely an unneeded restriction brought over from TurboGears. --- pecan/jsonify.py | 19 ++++++---------- tests/test_jsonify.py | 50 ++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/pecan/jsonify.py b/pecan/jsonify.py index 6ccff68..c47a31a 100644 --- a/pecan/jsonify.py +++ b/pecan/jsonify.py @@ -3,10 +3,11 @@ try: except ImportError: from simplejson import JSONEncoder -from datetime import datetime, date -from decimal import Decimal -from webob.multidict import MultiDict -from simplegeneric import generic +from datetime import datetime, date +from decimal import Decimal +from webob.multidict import MultiDict +from sqlalchemy.engine.base import ResultProxy, RowProxy +from simplegeneric import generic # # exceptions @@ -64,12 +65,4 @@ _instance = GenericFunctionJSON() def encode(obj): if isinstance(obj, basestring): return _instance.encode(obj) - try: - value = obj['test'] - except TypeError: - if not hasattr(obj, '__json__') and not is_saobject(obj): - raise JsonEncodeError('Your Encoded object must be dict-like.') - except: - pass - """Return a JSON string representation of a Python object.""" - return _instance.encode(obj) + return _instance.encode(obj) \ No newline at end of file diff --git a/tests/test_jsonify.py b/tests/test_jsonify.py index e11a4b7..e1792df 100644 --- a/tests/test_jsonify.py +++ b/tests/test_jsonify.py @@ -1,28 +1,27 @@ -from pecan.jsonify import jsonify, encode -from pecan import Pecan, expose -from webtest import TestApp -from simplejson import loads -import simplegeneric +from pecan.jsonify import jsonify, encode +from pecan import Pecan, expose +from webtest import TestApp +from simplejson import loads -class Person(object): - def __init__(self, first_name, last_name): - self.first_name = first_name - self.last_name = last_name + +def make_person(): + class Person(object): + def __init__(self, first_name, last_name): + self.first_name = first_name + self.last_name = last_name - @property - def name(self): - return '%s %s' % (self.first_name, self.last_name) + @property + def name(self): + return '%s %s' % (self.first_name, self.last_name) + return Person + + +def test_simple_rule(): + Person = make_person() -def test_simple_rule(): # create a Person instance p = Person('Jonathan', 'LaCour') - # encode the object using the existing "default" rules - result = loads(encode(p)) - assert result['first_name'] == 'Jonathan' - assert result['last_name'] == 'LaCour' - assert len(result) == 2 - # register a generic JSON rule @jsonify.when_type(Person) def jsonify_person(obj): @@ -38,28 +37,25 @@ def test_simple_rule(): class TestJsonify(object): - def test_simple_jsonify(self): - + def test_simple_jsonify(self): + Person = make_person() + # register a generic JSON rule @jsonify.when_type(Person) def jsonify_person(obj): return dict( name=obj.name ) - - + class RootController(object): @expose('json') def index(self): # create a Person instance p = Person('Jonathan', 'LaCour') return p - app = TestApp(Pecan(RootController())) - r = app.get('/') assert r.status_int == 200 - assert loads(r.body) == {'name':'Jonathan LaCour'} - + assert loads(r.body) == {'name':'Jonathan LaCour'} \ No newline at end of file