← index #16963PR #498
Off-topic · high · value 0.430
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

Remove json library

closedby mattytrentiniopened 2022-05-02updated 2022-07-10

It's not clear why there's a python implementation of the json library - perhaps it pre-dates json as a full-featured built-in?

If there are no other benefits to using the built-in over the python implementation then we should remove the python implementation.

9 comments
stinos · 2022-05-02

At first sight this implementation implements pretty much everything CPython's implementation has i.e. much more arguments for the dump/load functions than MicroPython's builtin version. Not sure if anyone actually uses this though.

mattytrentini · 2022-05-02

It actually looks like it is the CPython json implementation, albeit an older version.

It really would be good to know if people are using this! Then we could tell if it makes more sense to remove or update. I do think there's a strong case for the former...

DusKing1 · 2022-05-03

I'm using this lib, please do not remove.

mattytrentini · 2022-05-03

I'm using this lib, please do not remove.

Thanks for letting us know @DusKing1, can you also please explain what features of the library - more than the built-in json library - you're using?

At the moment there are no tests and if we're going to continue to support this library we ought to add some; it would make sense to preference the features that are being used.

DusKing1 · 2022-05-03

I'm using this lib, please do not remove.

Thanks for letting us know @DusKing1, can you also please explain what features of the library - more than the built-in json library - you're using?

At the moment there are no tests and if we're going to continue to support this library we ought to add some; it would make sense to preference the features that are being used.

Oh, sorry I might misread this repo and PR. This is not the lib in the micropython firmware but add-on libs for device that are running mpy firmware right?

dbrignoli · 2022-05-03

We also rely on this JSON implementation, please do not remove it. Thank you.

andrewleech · 2022-05-03

We also rely on this JSON implementation, please do not remove it. Thank you.

Hi @dbrignoli, do you use this copy of json that needs to be manually installed from micropython-lib? Not just the normal built in version?

If so, could you give some more details like https://github.com/micropython/micropython-lib/pull/498#issuecomment-1115884552 ?

dbrignoli · 2022-05-03

Hi @andrewleech,

A quick (partial) search of our codebase reveals we use indent and sort_keys kwargs to json.dump(). Additionally, I believe we encountered some "showstopper" issue with ujson which made us switch from ujson to json, but I cannot recall exactly what it was as it happened more than 2 years ago, sorry.

andrewleech · 2022-05-03

@dbrignoli that's great thanks, I absolutely know what it's like to not remember how/why I fixed things years ago! - kwargs usage is a really helpful hint!

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