Skip to main content

Camel case JSON support for Django REST framework with orjson.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_camel_case-1.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz.

File metadata

  • Download URL: drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for drf-camel-case-1.0.2.macosx-12.1-x86_64.tar.gz
Algorithm Hash digest
SHA256 c62e4e1e8601a59948a6002fc8e2c9762129b5f4f47686a5d893fb6ff06116e4
MD5 1f87b67f2f571ed87449327474c46d4f
BLAKE2b-256 d1c8dbcf5ee5a40da699a4d74e98ac2dd49745cfaf50ba8d1c785b07c0a2e3ae

See more details on using hashes here.

File details

Details for the file drf_camel_case-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: drf_camel_case-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for drf_camel_case-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b0c78a17daae728ae8bae43e39bc28495dc3139317a826017c2abff30f4e4ee
MD5 e027136d50b728127456d0e1d9b8ff1e
BLAKE2b-256 0be547ea1f1b7cbae8a3d597978e78e2945a1dd2578bb4a2646d1608cd19d702

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page