← index #16963PR #18530
Duplicate · high · value 4.178
QUERY · ISSUE

suggestion: json.dump() does not support default keyword argument

openby xuancong84opened 2025-03-20updated 2025-03-20
enhancement

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

CANDIDATE · PULL REQUEST

extmod/modjson: Add default argument for json.dump(s).

openby alectric-tropened 2025-12-07updated 2026-03-21
extmod

<!-- 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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied