mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 16:24:18 +03:00
Handle binary or unicode with JSONField
This commit is contained in:
parent
dad207de66
commit
265ec8ac62
|
@ -461,7 +461,7 @@ You can also use the declarative style, as with `ListField`. For example:
|
||||||
|
|
||||||
## JSONField
|
## JSONField
|
||||||
|
|
||||||
A field class that validates that the incoming data structure consists of valid JSON primitives. In its alternate binary mode, it will represent and validate JSON encoded strings.
|
A field class that validates that the incoming data structure consists of valid JSON primitives. In its alternate binary mode, it will represent and validate JSON-encoded binary strings.
|
||||||
|
|
||||||
**Signature**: `JSONField(binary)`
|
**Signature**: `JSONField(binary)`
|
||||||
|
|
||||||
|
|
|
@ -1535,6 +1535,8 @@ class JSONField(Field):
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
try:
|
try:
|
||||||
if self.binary:
|
if self.binary:
|
||||||
|
if isinstance(data, six.binary_type):
|
||||||
|
data = data.decode('utf-8')
|
||||||
return json.loads(data)
|
return json.loads(data)
|
||||||
else:
|
else:
|
||||||
json.dumps(data)
|
json.dumps(data)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user