Commits

Georg Brandl committed bcb6858

Add a custom json serializer implementation that forces translation proxies. Fixes building with the JSON builder.

Comments (0)

Files changed (3)

 Release 1.0.1 (in development)
 ==============================
 
+* Fix building with the JSON builder.
+
 * Fix hyperrefs in object descriptions for LaTeX.
 
 

sphinx/builders/html.py

 from docutils.readers.doctree import Reader as DoctreeReader
 
 from sphinx import package_dir, __version__
-from sphinx.util import copy_static_entry
+from sphinx.util import jsonimpl, copy_static_entry
 from sphinx.util.osutil import SEP, os_path, relative_uri, ensuredir, \
      movefile, ustrftime, copyfile
 from sphinx.util.nodes import inline_all_toctrees
 from sphinx.writers.html import HTMLWriter, HTMLTranslator, \
      SmartyPantsHTMLTranslator
 
-try:
-    import json
-except ImportError:
-    try:
-        import simplejson as json
-    except ImportError:
-        json = None
-
 #: the filename for the inventory of objects
 INVENTORY_FILENAME = 'objects.inv'
 #: the filename for the "last build" file (for serializing builders)
     """
     A builder that dumps the generated HTML into JSON files.
     """
-    implementation = json
-    indexer_format = json
+    implementation = jsonimpl
+    indexer_format = jsonimpl
     name = 'json'
     out_suffix = '.fjson'
     globalcontext_filename = 'globalcontext.json'
     searchindex_filename = 'searchindex.json'
 
     def init(self):
-        if json is None:
+        if jsonimpl.json is None:
             raise SphinxError(
                 'The module simplejson (or json in Python >= 2.6) '
                 'is not available. The JSONHTMLBuilder builder will not work.')

sphinx/util/jsonimpl.py

+# -*- 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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.