-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
JSONField(binary=True) represents using binary strings, which JSONRenderer does not support. #4185
Comments
Unless you set What you actually want, as you've noted, is this...
Which then works as expected - |
The tricky point with binary=True is that it isn't a json renderable data because it's a binary string and the json renderer expect unicodes. I'd point out that the documentation for that field would benefit some clarification:
As it stands, I understand that it expects a valid JSON input for non binary mode. |
Right - then that's the core issue that we should resolve here. |
Either that or we should output to unicode by default. (Not strictly correct, but it'd do the trick) |
Sorry, I misspoke. It doesn't break the deserialization, it breaks the So until #4187 is resolved, what is the best way to enforce valid JSON inputs for an API? |
Well, using a binary json blob as a field isn't a particularly common use case, so poss you should just reconsider if that's what you actually want (ie a nested document of json primitives may be better) but if it really is what you need I'd suggest subclassing JSONField and forcing to_representation to do .decode('utf8') on the result so that you have a custom JSONField that returns Unicode, not bytestrings. |
Checklist
master
branch of Django REST framework.Steps to reproduce
Expected behavior
Invalid JSON should result in
is_valid()
returningFalse
.Actual behavior
is_valid()
returnsTrue
Specifying
binary=True
for the field definition results in the desired behavior for serialization, but causes deserialization to fail on Python 3.The text was updated successfully, but these errors were encountered: