@contextmanager
def json_writer(response, fields, name=None, bom=False):
 if hasattr(response, u'headers'):
  response.headers['Content-Type'] = 'application/json; charset=utf-8'
  if name:
   response.headers['Content-disposition'] = 'attachment; filename="{name}.json"'.format(name=encode_rfc2231(name))
 if bom:
  response.write(UTF8_BOM)
 response.write(('{\n  "fields": %s,\n  "records": [' % json.dumps(fields, ensure_ascii=False, separators=(u',', u':'))))
 (yield JSONWriter(response, [f['id'] for f in fields]))
 response.write('\n]}\n')

@ contextmanager def json_writer ( response , fields , name = None , bom = False ) : if hasattr ( response , u'headers' ) : response . headers [ 'Content-Type' ] = 'application/json; charset=utf-8' if name : response . headers [ 'Content-disposition' ] = 'attachment; filename="{name}.json"' . format ( name = encode_rfc2231 ( name ) ) if bom : response . write ( UTF8_BOM ) response . write ( ( '{\n  "fields": %s,\n  "records": [' % json . dumps ( fields , ensure_ascii = False , separators = ( u',' , u':' ) ) ) ) ( yield JSONWriter ( response , [ f [ 'id' ] for f in fields ] ) ) response . write ( '\n]}\n' )
context manager for writing utf-8 json data to response .