Skip to main content

Read and write json files with datetime objects

Project description

Datetime JSON

Github action badge

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.7.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: datetimejson-0.1.7.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for datetimejson-0.1.7.tar.gz
Algorithm Hash digest
SHA256 59aa72eb6636b141e61b18b70b332ecfd99354535ceb2aa59e398f56a405a33d
MD5 72fa760bca583740761b55cbd7de12d7
BLAKE2b-256 44634282a5986daae2eb5f51b778a1e73fda92571c5cef0df8e5f3718478944e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for datetimejson-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b022586c83c7e76d41f54b17982dd74e8ef637cfb74eb1f7cedf6d8f4eb35d8e
MD5 df590d0223eed2105d0708e9be4f4a93
BLAKE2b-256 b47ca6df76a89e85bc60ba0e97f6075563a31dfdbd610b8b81b27e3dc2f79b14

See more details on using hashes here.

Supported by

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