suggestion: json.dump() does not support default keyword argument
Description
In the standard Python3 JSON library (https://docs.python.org/3/library/json.html), json.dump() or json.dumps() support the default keyword argument which specifies how to serialize non-standard data types.
default (callable | None) – A function that is called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError. If None (the default), TypeError is raised.
Currently, this is not implemented:
>>> json.dumps({}, default=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extra keyword arguments given
I think this will be a useful addition. Thanks!
Code Size
No response
Implementation
I hope the MicroPython maintainers or community will implement this feature
Code of Conduct
Yes, I agree
extmod/modjson: Add default argument for json.dump(s).
<!-- Thanks for submitting a Pull Request! We appreciate you spending the
time to improve MicroPython. Please provide enough information so that
others can review your Pull Request.
Before submitting, please read:
https://github.com/micropython/micropython/blob/master/CODEOFCONDUCT.md
https://github.com/micropython/micropython/wiki/ContributorGuidelines
Please check any CI failures that appear after your Pull Request is opened.
-->
Summary
As mentioned in the issue, the CPython api for json.dump and json.dumps allows for the named argument default to be provided. It is a callable type that specifies a way for non-serializable objects to be serialized. This patch series adds support for this argument in Micropython along with the documentation update and extending tests to cover the new argument.
CPython reference: https://docs.python.org/3/library/json.html
default (callable | None) – A function that is called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError. If None (the default), TypeError is raised.
Fixes: #16963
Testing
Tested on the Unix port.