Serialization
The serialization module provides secure methods for serializing and deserializing Money objects.
Module for secure serialization and deserialization of Money objects.
This module provides functions to safely convert Money objects to and from various serialized formats (JSON, dictionaries) with appropriate validation.
- moneyx.serialization.to_dict(money)[source]
Convert a Money object to a dictionary representation.
- Parameters:
money (
Any) – A Money object- Return type:
- Returns:
Dictionary with amount, currency, and rounding mode
Example
>>> from moneyx import Money >>> money = Money("10.50", "USD") >>> to_dict(money) {'amount': '10.50', 'currency': 'USD', 'rounding': 'HALF_UP'}
- moneyx.serialization.from_dict(cls, data)[source]
Create a Money object from a dictionary.
- Parameters:
- Return type:
TypeVar(T, bound= Money)- Returns:
New Money object
- Raises:
SerializationError – If the dictionary is missing required fields or has invalid data
Example
>>> from moneyx import Money >>> data = {'amount': '10.50', 'currency': 'USD', 'rounding': 'HALF_UP'} >>> from_dict(Money, data) <Money 10.50 USD>
- moneyx.serialization.to_json(money, indent=None)[source]
Convert a Money object to a JSON string.
- Parameters:
- Return type:
- Returns:
JSON string representation
Example
>>> from moneyx import Money >>> money = Money("10.50", "USD") >>> to_json(money) '{"amount": "10.50", "currency": "USD", "rounding": "HALF_UP"}'
- moneyx.serialization.from_json(cls, json_str)[source]
Create a Money object from a JSON string.
- Parameters:
- Return type:
TypeVar(T, bound= Money)- Returns:
New Money object
- Raises:
SerializationError – If the JSON is invalid or missing required fields
Example
>>> from moneyx import Money >>> json_str = '{"amount": "10.50", "currency": "USD", "rounding": "HALF_UP"}' >>> from_json(Money, json_str) <Money 10.50 USD>