🏛️ Bundle 2024-01
Object serialization and deserialization

Serialization and deserialization of objects

The dessia_common library provides methods for serializing and deserializing Python objects that inherit from either DessiaObject or PhysicalObject. Serialization transforms a Python object into a JSON file, while deserialization performs the inverse operation.

The benefits of these methods are twofold. Firstly, they allow for the transfer of Python objects through the Dessia API. Secondly, they enable the storage of Python objects in a database. Serialization is performed using the "to_dict()" method. For example, in the case of bearing, the resulting JSON takes the following form.

json = {'object_class': '__main__.Bearing',
	    'name': '',
	    'ball': {'object_class': '__main__.Ball', 'name': '', 'diameter': 0.01},
	    'internal_diameter': 0.1,
	    'external_diameter': 0.13,
	    'height': 0.05,
	    '_references': {}}

The inverse operation of deserialization requires the use of a class method because the goal is to instantiate an object from a JSON file (the object has not yet been instantiated, hence the need to use a class method). The method that enables this operation is "dict_to_object()", and it is used in the following manner:

bearing1 = Bearing.dict_to_object(json)

These methods can be customized, but in the context of standard usage of the Dessia platform, it is not recommended to do so.