About
Wrap view functions, allowng them render python native and custom objects to json
Usage
Simple wrap returning data into json
from jsonresponse import to_json
@to_json('plain')
def hello(request):
return dict(hello='world')
GET /hello/
{"hello": "world"}
Result can be wraped in some api manier. Note, you can pass debug=1 to pretty print response. Use JSONRESPONSE_DEFAULT_DEBUG=True to enable by default.
@to_json('api')
def goodbye(request):
return dict(good='bye')
GET /goodbye/?debug=1
{
"data": {
"good": "bye"
},
"err": 0
}
Automaticaly error handling, note response code is 500 here
@to_json('api')
def error(request):
raise Exception('Wooot!??')
GET /error/
{
"err_class": "Exception",
"err_desc": "Wooot!??",
"data": null,
"err": 1
}
You can pass raise=1 to raise exceptions in debug purposes instead of passing info to json response
GET /error/?raise=1 Traceback (most recent call last): Exception: Wooot!??
You can pass custom kwargs to json.dumps, just give them to constructor:
@to_json('plain', separators=(',', ':'))
def custom_kwargs(request):
return ['a', { 'b': 1 }]
GET /
["a",{"b":1}]
You can serialize not only pure python data types. Implement serialize method on toplevel object or each element of toplevel array. Note, that request object is passed to serialize method.
class User(object):
def __init__(self, name, age):
self.name = name
self.age = age
def serialize(self, request):
if request.GET.get('with_age', False):
return dict(name=self.name, age=self.age)
else:
return dict(name=self.name)
@to_json('objects')
def users(request):
return [User('Bob', 10), User('Anna', 12)]
GET /users/?with_age=1
{
"data": [
{
"age": 10,
"name": "Bob"
},
{
"age": 12,
"name": "Anna"
}
],
"err": 0
}
It is easy to use jsonp, just pass format=jsonp
GET /users/?format=jsonp
callback({
"data": [
{
"name": "Bob"
},
{
"name": "Anna"
}
],
"err": 0
});
You can override the name of callback method using JSONRESPONSE_CALLBACK_NAME option or query arg callback=another_callback
GET /users/?format=jsonp&callback=my_callback
my_callback({
"data": [
{
"name": "Bob"
},
{
"name": "Anna"
}
],
"err": 0
});
Metadata
Release files for django-jsonresponse 0.10.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-jsonresponse-0.10.0.tar.gz | 4.8 kB | Without provenance | Details for django-jsonresponse-0.10.0.tar.gz |
Release files / django-jsonresponse-0.10.0.tar.gz
| Download URL | django-jsonresponse-0.10.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
439dbd10a256a8fb87278394201788fe601b38890481c707cb8314b448295c59
|
|
BLAKE2b-256 checksum How to use checksums |
06479025f80b59af0aa19684e4564e5b813ad5c84927265eb6ad7d6beae6cf63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-jsonresponse 0.10.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.