Read and write json files with datetime objects
Project description
Datetime JSON
Serialize and deserialize datetime objects to and from JSON.
This package provides:
-
4 functions that wrap the corresponding functions of the
json
module:load/loads
- Deserialize a JSON string containing datetime objectsdump/dumps
- Serialize python object containting datetime objects to JSON
-
two classes derived respectively from
json.JSONEncoder
andjson.JSONDecoder
:DateTimeEncoder
- Serialize a datetime object to JSONDateTimeDecoder
- Deserialize a JSON string to a datetime object
Installation
pip install datetimejson
Usage
Just replace json
import by datetimejson
in your code:
>>> from datetimejson import dumps, loads
>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now)
2023-02-13 11:27:56.687439
>>> json_string = dumps(now)
>>> print(json_string)
{"__type__": "datetime", "year": 2023, "month": 2, "day": 13, "hour": 11, "minute": 27, "second": 56, "microsecond": 687439}
>>> print(loads(json_string))
2023-02-13 11:27:56.687439
>>>
Original code by @ApptuitAI: https://gist.github.com/abhinav-upadhyay/5300137
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
datetimejson-0.1.5.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for datetimejson-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd033d213276878f29676448f822b8700cc6ee713102f077e4dc7d6ff55d6866 |
|
MD5 | c783f47fa99b8cf4a35c565319f24c07 |
|
BLAKE2b-256 | d025fe90a7b8387f1cc8b569988ae2a51b0d5c2904782a5b270584e0359e51e5 |