haive.core.common.mixins.general.serialization¶

Serialization mixin for enhanced data conversion capabilities.

from typing import Any This module provides a mixin for adding enhanced serialization and deserialization capabilities to Pydantic models. It offers methods for converting models to dictionaries and JSON strings, and for creating models from dictionaries and JSON strings.

Usage:

from pydantic import BaseModel from haive.core.common.mixins.general import SerializationMixin

class User(SerializationMixin, BaseModel):

id: str name: str age: int _private_data: str = “hidden”

# Create a user user = User(id=”123”, name=”Alice”, age=30)

# Serialize to dict (excludes _private_data by default) user_dict = user.to_dict()

# Serialize to JSON with indentation user_json = user.to_json(indent=2)

# Deserialize from dict new_user = User.from_dict(user_dict)

# Deserialize from JSON new_user = User.from_json(user_json)

Classes¶

SerializationMixin

Mixin for enhanced serialization and deserialization capabilities.

Module Contents¶

class haive.core.common.mixins.general.serialization.SerializationMixin(/, **data)[source]¶

Bases: pydantic.BaseModel

Mixin for enhanced serialization and deserialization capabilities.

This mixin provides methods for converting Pydantic models to dictionaries and JSON strings, and for creating models from dictionaries and JSON strings. It handles private fields (starting with underscore) appropriately.

When combined with other mixins like IdMixin, TimestampMixin, etc., it provides a complete solution for model persistence.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

data (Any)

classmethod from_dict(data)[source]¶

Create instance from dictionary.

This class method creates a model instance from a dictionary, using Pydantic’s validation.

Parameters:

data (dict[str, Any]) – Dictionary containing model data.

Returns:

New model instance.

classmethod from_json(json_str)[source]¶

Create instance from JSON string.

This class method creates a model instance from a JSON string, parsing the JSON and then using from_dict().

Parameters:

json_str (str) – JSON string containing model data.

Returns:

New model instance.

to_dict(exclude_private=True)[source]¶

Convert to dictionary with options.

This method converts the model to a dictionary, with the option to exclude private fields (those starting with an underscore).

Parameters:

exclude_private (bool) – Whether to exclude private fields.

Returns:

Dictionary representation of the model.

Return type:

dict[str, Any]

to_json(exclude_private=True, **kwargs)[source]¶

Convert to JSON string.

This method converts the model to a JSON string, with options for controlling the JSON serialization.

Parameters:
  • exclude_private (bool) – Whether to exclude private fields.

  • **kwargs – Additional arguments to pass to json.dumps().

Returns:

JSON string representation of the model.

Return type:

str