Skip to content

Add a separate parse_data method to JSONParser #1083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
Alan Crosswell <[email protected]>
Alex Seidmann <[email protected]>
Anton Shutik <[email protected]>
Ashley Loewen <[email protected]>
Asif Saif Uddin <[email protected]>
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)