Skip to content

Commit c8511a0

Browse files
aseidmaAlex Seidmannsliverc
authored
Added a separate parse_data method to JSONParser (#1083)
Co-authored-by: Alex Seidmann <[email protected]> Co-authored-by: Oliver Sauder <[email protected]>
1 parent 38cf7f0 commit c8511a0

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Adam Wróbel <https://adamwrobel.com>
22
Adam Ziolkowski <[email protected]>
33
Alan Crosswell <[email protected]>
4+
Alex Seidmann <[email protected]>
45
Anton Shutik <[email protected]>
56
Ashley Loewen <[email protected]>
67
Asif Saif Uddin <[email protected]>

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b
1919
### Added
2020

2121
* Added support for Django 4.1.
22+
* Expanded JSONParser API with `parse_data` method
2223

2324
### Removed
2425

rest_framework_json_api/parsers.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@ def parse_metadata(result):
7070
else:
7171
return {}
7272

73-
def parse(self, stream, media_type=None, parser_context=None):
73+
def parse_data(self, result, parser_context):
7474
"""
75-
Parses the incoming bytestream as JSON and returns the resulting data
75+
Formats the output of calling JSONParser to match the JSON:API specification
76+
and returns the result.
7677
"""
77-
result = super().parse(
78-
stream, media_type=media_type, parser_context=parser_context
79-
)
80-
8178
if not isinstance(result, dict) or "data" not in result:
8279
raise ParseError("Received document does not contain primary data")
8380

@@ -166,3 +163,13 @@ def parse(self, stream, media_type=None, parser_context=None):
166163
parsed_data.update(self.parse_relationships(data))
167164
parsed_data.update(self.parse_metadata(result))
168165
return parsed_data
166+
167+
def parse(self, stream, media_type=None, parser_context=None):
168+
"""
169+
Parses the incoming bytestream as JSON and returns the resulting data
170+
"""
171+
result = super().parse(
172+
stream, media_type=media_type, parser_context=parser_context
173+
)
174+
175+
return self.parse_data(result, parser_context)

0 commit comments

Comments
 (0)