(answering my own question, more details or corrections welcomed) I have spent some time looking into these questions and I think I have better understanding now.

  1. Q: Is there no universal object serialization method in Python (to json)?
    A: no there is not. Quoting @gilch,

There is no universal object serialization in Python, full stop.

The closest equivalent there is, appears to be the module jsonpickle though it has some security considerations. With this module, it's going to work for the reasonable majority of the objects, and can be further expanded to support e.g. numpy types, too. Where the serialized content goes outside, the serialization can be done with unpicklable=False, which generates the regular JSON form of a DTO one may expect from e.g. dotnet.

string = jsonpickle.dumps(person1, unpicklable=False)
print(string)
>> {"name": "Mike", "age": 34, "learnsPython": true, "controllers": ["XboX", "Super Nintendo"]}

For the round-robin, this parameter has to be omitted or set to true which results in some metadata field(s) added to the payload, but this enables the JSON to be converted back to the source object with loads():

string = jsonpickle.dumps(person1, unpicklable=False)
print(string)
>> {"py/object": "__main__.Person", "name": "Mike", "age": 34, "learnsPython": true, "controllers": ["XboX", "Super Nintendo"]

There is also a number of alternatives to be used with other libraries (e.g. json), namely dumps(myObject.__dict__), dumps(vars(myType)), etc., up to custom default() methods and type-specific serializers.

  1. Q:Why is this so?
    A: (my speculation here based on the code I read) Since Python does not support constructor overloads, which eliminates for most of the types the availability of a parameterless constructor, serializing and deserializing types requires to have custom approach for a number of known types out there. This also calls for strict data contracts that serializers would have to adhere to in order to deserialize the objects back - and this is what pickle/jsonpickle does.
    Speculation #2: I assume the decision to put the type into the data instead of allowing the caller to specify the type was done for the purpose of data integrity, and to follow the established pickle serialization practice.
    Speculation #3: the generics only exist in Python since 3.5, so relatively recently, and hence are not yet used as a technique for deserialization.
Answer from Mike Makarov on Stack Overflow
๐ŸŒ
LearnPython.com
learnpython.com โ€บ blog โ€บ object-serialization-in-python
Object Serialization in Python | LearnPython.com
Serialization is essentially a way of storing data or objects and is a useful technique for saving complex objects. Itโ€™s the process of converting an object into a byte stream that can be stored, for example in memory or to a file.
Top answer
1 of 2
4

(answering my own question, more details or corrections welcomed) I have spent some time looking into these questions and I think I have better understanding now.

  1. Q: Is there no universal object serialization method in Python (to json)?
    A: no there is not. Quoting @gilch,

There is no universal object serialization in Python, full stop.

The closest equivalent there is, appears to be the module jsonpickle though it has some security considerations. With this module, it's going to work for the reasonable majority of the objects, and can be further expanded to support e.g. numpy types, too. Where the serialized content goes outside, the serialization can be done with unpicklable=False, which generates the regular JSON form of a DTO one may expect from e.g. dotnet.

string = jsonpickle.dumps(person1, unpicklable=False)
print(string)
>> {"name": "Mike", "age": 34, "learnsPython": true, "controllers": ["XboX", "Super Nintendo"]}

For the round-robin, this parameter has to be omitted or set to true which results in some metadata field(s) added to the payload, but this enables the JSON to be converted back to the source object with loads():

string = jsonpickle.dumps(person1, unpicklable=False)
print(string)
>> {"py/object": "__main__.Person", "name": "Mike", "age": 34, "learnsPython": true, "controllers": ["XboX", "Super Nintendo"]

There is also a number of alternatives to be used with other libraries (e.g. json), namely dumps(myObject.__dict__), dumps(vars(myType)), etc., up to custom default() methods and type-specific serializers.

  1. Q:Why is this so?
    A: (my speculation here based on the code I read) Since Python does not support constructor overloads, which eliminates for most of the types the availability of a parameterless constructor, serializing and deserializing types requires to have custom approach for a number of known types out there. This also calls for strict data contracts that serializers would have to adhere to in order to deserialize the objects back - and this is what pickle/jsonpickle does.
    Speculation #2: I assume the decision to put the type into the data instead of allowing the caller to specify the type was done for the purpose of data integrity, and to follow the established pickle serialization practice.
    Speculation #3: the generics only exist in Python since 3.5, so relatively recently, and hence are not yet used as a technique for deserialization.
2 of 2
0

The pickle module can be used for binary serialization of most Python objects.

JSON can only represent a subset of Python by default. See the Python documentation's comparison of pickle and JSON for more details.

However, for many purposes, converting the dictionary representation of an object to JSON may be sufficient:

import json

class Person:
    def __init__(self, name, age, learnsPython, controllers):
        self.name = name
        self.age = age
        self.learnsPython = learnsPython
        self.controllers = controllers

person = Person("Mike", 34, True, ['XboX', 'Super Nintendo'])

print(json.dumps(person.__dict__))

Output:

{"name": "Mike", "age": 34, "learnsPython": true, "controllers": ["XboX", "Super Nintendo"]}
Discussions

Help with serializing and deserializing custom class objects in Python!
Can you share your code? Or at least the parts that are relevant to the question? More on reddit.com
๐ŸŒ r/learnpython
29
2
February 16, 2025
Python safe serialization?
Not builtin to the standard library, but it shouldn't be too hard to roll your own on top of json. If the instances are of a known set of types all you need is the type name and the json serializion of the instance members. So something like: json.dumps([type(obj).__name__, obj.__dict__]) Deserializing: name, _dict = json.loads(the_json); instance = object.__new__(getattr(module, name)); instance.__dict__.update(_dict) More on reddit.com
๐ŸŒ r/Python
13
8
June 27, 2011
Yet another object serialization framework!
Interesting. Any metrics on speed? More on reddit.com
๐ŸŒ r/Python
9
10
September 6, 2022
What is your recommended way to serialize objects?
Help with serializing and deserializing custom class objects in Python! More on reddit.com
๐ŸŒ r/learnpython
1
0
March 30, 2022
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ pickle.html
pickle โ€” Python object serialization
February 23, 2026 - JSON is a text serialization format (it outputs unicode text, although most of the time it is then encoded to utf-8), while pickle is a binary serialization format; ... JSON is interoperable and widely used outside of the Python ecosystem, while pickle is Python-specific; JSON, by default, can only represent a subset of the Python built-in types, and no custom classes; pickle can represent an extremely large number of Python types (many of them automatically, by clever usage of Pythonโ€™s introspection facilities; complex cases can be tackled by implementing specific object APIs);
๐ŸŒ
The Hitchhiker's Guide to Python
docs.python-guide.org โ€บ scenarios โ€บ serialization
Data Serialization โ€” The Hitchhiker's Guide to Python
If the data to be serialized is located in a file and contains flat data, Python offers two methods to serialize data. The repr method in Python takes a single object parameter and returns a printable representation of the input:
๐ŸŒ
MachineLearningMastery
machinelearningmastery.com โ€บ home โ€บ blog โ€บ a gentle introduction to serialization for python
A Gentle Introduction to Serialization for Python - MachineLearningMastery.com
June 21, 2022 - Serialization refers to the process of converting a data object (e.g., Python objects, Tensorflow models) into a format that allows us to store or transmit the data and then recreate the object when needed using the reverse process of ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ object_oriented_python โ€บ object_oriented_python_serialization.htm
Object Oriented Python - Object Serialization
In serialization, an object is transformed into a format that can be stored, so as to be able to deserialize it later and recreate the original object from the serialized format. Pickling is the process whereby a Python object hierarchy is converted ...
๐ŸŒ
Real Python
realpython.com โ€บ python-serialize-data
Serialize Your Data With Python โ€“ Real Python
December 4, 2023 - Python ships with the following modules in the standard library, which provide binary data serialization formats for different purposes: ... In practice, youโ€™ll almost always want to serialize your objects with pickle, which is the standard data serialization format in Python.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ serialize-and-deserialize-an-open-file-object-in-python
Serialize and Deserialize an Open File Object in Python - GeeksforGeeks
July 23, 2025 - This Python script serializes a dictionary `data` into a JSON file named 'data.json', then deserializes it back into `loaded_data` from the same file, printing the result.
๐ŸŒ
Medium
gaurav-adarshi.medium.com โ€บ serialization-techniques-in-python-297a4489ba87
Serialization techniques in Python | by Gaurav Kumar | Medium
April 24, 2024 - Serialization in Python refers to the process of converting complex data structures, such as objects or data collections, into a format that can be easily stored or transmitted. This is often done using modules like pickle or JSON.
๐ŸŒ
Diveintopython3
diveintopython3.net โ€บ serializing.html
Serializing Python Objects - Dive Into Python 3
You can never go wrong with UTF-8. Like the pickle module, the json module defines a dump() function which takes a Python data structure and a writeable stream object. The dump() function serializes the Python data structure and writes it to the stream object.
๐ŸŒ
KnowledgeHut
knowledgehut.com โ€บ https://www.knowledgehut.com โ€บ tutorials โ€บ programming tutorials
Python Object Serialization Tutorial | Learn Serialization in Python
Object serialization is the process of converting state of an object into byte stream. This byte stream can further be stored in any file-like object such as a disk file or memory stream.
๐ŸŒ
Medium
medium.com โ€บ @ellysam โ€บ serialization-and-deserialization-in-python-a26780187359
Serialization and Deserialization in Python | by elijah samson | Medium
February 9, 2023 - Serialization is the process of converting an objectโ€™s state to a byte stream, and deserialization is the reverse process of reconstructing the object from a byte stream. This process of converting objects to a stream of bytes is useful in scenarios where data needs to be stored or transferred from one system to another. In Python...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ python_serialization.htm
Python - Serialization
Serialization refers to the process of converting an object into a format that can be easily stored, transmitted, or reconstructed later. In Python, this involves converting complex data structures, such as objects or dictionaries, into a byte
๐ŸŒ
Python
docs.python.org โ€บ 3.4 โ€บ library โ€บ pickle.html
12.1. pickle โ€” Python object serialization โ€” Python 3.4.10 documentation
June 16, 2019 - The pickle module implements binary protocols for serializing and de-serializing a Python object structure. โ€œPicklingโ€ is the process whereby a Python object hierarchy is converted into a byte stream, and โ€œunpicklingโ€ is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ pickle-python-tutorial
Python Pickle Tutorial: Object Serialization | DataCamp
December 13, 2024 - The next time we want to access the same data structure, this sequence of bytes must be converted back into the high-level object in a process known as deserialization. We can use formats such as JSON, XML, HDF5, and Pickle for serialization. In this tutorial, we will learn about the Python Pickle library for serialization.
๐ŸŒ
Real Python
realpython.com โ€บ videos โ€บ serializing-objects
Serializing Objects in Python (Video) โ€“ Real Python
Serialization is the process of converting a data structure into a linear byte stream. 00:12 This means taking complex data and encoding it into a stream of bytes in a way that allows a second operation to take that data and convert it back ...
Published ย  December 22, 2020
๐ŸŒ
Medium
medium.com โ€บ @moraneus โ€บ python-serialization-101-unlock-the-essentials-for-your-projects-5dd8b5bbbcc5
Python Serialization 101: Unlock the Essentials for Your Projects | by Moraneus | Medium
May 17, 2024 - In essence, serialization converts an in-memory object into a byte stream, which can then be stored in a file, sent over a network, or held in memory for later use. Deserialization is the reverse process, converting the byte stream back into ...
๐ŸŒ
Hynek
hynek.me โ€บ articles โ€บ serialization
Better Python Object Serialization
May 17, 2022 - The Python standard library is full of underappreciated gems. One of them allows for simple and elegant function dispatching based on argument types. This makes it perfect for serialization of arbitrary objects โ€“ for example to JSON in web APIs and structured logs.
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Serialization
Serialization - Learn Python - Free Interactive Python Tutorial
This method takes an object and ... "b", "c"]) print(json_string) Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle)....