2014-06-06 04:24:43 +00:00
|
|
|
import sys
|
2014-03-10 05:18:05 +00:00
|
|
|
import cherrypy
|
|
|
|
|
2014-06-06 04:24:43 +00:00
|
|
|
if sys.version_info >= (2, 6):
|
|
|
|
# Python 2.6: simplejson is part of the standard library
|
|
|
|
import json
|
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2014-06-06 04:24:43 +00:00
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
json = None
|
|
|
|
|
|
|
|
if json is None:
|
|
|
|
def json_decode(s):
|
|
|
|
raise ValueError('No JSON library is available')
|
|
|
|
def json_encode(s):
|
|
|
|
raise ValueError('No JSON library is available')
|
|
|
|
else:
|
|
|
|
json_decode = json.JSONDecoder().decode
|
|
|
|
json_encode = json.JSONEncoder().iterencode
|
|
|
|
|
|
|
|
def json_in(force=True, debug=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
request = cherrypy.serving.request
|
2014-06-06 04:24:43 +00:00
|
|
|
def json_processor(entity):
|
|
|
|
"""Read application/json data into request.json."""
|
|
|
|
if not entity.headers.get(u"Content-Length", u""):
|
|
|
|
raise cherrypy.HTTPError(411)
|
|
|
|
|
|
|
|
body = entity.fp.read()
|
|
|
|
try:
|
|
|
|
request.json = json_decode(body)
|
|
|
|
except ValueError:
|
|
|
|
raise cherrypy.HTTPError(400, 'Invalid JSON document')
|
2014-03-10 05:18:05 +00:00
|
|
|
if force:
|
|
|
|
request.body.processors.clear()
|
|
|
|
request.body.default_proc = cherrypy.HTTPError(
|
2014-06-06 04:24:43 +00:00
|
|
|
415, 'Expected an application/json content type')
|
|
|
|
request.body.processors[u'application/json'] = json_processor
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-06 04:24:43 +00:00
|
|
|
def json_out(debug=False):
|
2014-06-05 01:28:59 +00:00
|
|
|
request = cherrypy.serving.request
|
2014-06-06 04:24:43 +00:00
|
|
|
response = cherrypy.serving.response
|
|
|
|
|
|
|
|
real_handler = request.handler
|
|
|
|
def json_handler(*args, **kwargs):
|
|
|
|
response.headers['Content-Type'] = 'application/json'
|
|
|
|
value = real_handler(*args, **kwargs)
|
|
|
|
return json_encode(value)
|
|
|
|
request.handler = json_handler
|
|
|
|
|