Skip to main content

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, and datetime.time objects to their string representation.
  • UUID Encoding: Encodes uuid objects as uuid strings.
  • Bytes Encoding: Encodes bytes objects as base64 strings, prefixed with b64:.
  • 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.

Metadata

Release files for json-advanced 0.13.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for json-advanced 0.13.0
File Size Uploaded
json_advanced-0.13.0.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for json-advanced 0.13.0
File Interpreter ABI Platform
json_advanced-0.13.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / json_advanced-0.13.0.tar.gz

Download URL json_advanced-0.13.0.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0e8b9b28596e96b062460099d89d5d0dd9083c81e7eb351cbfb70fc82b6f2b08
BLAKE2b-256 checksum
How to use checksums
552130e167c777feb9a324df3cf1511928d293e10101194a5f74aba96fc3d81e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / json_advanced-0.13.0-py3-none-any.whl

Download URL json_advanced-0.13.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
12dab918cacf0a101721da35136937f6a7560f42f002ffb352f91d6fd7204cf5
BLAKE2b-256 checksum
How to use checksums
48fff06e1109020fab3ec44d8fb1c95be81554864d8a15bccfa49d8df435f9c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.13.0 This release

2 release files

0.12.9

2 release files

0.12.8

2 release files

0.12.4

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page