Serialization
provide.foundation.serialization
¶
TODO: Add module docstring.
Functions¶
env_dumps
¶
Serialize dictionary to .env file format string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict[str, str]
|
Dictionary of environment variables |
required |
quote_values
|
bool
|
Whether to quote string values |
True
|
Returns:
| Type | Description |
|---|---|
str
|
.env format string |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If object cannot be serialized |
Example
env_dumps({"KEY": "value"}) 'KEY="value"\n' env_dumps({"KEY": "value"}, quote_values=False) 'KEY=value\n'
Source code in provide/foundation/serialization/env.py
env_loads
¶
Deserialize .env file format string to dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
.env format string to deserialize |
required |
use_cache
|
bool
|
Whether to use caching for this operation |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of environment variables |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If string is not valid .env format |
Example
env_loads('KEY=value') {'KEY': 'value'} env_loads('KEY="value"')
Source code in provide/foundation/serialization/env.py
get_cache_key
¶
Generate cache key from content and format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
str
|
String content to hash |
required |
format
|
str
|
Format identifier (json, yaml, toml, etc.) |
required |
Returns:
| Type | Description |
|---|---|
str
|
Cache key string |
Source code in provide/foundation/serialization/cache.py
ini_dumps
¶
Serialize nested dictionary to INI format string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict[str, dict[str, str]]
|
Nested dictionary (sections -> key-value pairs) |
required |
include_default
|
bool
|
Whether to include DEFAULT section |
False
|
Returns:
| Type | Description |
|---|---|
str
|
INI format string |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If object cannot be serialized |
Example
ini_dumps({"section": {"key": "value"}}) '[section]\nkey = value\n\n'
Source code in provide/foundation/serialization/ini.py
ini_loads
¶
Deserialize INI format string to nested dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
INI format string to deserialize |
required |
use_cache
|
bool
|
Whether to use caching for this operation |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, str]]
|
Nested dictionary (sections -> key-value pairs) |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If string is not valid INI format |
Example
ini_loads('[section]\nkey = value') {'section': {'key': 'value'}}
Source code in provide/foundation/serialization/ini.py
json_dumps
¶
json_dumps(
obj: Any,
*,
ensure_ascii: bool = False,
indent: int | None = None,
sort_keys: bool = False,
default: Any = None
) -> str
Serialize object to JSON string with Foundation tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to serialize |
required |
ensure_ascii
|
bool
|
If True, non-ASCII characters are escaped |
False
|
indent
|
int | None
|
Number of spaces for indentation (None for compact) |
None
|
sort_keys
|
bool
|
Whether to sort dictionary keys |
False
|
default
|
Any
|
Function called for objects that can't be serialized |
None
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If object cannot be serialized |
Example
json_dumps({"key": "value"}) '{"key": "value"}' json_dumps({"b": 1, "a": 2}, sort_keys=True, indent=2) '{\n "a": 2,\n "b": 1\n}'
Source code in provide/foundation/serialization/json.py
json_loads
¶
Deserialize JSON string to Python object with Foundation tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
JSON string to deserialize |
required |
use_cache
|
bool
|
Whether to use caching for this operation |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If string is not valid JSON |
Example
json_loads('{"key": "value"}') {'key': 'value'} json_loads('[1, 2, 3]') [1, 2, 3]
Source code in provide/foundation/serialization/json.py
toml_dumps
¶
Serialize dictionary to TOML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
dict[str, Any]
|
Dictionary to serialize (TOML requires dict at top level) |
required |
Returns:
| Type | Description |
|---|---|
str
|
TOML string representation |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If object cannot be serialized |
ImportError
|
If tomli-w is not installed |
Example
toml_dumps({"key": "value"}) 'key = "value"\n'
Source code in provide/foundation/serialization/toml.py
toml_loads
¶
Deserialize TOML string to Python dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
TOML string to deserialize |
required |
use_cache
|
bool
|
Whether to use caching for this operation |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Deserialized Python dictionary |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If string is not valid TOML |
Example
toml_loads('key = "value"')
Source code in provide/foundation/serialization/toml.py
yaml_dumps
¶
yaml_dumps(
obj: Any,
*,
default_flow_style: bool = False,
allow_unicode: bool = True,
sort_keys: bool = False
) -> str
Serialize object to YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to serialize |
required |
default_flow_style
|
bool
|
Use flow style (JSON-like) instead of block style |
False
|
allow_unicode
|
bool
|
If True, allow unicode characters |
True
|
sort_keys
|
bool
|
Whether to sort dictionary keys |
False
|
Returns:
| Type | Description |
|---|---|
str
|
YAML string representation |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If object cannot be serialized |
ImportError
|
If PyYAML is not installed |
Example
yaml_dumps({"key": "value"}) 'key: value\n'
Source code in provide/foundation/serialization/yaml.py
yaml_loads
¶
Deserialize YAML string to Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
YAML string to deserialize |
required |
use_cache
|
bool
|
Whether to use caching for this operation |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
Deserialized Python object |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If string is not valid YAML |
ImportError
|
If PyYAML is not installed |
Example
yaml_loads('key: value') {'key': 'value'} yaml_loads('[1, 2, 3]') [1, 2, 3]