json
JSONBaseDataclass
Bases: JSONDataclass
This class should be used in place of JSONDataclass when you intend to inherit from the class.
When converting a subclass to a dict with to_dict, it will store the subclass's fully qualified type in the type field. It will also resolve this type when calling from_dict.
Source code in fancy_dataclass/json.py
JSONDataclass
Bases: DictFileSerializableDataclass, JSONSerializable
Dataclass mixin enabling default serialization of dataclass objects to and from JSON.
Source code in fancy_dataclass/json.py
JSONSerializable
Bases: TextFileSerializable
Mixin class enabling conversion of an object to/from JSON.
Source code in fancy_dataclass/json.py
from_json(fp, **kwargs)
classmethod
Constructs an object from a JSON file-like object (text or binary).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
AnyIO
|
A readable file-like object |
required |
kwargs |
Any
|
Keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
Converted object of this class |
Source code in fancy_dataclass/json.py
from_json_string(s, **kwargs)
classmethod
Constructs an object from a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s |
str
|
JSON string |
required |
kwargs |
Any
|
Keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Self
|
Converted object of this class |
Source code in fancy_dataclass/json.py
json_encoder()
classmethod
Override this method to create a custom JSONEncoder to handle specific data types.
A skeleton for this looks like:
Source code in fancy_dataclass/json.py
json_key_decoder(key)
classmethod
to_json(fp, **kwargs)
Writes the object as JSON to a file-like object (text or binary). If binary, applies UTF-8 encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp |
IOBase
|
A writable file-like object |
required |
kwargs |
Any
|
Keyword arguments |
{}
|
Source code in fancy_dataclass/json.py
to_json_string(**kwargs)
Converts the object into a JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs |
Any
|
Keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Object rendered as a JSON string |