Skip to main content

PascalCase JSON support for Django REST framework.

Project description

Django REST Framework JSON PascalCase

PascalCase JSON support for Django REST framework.

This library is a fork of djangorestframework-camel-case by Vitaly Babiy, modified to output PascalCase instead of camelCase. This is useful for APIs that need to be compatible with .NET clients or frontends that expect PascalCase JSON keys.

Installation

Install from the local package:

$ pip install -e ./djangorestframework-pascal-case

Add the render and parser to your django settings file.

# ...
REST_FRAMEWORK = {

    'DEFAULT_RENDERER_CLASSES': (
        'djangorestframework_pascal_case.render.PascalCaseJSONRenderer',
        'djangorestframework_pascal_case.render.PascalCaseBrowsableAPIRenderer',
        # Any other renders
    ),

    'DEFAULT_PARSER_CLASSES': (
        # If you use MultiPartFormParser or FormParser, we also have a pascal case version
        'djangorestframework_pascal_case.parser.PascalCaseFormParser',
        'djangorestframework_pascal_case.parser.PascalCaseMultiPartParser',
        'djangorestframework_pascal_case.parser.PascalCaseJSONParser',
        # Any other parsers
    ),
}
# ...

Add query param middleware to django settings file.

# ...
MIDDLEWARE = [
    # Any other middleware
    'djangorestframework_pascal_case.middleware.PascalCaseMiddleWare',
]
# ...

Swapping Renderer

By default the package uses rest_framework.renderers.JSONRenderer. If you want to use another renderer, the two possible are:

drf_orjson_renderer.renderers.ORJSONRenderer or drf_ujson.renderers.UJSONRenderer or rest_framework.renderers.UnicodeJSONRenderer for DRF < 3.0, specify it in your django settings file.

# ...
JSON_PASCAL_CASE = {
    'RENDERER_CLASS': 'drf_orjson_renderer.renderers.ORJSONRenderer'
}
# ...

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.

JSON_PASCAL_CASE = {
    # ...
    'JSON_UNDERSCOREIZE': {
        'no_underscore_before_number': True,
    },
    # ...
}

Alternatively, you can change this behavior on a class level by setting json_underscoreize:

from djangorestframework_pascal_case.parser import PascalCaseJSONParser
from rest_framework.generics import CreateAPIView

class NoUnderscoreBeforeNumberPascalCaseJSONParser(PascalCaseJSONParser):
    json_underscoreize = {'no_underscore_before_number': True}

class MyView(CreateAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MySerializer
    parser_classes = (NoUnderscoreBeforeNumberPascalCaseJSONParser,)

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:

JSON_PASCAL_CASE = {
    # ...
    "JSON_UNDERSCOREIZE": {
        # ...
        "ignore_fields": ("my_key",),
        # ...
    },
    # ...
}

The my_key field would not have its data changed:

{"MyKey": {"do_not_change": 1}}

Ignore Keys

You can also specify keys which should not be renamed. The specified field(s) would still change (even recursively). For example:

data = {"unchanging_key": {"change_me": 1}}

Would become:

{"UnchangingKey": {"ChangeMe": 1}}

However, if you set in your settings:

JSON_PASCAL_CASE = {
    # ...
    "JSON_UNDERSCOREIZE": {
        # ...
        "ignore_keys": ("unchanging_key",),
        # ...
    },
    # ...
}

The unchanging_key field would not be renamed:

{"unchanging_key": {"ChangeMe": 1}}

ignore_keys and ignore_fields can be applied to the same key if required.

Running Tests

To run the current test suite, execute the following from the root of the project:

$ python -m unittest discover

Credits

This library is a fork of djangorestframework-camel-case by Vitaly Babiy (vbabiy86@gmail.com).

License

  • Free software: BSD license

History

1.0.0 (2024-11-28)

  • Initial release of djangorestframework-pascal-case

  • Forked from djangorestframework-camel-case v1.4.2

  • Modified to output PascalCase instead of camelCase for .NET frontend compatibility

  • Renamed all classes and functions from CamelCase to PascalCase

  • Updated settings key from JSON_CAMEL_CASE to JSON_PASCAL_CASE (with backwards compatibility)

Original Library History (djangorestframework-camel-case)

1.4.2 (2023-02-13)
  • Middleware to underscorize query params

1.4.1 (2023-02-13)
  • ORJSONRenderer support

1.4.0 (2023-02-09)
  • Various improvements and bug fixes

1.3.0 (2021-11-14)
  • Added ignore_keys option

1.2.0 (2020-06-16)
  • Added ignore_fields option

1.1.0 (2019-09-09)
  • Stable release with class attribute support

0.1.0 (2013-12-20)
  • First release on PyPI by Vitaly Babiy

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

djangorestframework_pascal_case-1.0.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file djangorestframework_pascal_case-1.0.0.tar.gz.

File metadata

File hashes

Hashes for djangorestframework_pascal_case-1.0.0.tar.gz
Algorithm Hash digest
SHA256 751cb34426287d992bce05a1a8b3afbc327055612d2ce6801f01a068af028cab
MD5 5c816862fe13e81a0f2564443ce21a56
BLAKE2b-256 c11bdffe29907fc89dfa55b211ac7b31684cd8ba159e7c143bad0ed286a1afca

See more details on using hashes here.

File details

Details for the file djangorestframework_pascal_case-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for djangorestframework_pascal_case-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 645038fd9d436298898b74ad9979da0b9222be8ab9e443b940261bca99a3dd96
MD5 a3ed6c5cafae67845badf2a36cb48572
BLAKE2b-256 7f3e502037da8875c59d31d6aa91d9c55f48b46ef7fb48b3e1e27d91c053d58f

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