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.time
objects to their string representation. - UUID Encoding: Encodes
uuid
objects as uuid strings. - Bytes Encoding: Encodes
bytes
objects as base64 strings, prefixed withb64:
. - Custom Object Support: Encodes any object that has a
to_json
method 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
File details
Details for the file json_advanced-0.12.6.tar.gz
.
File metadata
- Download URL: json_advanced-0.12.6.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc96906d8be1a78d178d45ffdee3648dd4cf40a19f041e03ff330b21f5d55aec |
|
MD5 | 94721adc2abc5086a444e6a6be317e14 |
|
BLAKE2b-256 | 81e8620e37b9fc10ad7b8db5fa00bb6abb6ad48f84b4b8358653a00be694dc88 |
File details
Details for the file json_advanced-0.12.6-py3-none-any.whl
.
File metadata
- Download URL: json_advanced-0.12.6-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20fbe9bc9b7a95276ca20c259221400b6d9f9c31a83cec20f44604d254511b29 |
|
MD5 | c8e11d86fb1fb5fece08d032543cfe75 |
|
BLAKE2b-256 | 1826852841fa1fbfc07eea0cee3dd2c47f62de6478a09a63277b0b46a840de83 |