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:
|
except ImportError:
|
||||||
from simplejson import JSONEncoder
|
from simplejson import JSONEncoder
|
||||||
|
|
||||||
from datetime import datetime, date
|
from datetime import datetime, date
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from webob.multidict import MultiDict
|
from webob.multidict import MultiDict
|
||||||
from simplegeneric import generic
|
from sqlalchemy.engine.base import ResultProxy, RowProxy
|
||||||
|
from simplegeneric import generic
|
||||||
|
|
||||||
#
|
#
|
||||||
# exceptions
|
# exceptions
|
||||||
@@ -64,12 +65,4 @@ _instance = GenericFunctionJSON()
|
|||||||
def encode(obj):
|
def encode(obj):
|
||||||
if isinstance(obj, basestring):
|
if isinstance(obj, basestring):
|
||||||
return _instance.encode(obj)
|
return _instance.encode(obj)
|
||||||
try:
|
return _instance.encode(obj)
|
||||||
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)
|
|
||||||
@@ -1,28 +1,27 @@
|
|||||||
from pecan.jsonify import jsonify, encode
|
from pecan.jsonify import jsonify, encode
|
||||||
from pecan import Pecan, expose
|
from pecan import Pecan, expose
|
||||||
from webtest import TestApp
|
from webtest import TestApp
|
||||||
from simplejson import loads
|
from simplejson import loads
|
||||||
import simplegeneric
|
|
||||||
|
|
||||||
class Person(object):
|
|
||||||
def __init__(self, first_name, last_name):
|
def make_person():
|
||||||
self.first_name = first_name
|
class Person(object):
|
||||||
self.last_name = last_name
|
def __init__(self, first_name, last_name):
|
||||||
|
self.first_name = first_name
|
||||||
|
self.last_name = last_name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
return '%s %s' % (self.first_name, self.last_name)
|
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
|
# create a Person instance
|
||||||
p = Person('Jonathan', 'LaCour')
|
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
|
# register a generic JSON rule
|
||||||
@jsonify.when_type(Person)
|
@jsonify.when_type(Person)
|
||||||
def jsonify_person(obj):
|
def jsonify_person(obj):
|
||||||
@@ -38,28 +37,25 @@ def test_simple_rule():
|
|||||||
|
|
||||||
class TestJsonify(object):
|
class TestJsonify(object):
|
||||||
|
|
||||||
def test_simple_jsonify(self):
|
def test_simple_jsonify(self):
|
||||||
|
Person = make_person()
|
||||||
|
|
||||||
# register a generic JSON rule
|
# register a generic JSON rule
|
||||||
@jsonify.when_type(Person)
|
@jsonify.when_type(Person)
|
||||||
def jsonify_person(obj):
|
def jsonify_person(obj):
|
||||||
return dict(
|
return dict(
|
||||||
name=obj.name
|
name=obj.name
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RootController(object):
|
class RootController(object):
|
||||||
@expose('json')
|
@expose('json')
|
||||||
def index(self):
|
def index(self):
|
||||||
# create a Person instance
|
# create a Person instance
|
||||||
p = Person('Jonathan', 'LaCour')
|
p = Person('Jonathan', 'LaCour')
|
||||||
return p
|
return p
|
||||||
|
|
||||||
|
|
||||||
app = TestApp(Pecan(RootController()))
|
app = TestApp(Pecan(RootController()))
|
||||||
|
|
||||||
|
|
||||||
r = app.get('/')
|
r = app.get('/')
|
||||||
assert r.status_int == 200
|
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