Json
provide.foundation.serialization.json
¶
TODO: Add module docstring.
Functions¶
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]