def jsonp(data, **json_kwargs): """ jsonp is callback key name """ from uliweb import request if 'jsonp' in json_kwargs: cb = json_kwargs.pop('jsonp') else: cb = 'callback' begin = str(request.GET.get(cb)) if not begin: raise BadRequest("Can't found %s parameter in request's query_string" % cb) if not r_callback.match(begin): raise BadRequest("The callback name is not right, it can be alphabetic, number and underscore only") if callable(data): @wraps(data) def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(begin + '(' + json_dumps(ret) + ');', **json_kwargs) return f else: return Response(begin + '(' + json_dumps(data) + ');', **json_kwargs)
def json(data, **json_kwargs): if 'content_type' not in json_kwargs: json_kwargs['content_type'] = 'application/json; charset=utf-8' if callable(data): @wraps(data) def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(json_dumps(ret), **json_kwargs) return f else: return Response(json_dumps(data), **json_kwargs)
def json(data, unicode=False): from js import json_dumps if unicode: ensure_ascii = True else: ensure_ascii = False if callable(data): @wraps(data) def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(json_dumps(ret), content_type='application/json; charset=utf-8') return f else: return Response(json_dumps(data), content_type='application/json; charset=utf-8')
def jsonp(data, **json_kwargs): """ jsonp is callback key name """ from uliweb import request if 'jsonp' in json_kwargs: cb = json_kwargs.pop('jsonp') else: cb = 'callback' begin = request.GET.get(cb) if not begin: raise UliwebError("Can't found %s parameter in request's query_string" % cb) if callable(data): @wraps(data) def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(begin + '(' + json_dumps(ret) + ');', **json_kwargs) return f else: return Response(begin + '(' + json_dumps(data) + ');', **json_kwargs)
def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(begin + '(' + json_dumps(ret) + ');', **json_kwargs)
def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(json_dumps(ret), **json_kwargs)
def f(*arg, **kwargs): ret = data(*arg, **kwargs) return Response(json_dumps(ret), content_type='application/json; charset=utf-8')