Fixing a few issues in the jsonify library, namely an unneeded

restriction brought over from TurboGears.
This commit is contained in:
Jonathan LaCour
2010-11-19 09:16:19 -05:00
parent 3b9251534c
commit 66c92205f7
2 changed files with 29 additions and 40 deletions

View File

@@ -6,6 +6,7 @@ except ImportError:
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
#
@@ -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)

View File

@@ -2,8 +2,9 @@ from pecan.jsonify import jsonify, encode
from pecan import Pecan, expose
from webtest import TestApp
from simplejson import loads
import simplegeneric
def make_person():
class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
@@ -12,17 +13,15 @@ class Person(object):
@property
def name(self):
return '%s %s' % (self.first_name, self.last_name)
return Person
def test_simple_rule():
Person = make_person()
# 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):
@@ -39,6 +38,7 @@ def test_simple_rule():
class TestJsonify(object):
def test_simple_jsonify(self):
Person = make_person()
# register a generic JSON rule
@jsonify.when_type(Person)
@@ -47,7 +47,6 @@ class TestJsonify(object):
name=obj.name
)
class RootController(object):
@expose('json')
def index(self):
@@ -55,11 +54,8 @@ class TestJsonify(object):
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'}