📋 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
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 |
|
🔗 Related Documentation¶
- Modern Deserialization - load_perfect() uses templates
- Core Functions - Basic serialization functions