Skip to content

Commit

Permalink
Added a separate parse_data method to JSONParser (#1083)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Seidmann <alex@leanpnt.de>
Co-authored-by: Oliver Sauder <os@esite.ch>
  • Loading branch information
3 people authored Aug 20, 2022
1 parent 38cf7f0 commit c8511a0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Adam Wróbel <https://adamwrobel.com>
Adam Ziolkowski <adam@adsized.com>
Alan Crosswell <alan@columbia.edu>
Alex Seidmann <alex@leanpnt.de>
Anton Shutik <shutikanton@gmail.com>
Ashley Loewen <github@ashleycodes.tech>
Asif Saif Uddin <auvipy@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ any parts of the framework not mentioned in the documentation should generally b
### Added

* Added support for Django 4.1.
* Expanded JSONParser API with `parse_data` method

### Removed

Expand Down
19 changes: 13 additions & 6 deletions rest_framework_json_api/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ def parse_metadata(result):
else:
return {}

def parse(self, stream, media_type=None, parser_context=None):
def parse_data(self, result, parser_context):
"""
Parses the incoming bytestream as JSON and returns the resulting data
Formats the output of calling JSONParser to match the JSON:API specification
and returns the result.
"""
result = super().parse(
stream, media_type=media_type, parser_context=parser_context
)

if not isinstance(result, dict) or "data" not in result:
raise ParseError("Received document does not contain primary data")

Expand Down Expand Up @@ -166,3 +163,13 @@ def parse(self, stream, media_type=None, parser_context=None):
parsed_data.update(self.parse_relationships(data))
parsed_data.update(self.parse_metadata(result))
return parsed_data

def parse(self, stream, media_type=None, parser_context=None):
"""
Parses the incoming bytestream as JSON and returns the resulting data
"""
result = super().parse(
stream, media_type=media_type, parser_context=parser_context
)

return self.parse_data(result, parser_context)

0 comments on commit c8511a0

Please sign in to comment.