This Python package provides an extended JSON encoder class, `JSONSerializer`, that enables encoding of complex Python data types such as `datetime.datetime`, `datetime.date`, `datetime.time`, `bytes` and `uuid`. It also supports objects that have a `to_json` method, allowing for customizable JSON encoding.
Project description
Extended JSON Encoder
The json-advanced is Python package provides an extended JSON encoder class, JSONSerializer, that enables encoding of complex Python data types such as datetime.datetime, datetime.date, datetime.time, bytes and uuid. It also supports objects that have a to_json method, allowing for customizable JSON encoding.
Features
- Datetime Handling: Automatically converts
datetime.datetime,datetime.date, anddatetime.timeobjects to their string representation. - UUID Encoding: Encodes
uuidobjects as uuid strings. - Bytes Encoding: Encodes
bytesobjects as base64 strings, prefixed withb64:. - Custom Object Support: Encodes any object that has a
to_jsonmethod by calling that method.
Installation
You can install the package directly from source:
pip install json-advanced
Usage
To use the JSONSerializer in your project, you need to import it and use it with the standard json module's dump or dumps functions:
import json
import datetime
import uuid
from json_advanced.json_encoder import JSONSerializer
# Example object containing various complex data types
data = {
"now": datetime.datetime.now(),
"today": datetime.date.today(),
"time": datetime.datetime.now().time(),
"bytes_data": b"example bytes",
"uuid": uuid.uuid4(),
}
# Serialize the object to a JSON string
json_string = json.dumps(data, cls=JSONSerializer)
print(json_string)
Extending the Serializer
If you have custom types that you want to serialize, you can extend JSONSerializer by overriding the default method. Ensure you call super().default(obj) for types you do not handle:
class MyCustomSerializer(JSONSerializer):
def default(self, obj):
if isinstance(obj, MyCustomType):
return obj.custom_serialize()
return super().default(obj)
Contributions
Contributions are welcome! Please open an issue or pull request on GitHub if you have suggestions or improvements.
License
This package is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_advanced-0.12.11.tar.gz.
File metadata
- Download URL: json_advanced-0.12.11.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f5572a6928f06d38816ac3fe6778646ce320a64c6f34e0528510c5493725826
|
|
| MD5 |
3f572734a6d9d68ba51cf7a1d8cfc4a3
|
|
| BLAKE2b-256 |
582d6095da1da158438d759e44a6b1527994e032878092da21a4ae997e79cb06
|
File details
Details for the file json_advanced-0.12.11-py3-none-any.whl.
File metadata
- Download URL: json_advanced-0.12.11-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
caad19a6c9e77b22374b5eb57baf2c4d9f4f5961a02cecbc8d268e566cfadcd7
|
|
| MD5 |
7dae94362befe53afde8f19912476c3b
|
|
| BLAKE2b-256 |
6b95a77415e5607095417fcd57eecb6990bdf3dec7cf0c7a331c8b7430a76f53
|