diff options
author | Georg Brandl <georg@python.org> | 2010-07-25 14:52:37 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-07-25 14:52:37 +0200 |
commit | ebb7a541419276c3207afd3a4ef2543ebac3dd48 (patch) | |
tree | 6251220017ee6ec55a7e6f1969569cdac3481e32 /sphinx/util/jsonimpl.py | |
parent | e3f47a800ad157e5b61743a6d6d4e85de8c0dbd0 (diff) | |
download | sphinx-git-ebb7a541419276c3207afd3a4ef2543ebac3dd48.tar.gz |
Add a custom json serializer implementation that forces translation proxies. Fixes building with the JSON builder.
Diffstat (limited to 'sphinx/util/jsonimpl.py')
-rw-r--r-- | sphinx/util/jsonimpl.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/sphinx/util/jsonimpl.py b/sphinx/util/jsonimpl.py new file mode 100644 index 000000000..21f285d30 --- /dev/null +++ b/sphinx/util/jsonimpl.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +""" + sphinx.util.jsonimpl + ~~~~~~~~~~~~~~~~~~~~ + + JSON serializer implementation wrapper. + + :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import UserString + +try: + import json + JSONEncoder = json.JSONEncoder +except ImportError: + try: + import simplejson as json + JSONEncoder = json.JSONEncoder + except ImportError: + json = None + JSONEncoder = object + + +class SphinxJSONEncoder(JSONEncoder): + """JSONEncoder subclass that forces translation proxies.""" + def default(self, obj): + if isinstance(obj, UserString.UserString): + return unicode(obj) + return JSONEncoder.default(self, obj) + + +def dump(obj, fp, *args, **kwds): + kwds['cls'] = SphinxJSONEncoder + return json.dump(obj, fp, *args, **kwds) + +def dumps(obj, *args, **kwds): + kwds['cls'] = SphinxJSONEncoder + return json.dumps(obj, *args, **kwds) + +load = json.load +loads = json.loads |