Last active
March 29, 2020 22:36
-
-
Save vbabiy/5842073 to your computer and use it in GitHub Desktop.
CamelCaseJSONRenderer For django rest framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.parsers import JSONParser | |
from django.conf import settings | |
import re | |
import json | |
first_cap_re = re.compile('(.)([A-Z][a-z]+)') | |
all_cap_re = re.compile('([a-z0-9])([A-Z])') | |
def camel_to_underscore(name): | |
s1 = first_cap_re.sub(r'\1_\2', name) | |
return all_cap_re.sub(r'\1_\2', s1).lower() | |
def underscoreize(data): | |
if isinstance(data, dict): | |
new_dict = {} | |
for key, value in data.items(): | |
new_key = camel_to_underscore(key) | |
new_dict[new_key] = underscoreize(value) | |
return new_dict | |
if isinstance(data, (list, tuple)): | |
for i in range(len(data)): | |
data[i] = underscoreize(data[i]) | |
return data | |
return data | |
class CamelCaseJSONRenderer(JSONParser): | |
def parse(self, stream, media_type=None, parser_context=None): | |
parser_context = parser_context or {} | |
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET) | |
try: | |
data = stream.read().decode(encoding) | |
return underscoreize(json.loads(data)) | |
except ValueError as exc: | |
raise ParseError('JSON parse error - %s' % six.text_type(exc)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.renderers import JSONRenderer | |
import re | |
def underscoreToCamel(match): | |
return match.group()[0] + match.group()[2].upper() | |
def camelize(data): | |
if isinstance(data, dict): | |
new_dict = {} | |
for key, value in data.items(): | |
new_key = re.sub(r"[a-z]_[a-z]", underscoreToCamel, key) | |
new_dict[new_key] = camelize(value) | |
return new_dict | |
if isinstance(data, (list, tuple)): | |
for i in range(len(data)): | |
data[i] = camelize(data[i]) | |
return data | |
return data | |
class CamelCaseJSONRenderer(JSONRenderer): | |
def render(self, data, *args, **kwargs): | |
return super(CamelCaseJSONRenderer, self).render(camelize(data), *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
REST_FRAMEWORK = { | |
'DEFAULT_RENDERER_CLASSES': ( | |
'utils.api.renderers.CamelCaseJSONRenderer', | |
'rest_framework.renderers.BrowsableAPIRenderer', | |
), | |
'DEFAULT_PARSER_CLASSES': ( | |
'utils.api.parsers.CamelCaseJSONRenderer', | |
'rest_framework.parsers.FormParser', | |
'rest_framework.parsers.MultiPartParser' | |
), | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment