Skip to main content

PascalCase JSON support for Django REST framework.

Project description

Django REST Framework JSON PascalCase

https://img.shields.io/pypi/v/djangorestframework-pascal-case.svg https://img.shields.io/pypi/pyversions/djangorestframework-pascal-case.svg

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

At the command line:

$ pip install 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.1.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.1.tar.gz.

File metadata

File hashes

Hashes for djangorestframework_pascal_case-1.0.1.tar.gz
Algorithm Hash digest
SHA256 eeb50ad01b2758bba86bc7b434f4ab1c7dda319a4dbb55b72afc8d86b1fd95db
MD5 876e06684abf24894bc709b3ff89d282
BLAKE2b-256 d3798d46cafe688fb52d5a18f997bd0208031469a5372f52d67d174d26435d3c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for djangorestframework_pascal_case-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b99d385a09297933f902f075fe564ed20abd05b7d0eec7f98652aabf2f1dd264
MD5 78892ba74ce166b68eee2f4323b42633
BLAKE2b-256 d7fae9d2eaefae386a335b6df4caea61d906526f97219f1e98b34a6ec1665a66

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