Fixing a few issues in the jsonify library, namely an unneeded
restriction brought over from TurboGears.
This commit is contained in:
@@ -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)
|
||||
@@ -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'}
|
||||
Reference in New Issue
Block a user