JSON¶
Functions for working with JSON data.
DecimalEncoder ¶
Bases: JSONEncoder
Used to encode decimal.Decimal when printing/encoding dicts to JSON strings
default ¶
default(o)
Encode decimal.Decimal values as strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
o
|
Any
|
The object to encode. |
required |
Returns:
| Type | Description |
|---|---|
str | JSONEncoder
|
String representation for Decimal values, otherwise delegates to parent. |
Source code in src/aibs_informatics_core/utils/json.py
26 27 28 29 30 31 32 33 34 35 36 37 | |
is_json_str ¶
is_json_str(data)
Check if a value is a valid JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The value to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in src/aibs_informatics_core/utils/json.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
load_json ¶
load_json(path_or_str, **kwargs)
Load JSON from a string or file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_str
|
str | Path
|
A JSON string or path to a JSON file. |
required |
**kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
JSON
|
The parsed JSON value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is neither a valid JSON string nor an existing file path. |
Source code in src/aibs_informatics_core/utils/json.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
load_json_object ¶
load_json_object(path_or_str, **kwargs)
Load a JSON object (dict) from a string or file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_str
|
str | Path
|
A JSON string or path to a JSON file. |
required |
**kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, JSON]
|
The parsed JSON object as a dictionary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the loaded JSON is not a dictionary. |
Source code in src/aibs_informatics_core/utils/json.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |