Opened 5 years ago

Closed 5 years ago

#25254 closed New feature (fixed)

Allow control over json.dumps keyword arguments in JsonResponse

Reported by: Jaap Roes Owned by: Sambhav Satija
Component: HTTP handling Version: master
Severity: Normal Keywords: json dumps indent separators jsonresponse
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently JsonResponse's only allows changing the JSON encoder class. It would be helpful to allow some/all of the arguments to json.dumps .

In my case I've had the need to set indent=2, to get a more readable output, and separators=(',', ':') to get the most compact output possible. Having control over allow_nan or skipkeys could also be useful for some.

Change History (7)

comment:1 Changed 5 years ago by Jaap Roes

A partial workaround for this is to create a custom encoder class that sets the desired values for indent and separators, this won't work as well for other kwargs as there are defaults set in dumps.

Last edited 5 years ago by Jaap Roes (previous) (diff)

comment:2 Changed 5 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Tim Graham

Maybe it would make sense to add a parameter: json_dumps_options=None which could be a dictionary of options.

comment:4 Changed 5 years ago by Sambhav Satija

Owner: changed from nobody to Sambhav Satija
Status: newassigned

Going to add parameter which would take in dictionary and pass kwargs as suggested by timgraham.

comment:5 Changed 5 years ago by Sambhav Satija

Has patch: set

comment:6 Changed 5 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

Looks okay, pending a few cosmetic issues.

comment:7 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In d0bd533:

Fixed #25254 -- Added JsonResponse json_dumps_params parameter.

Note: See TracTickets for help on using tickets.
Back to Top