mirror of
https://github.com/ansible/awx.git
synced 2026-03-22 19:35:02 -02:30
Allow DRF parser to accept trailing commas.
This commit is contained in:
@@ -12,6 +12,26 @@ from rest_framework import parsers
|
||||
from rest_framework.exceptions import ParseError
|
||||
|
||||
|
||||
def _remove_trailing_commas(data):
|
||||
left = 0
|
||||
right = 0
|
||||
in_string = False
|
||||
ret = []
|
||||
while left != len(data):
|
||||
if data[left] == ',' and not in_string:
|
||||
while right != len(data) and data[right] in ',\n\t\r ':
|
||||
right += 1
|
||||
if right == len(data) or data[right] not in '}]':
|
||||
ret.append(',')
|
||||
else:
|
||||
if data[left] == '"' and (left - 1 >= 0 and data[left - 1] != '\\'):
|
||||
in_string = not in_string
|
||||
ret.append(data[left])
|
||||
right += 1
|
||||
left = right
|
||||
return ''.join(ret)
|
||||
|
||||
|
||||
class JSONParser(parsers.JSONParser):
|
||||
"""
|
||||
Parses JSON-serialized data, preserving order of dictionary keys.
|
||||
@@ -25,7 +45,7 @@ class JSONParser(parsers.JSONParser):
|
||||
encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
|
||||
|
||||
try:
|
||||
data = stream.read().decode(encoding)
|
||||
data = _remove_trailing_commas(stream.read().decode(encoding))
|
||||
obj = json.loads(data, object_pairs_hook=OrderedDict)
|
||||
if not isinstance(obj, dict):
|
||||
raise ParseError(_('JSON parse error - not a JSON object'))
|
||||
|
||||
Reference in New Issue
Block a user