#35885: JSONField does accept strings that look like dicts and incorrectly saves
them as strings, breaking JSON filtering
-------------------------------------+-------------------------------------
Reporter: DataGreed | Owner: (none)
Type: Uncategorized | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):
> Why IntegerField breaks if you try to save "FOO" in it and JSONField
doesn't break when you save string in it? It should throw a validation
error at least.
Because a string is not a valid value for an integer field while a string
is a valid value for a JSON field? It's completely valid to store a JSON
de-serializable string such as `"true"` in a JSON document. Would you
expect `json.dumps(json.dumps({"foo": "bar"}}` to raise an exception
because you attempt to serialize a JSON de-serializable string?
> Well, you can jsut check it for these values before calling json.loads.
Although, I have no ide why would you even try to write "false" or "1" in
a JSONField.
JSON is a standard and `JSONField` adheres to it. Django doesn't offer a
JSON except top level values cannot be JSON de-serializable strings kind
of field. To me what you are asking for is analogous to the implicit bytes
to text conversion that Python 2 use to make when asked to shove bytes
into text expecting interfaces. It is much better from a data hygiene
perspective to deserialize data at input boundaries (forms, serializers)
and pass it around in its deserialized form through interfaces.
--
Ticket URL: <https://code.djangoproject.com/ticket/35885#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070193527cf7aa-2a9eedf3-3123-4df4-91f2-647e68a6938d-000000%40eu-central-1.amazonses.com.