two json modules... one for 2.5, one for 2.6... util that just works.
authorBrandon King <kingb@caltech.edu>
Tue, 30 Jun 2009 19:04:26 +0000 (19:04 +0000)
committerBrandon King <kingb@caltech.edu>
Tue, 30 Jun 2009 19:04:26 +0000 (19:04 +0000)
htsworkflow/frontend/bcmagic/views.py
htsworkflow/frontend/inventory/views.py
htsworkflow/util/jsonutil.py [new file with mode: 0644]

index 492d7e58f685ca2d55f4570f79e5cca7e60f2ebd..835ccf42ddb63c0f290788d63d995f5004dcb3f3 100644 (file)
@@ -6,8 +6,8 @@ from django.core.exceptions import ObjectDoesNotExist
 from htsworkflow.frontend.bcmagic import models
 from htsworkflow.frontend.bcmagic.utils import report_error, redirect_to_url
 from htsworkflow.frontend.bcmagic.plugin import bcm_plugin_processor
+from htsworkflow.util.jsonutil import encode_json
 
-import json
 import re
 
 from htsworkflow.frontend.bcmagic import forms
@@ -116,8 +116,8 @@ def magic(request):
     # Try keyword mapper
     else:
         d = __magic_process(text)
-    j = json.JSONEncoder()
-    return HttpResponse(j.encode(d), 'text/plain')
+    
+    return HttpResponse(encode_json(d), 'text/plain')
 
 
 
@@ -129,11 +129,11 @@ def json_test(request):
     else:
         text = None
     
-    #return HttpResponse(json.write(request.POST.items()), 'text/plain')
+    #return HttpResponse(encode_json(request.POST.items()), 'text/plain')
     if text is None or text.strip() == '':
         d['mode'] = 'Error'
         d['status'] = 'Did not recieve text'
-        return HttpResponse(json.write(d), 'text/plain')
+        return HttpResponse(encode_json(d), 'text/plain')
     
     if text.split('|')[0] == 'url':
         d['mode'] = 'redirect'
@@ -142,5 +142,4 @@ def json_test(request):
         d['msg'] = 'Recieved text: %s' % (text)
         d['mode'] = 'clear'
     
-    j = json.JSONEncoder()
-    return HttpResponse(j.encode(d), 'text/plain')
+    return HttpResponse(json_encode(d), 'text/plain')
index b8a897b5441af872847a5ace8065d7e1dbcc4f59..8273717874c8b7aec371d4ba29a71e4ed01fbf1b 100644 (file)
@@ -1,6 +1,7 @@
 from htsworkflow.frontend.inventory.models import Item, LongTermStorage
 from htsworkflow.frontend.experiments.models import FlowCell
 from htsworkflow.frontend.bcmagic.forms import BarcodeMagicForm
+from htsworkflow.util.jsonutil import encode_json
 
 from django.core.exceptions import ObjectDoesNotExist
 from django.http import HttpResponse, HttpResponseRedirect
@@ -9,7 +10,7 @@ from django.template import RequestContext
 from django.template.loader import get_template
 from django.contrib.auth.decorators import login_required
 
-import json
+
 
 INVENTORY_CONTEXT_DEFAULTS = {
     'app_name': 'Inventory Tracker',
@@ -35,8 +36,7 @@ def data_items(request):
     
     d['rows'] = rows
     
-    j = json.JSONEncoder()
-    return HttpResponse(j.encode(d), content_type="application/javascript")
+    return HttpResponse(encode_json(d), content_type="application/javascript")
 
 def index(request):
     """
diff --git a/htsworkflow/util/jsonutil.py b/htsworkflow/util/jsonutil.py
new file mode 100644 (file)
index 0000000..84dc599
--- /dev/null
@@ -0,0 +1,33 @@
+import json
+
+_ENCODER_METHOD = 1
+_WRITE_METHOD = 2
+
+JSON_METHOD = None
+
+try:
+    json.write({})
+except:
+    JSON_METHOD = _ENCODER_METHOD
+
+try:
+    json.JSONEncoder()
+except:
+    JSON_METHOD = _WRITE_METHOD
+
+assert JSON_METHOD is not None
+
+
+def encode_json(data):
+    """
+    encodes json data given whatever json module we have access to (2.6 builtin or python-json)
+    """
+    if JSON_METHOD == _ENCODER_METHOD:
+        j = json.JSONEncoder()
+        return j.encode(data)
+    
+    elif JSON_METHOD == _WRITE_METHOD:
+        return json.write(data)
+
+    msg = "JSON_METHOD of value %s not supported." % (JSON_METHOD)
+    raise ValueError, msg
\ No newline at end of file