<s> import sys <EOL> import cherrypy <EOL> from cherrypy . _cpcompat import basestring , ntou , json , json_encode , json_decode <EOL> def json_processor ( entity ) : <EOL> """<STR_LIT>""" <EOL> if not entity . headers . get ( ntou ( "<STR_LIT>" ) , ntou ( "<STR_LIT>" ) ) : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> ) <EOL> body = entity . fp . read ( ) <EOL> try : <EOL> cherrypy . serving . request . json = json_decode ( body . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except ValueError : <EOL> raise cherrypy . HTTPError ( <NUM_LIT> , '<STR_LIT>' ) <EOL> def json_in ( content_type = [ ntou ( '<STR_LIT:application/json>' ) , ntou ( '<STR_LIT>' ) ] , <EOL> force = True , debug = False , processor = json_processor ) : <EOL> """<STR_LIT>""" <EOL> request = cherrypy . serving . request <EOL> if isinstance ( content_type , basestring ) : <EOL> content_type = [ content_type ] <EOL> if force : <EOL> if debug : <EOL> cherrypy . log ( '<STR_LIT>' % <EOL> repr ( request . body . processors . keys ( ) ) , '<STR_LIT>' ) <EOL> request . body . processors . clear ( ) <EOL> request . body . default_proc = cherrypy . HTTPError ( <EOL> <NUM_LIT> , '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( content_type ) ) <EOL> for ct in content_type : <EOL> if debug : <EOL> cherrypy . log ( '<STR_LIT>' % ct , '<STR_LIT>' ) <EOL> request . body . processors [ ct ] = processor <EOL> def json_handler ( * args , ** kwargs ) : <EOL> value = cherrypy . serving . request . _json_inner_handler ( * args , ** kwargs ) <EOL> return json_encode ( value ) <EOL> def json_out ( content_type = '<STR_LIT:application/json>' , debug = False , handler = json_handler ) : <EOL> """<STR_LIT>""" <EOL> request = cherrypy . serving . request <EOL> if debug : <EOL> cherrypy . log ( '<STR_LIT>' % request . handler , <EOL> '<STR_LIT>' ) <EOL> request . _json_inner_handler = request . handler <EOL> request . handler = handler <EOL> if content_type is not None : <EOL> if debug : <EOL> cherrypy . log ( '<STR_LIT>' % content_type , '<STR_LIT>' ) <EOL> cherrypy . serving . response . headers [ '<STR_LIT:Content-Type>' ] = content_type </s>
