Currently JSON arrays are not supported.
This is my first ticket (I think), so any advice on what's required would
be appreciated. I'm planning to open a PR on GitHub for this.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Cleanup/optimization
Comment:
Does using a custom encoder with test client's `json_encoder` parameter
solve your issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:1>
Comment (by danpalmer):
It doesn't - the bug is in the detection in the test client of whether to
serialise with the encoder or not. I've fixed it in
https://github.com/django/django/pull/10313
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:2>
Comment (by Adam (Chainz) Johnson):
This isn't a bug on 2.1, but the docs could be marginally clearer by
saying it *only* happens when `data` is a dictionary.
Adding support for detecting arrays would be a 2.2 feature. But then it
should probably detect all other types that `json.dumps` can do too -
booleans, integers, floats, and strings? This could break more workflows,
especially subtly as `json.dumps` on a string will normally just yield
extra quote marks.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:3>
Comment (by danpalmer):
Great, I'll update the ticket/PR for that and document it as a 2.2
feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:4>
Old description:
> This is a follow up to #29082, which added support for serialising JSON
> objects in the Django Test Client.
>
> Currently JSON arrays are not supported.
>
> This is my first ticket (I think), so any advice on what's required would
> be appreciated. I'm planning to open a PR on GitHub for this.
New description:
This is a follow up to #29082, which added support for serialising JSON
objects in the Django Test Client.
Currently JSON types other than objects are not supported.
This is my first ticket (I think), so any advice on what's required would
be appreciated. I'm planning to open a PR on GitHub for this.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:5>
* needs_better_patch: 0 => 1
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:6>
Comment (by danpalmer):
So, while implementing I've found a minor edge case.
From the Python docs
[https://docs.python.org/3/library/json.html#json.JSONEncoder table of
Python to JSON type conversions] list:
||= Python =||= JSON =||
|| int, float, int- & float-derived Enums || number ||
Unfortunately I can't figure out how to test a float-derived enum. I don't
use Python Enums much, so I might just be getting this wrong, but looking
at the [https://github.com/python/cpython/blob/3.5/Lib/json/encoder.py
source for the standard JSONEncoder], I can't see any mention of support
for this feature. It explicitly calls out {{{IntEnums}}}, but nothing
float related for them.
For these reasons, I'm going to just leave this.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:7>
Comment (by danpalmer):
[https://github.com/django/django/pull/10313 PR]
Updated with full support, modulo the issue mentioned above.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:8>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
Comment:
Further discussion on the pull request decided to only support list and
tuple absent use cases for other types and the possibility of over-broad
exception silencing if trying to serialize data unconditionally.
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:9>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"e1816669732c54c40122c8a22dcba42f6ee3c326" e1816669]:
{{{
#!CommitTicketReference repository=""
revision="e1816669732c54c40122c8a22dcba42f6ee3c326"
Fixed #29687 -- Allowed the test client to serialize list/tuple as JSON.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29687#comment:10>