Skip to main content

Read and write json files with datetime objects

Project description

Datetime JSON

Github action badge PyPI version PyPI - License

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 objects
    • dump/dumps - Serialize python object containting datetime objects to JSON
  • two classes derived respectively from json.JSONEncoder and json.JSONDecoder:

    • DateTimeEncoder - Serialize a datetime object to JSON
    • DateTimeDecoder - 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.8.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datetimejson-0.1.8-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file datetimejson-0.1.8.tar.gz.

File metadata

  • Download URL: datetimejson-0.1.8.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for datetimejson-0.1.8.tar.gz
Algorithm Hash digest
SHA256 aeb6439e41459ad0692d246ecece747324a6b0afd63fa2e50c3fdebad64a2f1d
MD5 e156b6372f371a98be14066ca41aa7af
BLAKE2b-256 0f807f8821d0bd3b340c5aa2ae276928bbbf5796c4baf12b48f0cd150173d06c

See more details on using hashes here.

File details

Details for the file datetimejson-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: datetimejson-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for datetimejson-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 01f36d3893e603357cd339169d520dde91654938c1bd21400e65e003dcfc37df
MD5 29ef6f81b5b29d1f46cad2d15289a3d1
BLAKE2b-256 4bc2f7f71e3a145507d752a4fd8799b790e62a02da4ef5efd91413052d5f2b93

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page