Permalink
Browse files

Better error message if simplegeneric is not installed.

Require latest TurboJson for tests since old one does not work with recent peak.rules.
  • Loading branch information...
1 parent cc32bca commit f6a1543cb963e5eacc8613192165f44eccbc1f2f @Cito Cito committed Nov 19, 2010
Showing with 17 additions and 8 deletions.
  1. +1 −1 setup.py
  2. +16 −7 tg/jsonify.py
View
@@ -25,7 +25,7 @@
'Genshi >= 0.5.1',
'TurboKid >= 1.0.4',
'Mako',
- 'TurboJson >= 1.2.1',
+ 'TurboJson >= 1.3',
'Babel >=0.9.4',
'tgext.admin>=0.3.9',
]
View
@@ -1,10 +1,12 @@
-"""JSON encoding functions using EAK-Rules."""
+"""JSON encoding functions."""
import datetime
import decimal
from simplejson import JSONEncoder
+from webob.multidict import MultiDict
+
try:
import sqlalchemy
from sqlalchemy.engine.base import ResultProxy, RowProxy
@@ -15,12 +17,13 @@
def is_saobject(obj):
return hasattr(obj, '_sa_class_manager')
-from webob.multidict import MultiDict
+class JsonEncodeError(Exception):
+ """JSON Encode error"""
-class JsonEncodeError(Exception):pass
-# JSON Encoder class
class GenericJSON(JSONEncoder):
+ """JSON Encoder class"""
+
def default(self, obj):
if hasattr(obj, '__json__') and callable(obj.__json__):
return obj.__json__()
@@ -43,8 +46,6 @@ def default(self, obj):
else:
return JSONEncoder.default(self, obj)
-# Generic Function JSON Encoder class
-
try:
from simplegeneric import generic
@@ -55,16 +56,24 @@ def jsonify(obj):
return _default.default(obj)
class GenericFunctionJSON(GenericJSON):
+ """Generic Function JSON Encoder class."""
+
def default(self, obj):
return jsonify(obj)
_instance = GenericFunctionJSON()
except ImportError:
+
+ def jsonify(obj):
+ raise ImportError('simplegeneric is not installed')
+
_instance = GenericJSON()
+
# General encoding functions
def encode(obj):
+ """Return a JSON string representation of a Python object."""
if isinstance(obj, basestring):
return _instance.encode(obj)
try:
@@ -74,9 +83,9 @@ def encode(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)
+
def encode_iter(obj):
"""Encode object, yielding each string representation as available."""
return _instance.iterencode(obj)

0 comments on commit f6a1543

Please sign in to comment.