pysdic.core.serialization.JsonSerializerMixin#
Note
This class is internal: it is not exported in the pysdic namespace. Its methods are
available on all the classes that inherit from it.
Serialization to and from JSON strings and files.
- class JsonSerializerMixin[source]#
Bases:
SerializerMixinMixin class to provide JSON serialization and deserialization capabilities. Classes that inherit from this mixin can be easily converted to and from JSON format.
Important
Numpy arrays, numpy scalars and sets are not directly serializable to JSON. They are converted to lists and Python scalars when serializing.
When deserializing from JSON using the
from_dictmethod, ensure that you convert lists back to numpy arrays if needed.- classmethod from_json_file(file_path, **kwargs)[source]#
Deserialize a JSON file to an object of the class.
- Parameters:
file_path (Any) – The path to the JSON file containing the serialized class data (str, Path, …).
- Return type:
- classmethod from_json_string(json_string, **kwargs)[source]#
Deserialize a JSON string to an object of the class.
This method parses the input JSON string, converts it to a dictionary, and then uses the
from_dictmethod to create an instance of the class.- Parameters:
json_string (str) – A JSON string representation of the object.
- Returns:
An instance of the class, populated with the data from the JSON string.
- Return type:
Any
- Raises:
ValueError – If the input JSON string is not valid or cannot be parsed.
- to_json_file(file_path, *, make_dirs=False, **kwargs)[source]#
Serialize the object to a JSON file.
- Parameters:
file_path (Any) – The path to the file where the JSON representation of the class will be saved (str, Path, …).
make_dirs (bool, optional (default:
False)) – If True, the method will create any missing directories in the file path before writing the JSON.
- Return type:
None
- to_json_string(**kwargs)[source]#
Serialize the object to a JSON string.
This method uses the
to_dictmethod to convert the object to a dictionary, and then converts that dictionary to a JSON string.- Returns:
A JSON string representation of the object, with indentation for readability.
- Return type:
- Raises:
ValueError – If the dictionary returned by
to_dictcannot be encoded to JSON.