json_tools
Class info¶
Re-exported classes¶
| Class | Module | Description |
|---|---|---|
| JsonDumpError | configz.json_tools.base | Unified exception for all JSON serialization errors. |
| JsonLoadError | configz.json_tools.base | Unified exception for all JSON parsing errors. |
| OrJsonProvider | configz.json_tools.orjson_provider.provider | OrJSON implementation of the JSON provider interface. |
| ParseErrorInfo | configz.parse_errors | Structured information about a parse error. |
🛈 DocStrings¶
Package for JSON-related tools with validation support.
Provides a unified interface for JSON serialization/deserialization with automatic selection of the best available backend and type validation.
JsonDumpError
¶
JsonLoadError
¶
Bases: Exception
Unified exception for all JSON parsing errors.
Source code in src/configz/json_tools/base.py
source_path
property
¶
Path to the source file that caused the error.
format
¶
Format error with source context for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_lines
|
int
|
Number of lines to show before/after the error line. |
2
|
use_color
|
bool
|
Whether to include ANSI color codes. |
True
|
Source code in src/configz/json_tools/base.py
ParseErrorInfo
dataclass
¶
Structured information about a parse error.
Source code in src/configz/parse_errors.py
format
¶
Format error with source context for display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context_lines
|
int
|
Number of lines to show before/after the error line. |
2
|
use_color
|
bool
|
Whether to include ANSI color codes. |
True
|
Source code in src/configz/parse_errors.py
dump_json
¶
dump_json(
data: Any,
indent: bool = False,
naive_utc: bool = False,
serialize_numpy: bool = False,
sort_keys: bool = False,
backend: BackendType = "auto",
) -> str
Serialize data to a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The data to serialize |
required |
indent
|
bool
|
Whether to format the output with indentation |
False
|
naive_utc
|
bool
|
Whether to interpret naive datetime objects as UTC |
False
|
serialize_numpy
|
bool
|
Whether to serialize numpy arrays |
False
|
sort_keys
|
bool
|
Sort dictionary keys |
False
|
backend
|
BackendType
|
JSON backend to use for serialization |
'auto'
|
Returns:
| Type | Description |
|---|---|
str
|
The serialized JSON string |
Raises:
| Type | Description |
|---|---|
JsonDumpError
|
If serialization fails |
Source code in src/configz/json_tools/__init__.py
get_json_provider
¶
get_json_provider(backend: BackendType = 'auto') -> type[JsonProviderBase]
Get the specified JSON provider or the best available one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
BackendType
|
The JSON backend to use. If "auto", uses the best available. |
'auto'
|
Returns:
| Type | Description |
|---|---|
type[JsonProviderBase]
|
A JSON provider implementation |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the requested backend is not available |
Source code in src/configz/json_tools/__init__.py
load_json
¶
load_json(
data: str | bytes | TextIOWrapper,
return_type: type[T] | None = None,
backend: BackendType = "auto",
) -> T
Load JSON data with optional type validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | bytes | TextIOWrapper
|
The JSON data to parse (string, bytes, or file-like object) |
required |
return_type
|
type[T] | None
|
Optional type to validate the data against |
None
|
backend
|
BackendType
|
JSON backend to use for parsing |
'auto'
|
Returns:
| Type | Description |
|---|---|
T
|
The parsed and validated data |
Raises:
| Type | Description |
|---|---|
JsonLoadError
|
If parsing fails |
TypeError
|
If validation against return_type fails |