Django REST Framework JSON CamelCase with orjson support
Camel case JSON support for Django REST framework with orjson.
Installation
At the command line:
$ pip install drf-camel-case
Add the render and parser to your django settings file.
# ...
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'drf_camel_case.render.CamelCaseJSONRenderer',
'drf_camel_case.render.CamelCaseBrowsableAPIRenderer',
# Any other renders
),
'DEFAULT_PARSER_CLASSES': (
# If you use MultiPartFormParser or FormParser, we also have a camel case version
'drf_camel_case.parser.CamelCaseFormParser',
'drf_camel_case.parser.CamelCaseMultiPartParser',
'drf_camel_case.parser.CamelCaseJSONParser',
# Any other parsers
),
}
# ...
Swapping Renderer
By default the package uses rest_framework.renderers.JSONRenderer. If you want to use another renderer (the only possible alternative is rest_framework.renderers.UnicodeJSONRenderer, only available in DRF < 3.0), you must specify it in your django settings file.
# ...
JSON_CAMEL_CASE = {
'RENDERER_CLASS': 'rest_framework.renderers.UnicodeJSONRenderer'
}
# ...
Underscoreize Options
No Underscore Before Number
As raised in this comment there are two conventions of snake case.
# Case 1 (Package default)
v2Counter -> v_2_counter
fooBar2 -> foo_bar_2
# Case 2
v2Counter -> v2_counter
fooBar2 -> foo_bar2
By default, the package uses the first case. To use the second case, specify it in your django settings file.
REST_FRAMEWORK = {
# ...
'JSON_UNDERSCOREIZE': {
'no_underscore_before_number': True,
},
# ...
}
Alternatively, you can change this behavior on a class level by setting json_underscoreize:
from drf_camel_case.parser import CamelCaseJSONParser
from rest_framework.generics import CreateAPIView
class NoUnderscoreBeforeNumberCamelCaseJSONParser(CamelCaseJSONParser):
json_underscoreize = {'no_underscore_before_number': True}
class MyView(CreateAPIView):
queryset = MyModel.objects.all()
serializer_class = MySerializer
parser_classes = (NoUnderscoreBeforeNumberCamelCaseJSONParser,)
Ignore Fields
You can also specify fields which should not have their data changed. The specified field(s) would still have their name change, but there would be no recursion. For example:
data = {"my_key": {"do_not_change": 1}}
Would become:
{"myKey": {"doNotChange": 1}}
However, if you set in your settings:
REST_FRAMEWORK = {
# ...
"JSON_UNDERSCOREIZE": {
# ...
"ignore_fields": ("my_key",),
# ...
},
# ...
}
The my_key field would not have its data changed:
{"myKey": {"do_not_change": 1}}
Running Tests
To run the current test suite, execute the following from the root of he project:
$ python -m unittest discover
License
Free software: BSD license
History
1.0.2 (2021-10-21)
Update with upstream changes
1.0.1 (2021-10-21)
Release fix
1.0.0 (2021-10-21)
First release
Release files for drf-camel-case 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz | 8.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| drf_camel_case-1.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.2 kB
Release files / drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz
| Download URL | drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c62e4e1e8601a59948a6002fc8e2c9762129b5f4f47686a5d893fb6ff06116e4
|
|
BLAKE2b-256 checksum How to use checksums |
d1c8dbcf5ee5a40da699a4d74e98ac2dd49745cfaf50ba8d1c785b07c0a2e3ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Release files / drf_camel_case-1.0.2-py3-none-any.whl
| Download URL | drf_camel_case-1.0.2-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7b0c78a17daae728ae8bae43e39bc28495dc3139317a826017c2abff30f4e4ee
|
|
BLAKE2b-256 checksum How to use checksums |
0be547ea1f1b7cbae8a3d597978e78e2945a1dd2578bb4a2646d1608cd19d702
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|