question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support serialization of NamedTuples by default

See original GitHub issue

Hi! 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:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ijlcommented, Apr 4, 2019

Ok, if namedtuple becomes a simple type like a dict (in terms of one way of serializing it) that helps.

0reactions
belm0commented, Oct 12, 2022

It’s unfortunate not to support a type that json handles. Because if I’m wrapping orjson for compatibility with json, this seems to be the only point to need an unknown-type handler. Moreover, if the caller of the wrapped dumps() is supplying a custom default= himself, then my wrapper must implement chaining with the NamedTuple handler…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found