📋 Template System¶
Functions for enforcing consistent data structures and validation.
🎯 Overview¶
Template system provides data validation and structure enforcement for reliable deserialization.
📦 Functions¶
TemplateDeserializer¶
datason.TemplateDeserializer(template: Any, strict: bool = True, fallback_auto_detect: bool = True)
¶
Template-based deserializer for enhanced type fidelity and round-trip scenarios.
This class allows users to provide a template object that guides the deserialization process, ensuring that the output matches the expected structure and types.
Initialize template deserializer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template
|
Any
|
Template object to guide deserialization |
required |
strict
|
bool
|
If True, raise errors when structure doesn't match |
True
|
fallback_auto_detect
|
bool
|
If True, use auto-detection when template doesn't match |
True
|
Source code in datason/deserializers_new.py
deserialize(obj: Any) -> Any
¶
Deserialize object using template guidance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
Serialized object to deserialize |
required |
Returns:
Type | Description |
---|---|
Any
|
Deserialized object matching template structure |
Source code in datason/deserializers_new.py
deserialize_with_template()¶
datason.deserialize_with_template(obj: Any, template: Any, **kwargs: Any) -> Any
¶
Convenience function for template-based deserialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
Any
|
Serialized object to deserialize |
required |
template
|
Any
|
Template object to guide deserialization |
required |
**kwargs
|
Any
|
Additional arguments for TemplateDeserializer |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Deserialized object matching template structure |
Examples:
>>> import pandas as pd
>>> template_df = pd.DataFrame({'a': [1], 'b': ['text']})
>>> serialized_data = [{'a': 2, 'b': 'hello'}, {'a': 3, 'b': 'world'}]
>>> result = deserialize_with_template(serialized_data, template_df)
>>> isinstance(result, pd.DataFrame)
True
>>> result.dtypes['a'] # Should match template
int64
Source code in datason/deserializers_new.py
infer_template_from_data()¶
datason.infer_template_from_data(data: Any, max_samples: int = 100) -> Any
¶
Infer a template from sample data.
This function analyzes sample data to create a template that can be used for subsequent template-based deserialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
Sample data to analyze (list of records, DataFrame, etc.) |
required |
max_samples
|
int
|
Maximum number of samples to analyze |
100
|
Returns:
Type | Description |
---|---|
Any
|
Inferred template object |
Examples:
>>> sample_data = [
... {'name': 'Alice', 'age': 30, 'date': '2023-01-01T10:00:00'},
... {'name': 'Bob', 'age': 25, 'date': '2023-01-02T11:00:00'}
... ]
>>> template = infer_template_from_data(sample_data)
>>> # template will be a dict with expected types
Source code in datason/deserializers_new.py
create_ml_round_trip_template()¶
datason.create_ml_round_trip_template(ml_object: Any) -> Dict[str, Any]
¶
Create a template optimized for ML object round-trip serialization.
This function creates templates specifically designed for machine learning workflows where perfect round-trip fidelity is crucial.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ml_object
|
Any
|
ML object (model, dataset, etc.) to create template for |
required |
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
Template dictionary with ML-specific metadata |
Examples:
>>> import sklearn.linear_model
>>> model = sklearn.linear_model.LogisticRegression()
>>> template = create_ml_round_trip_template(model)
>>> # template will include model structure, parameters, etc.
Source code in datason/deserializers_new.py
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 |
|
🔗 Related Documentation¶
- Modern Deserialization - load_perfect() uses templates
- Core Functions - Basic serialization functions