Support serialization of NamedTuples by default
See original GitHub issueHi! It would be cool to have it implemented. So far simplejson
seems to be the only one that does it right (stdlib’s json
serializes named tuples as list values, which is hardly a desired default behaviour):
>>> from simplejson import dumps
>>> from typing import NamedTuple
>>> class X(NamedTuple):
... x: int
...
>>> dumps(X(1))
'{"x": 1}'
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Serializing a Python namedtuple to json - Stack Overflow
It can go from and to named-tuple and back. It supports quite complicated nested structures, with lists, sets, enums, unions, default values. It ......
Read more >Everything You Need to Know About Python's Namedtuples
Master how to use named tuples in Python by examples. Learn to convert namedtuple to dict, create namedtuple from dict, add optional fields...
Read more >Python namedtuple - ZetCode
Python namedtuple tutorial shows how to work with namedtuples in Python. ... With the help of the json.dumps method, we serialize a single ......
Read more >Search — Python 3.6.15 documentation
Note that the search function will automatically search for all of the words. ... NamedTuple (Python class, in 26.1. typing — Support for...
Read more >pyspark.serializers — PySpark 2.3.3 documentation
PySpark supports custom serializers for transferring data; this can improve performance. By default, PySpark uses L{PickleSerializer} to serialize objects ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Ok, if namedtuple becomes a simple type like a dict (in terms of one way of serializing it) that helps.
It’s unfortunate not to support a type that
json
handles. Because if I’m wrappingorjson
for compatibility withjson
, this seems to be the only point to need an unknown-type handler. Moreover, if the caller of the wrappeddumps()
is supplying a customdefault=
himself, then my wrapper must implement chaining with the NamedTuple handler…