serializer.JSONField dose not save the decoded data when doing the to_internal_value
https://github.com/encode/django-rest-framework/blob/40eccb0d6cdb769876dbb79724c5871b4f04163d/rest_framework/fields.py#L1782 json.dumps result dosen't save to 'data' when this field save to db, the type of the data is a dict, and may save with the wrong format of json (with single quota).
please provide test case aswell
That line does not need to alter the data since it just a validation check to ensure that provided data is JSON-serializable.
https://github.com/encode/django-rest-framework/blob/40eccb0d6cdb769876dbb79724c5871b4f04163d/rest_framework/fields.py#L1776-L1785
The method does the following:
- check if the provided data is to be considered raw data or not
- if raw data (aka: a string or bytes) decode it
- otherwhise ensure that it can be dumped in JSON format without raising any error
Line 1782 does the latest check, so if that dump is going to be saved into data that would cause an unwanted change.
To give a real example:
from rest_framework.fields import JSONField
field = JSONField()
field.to_internal_value({'hello': 'world'})
>> {'hello': 'world'}
So I guess we can close the issue or transfer it to a discussion!
Sorry, just this problem: Enabling JSON1 extension on SQLite. And because of missing sqlite json extension, my code can not save json data correctly(with out the extension, sqlite transfer all the “ to ‘ before saving, and can not load the data again).
This is not a problem of rest-framework rather an issue with the SQLite installation.
If you cannot update SQLite I suggest to use the legacy jsonfield https://github.com/rpkilby/jsonfield/ which should map to a TEXT field on database.
Note that that repo, nor other regarding custom JSON field implementations, is no more maintained.
This is not a problem of rest-framework rather an issue with the SQLite installation.
If you cannot update SQLite I suggest to use the legacy jsonfield https://github.com/rpkilby/jsonfield/ which should map to a TEXT field on database.
Note that that repo, nor other regarding custom JSON field implementations, is no more maintained.
I follow the guide and upgrade SQLite, and the problem solved.