Skip to main content

Allows for encoding and decoding of datetime values contained in JSON streams

Project description

jsondatetime

image

jsondatetime allows for proper decoding of datetime values contained in JSON streams.

The problem

The JSON standard RFC 4627 does not support datetime types. These are usually represented as strings and Python decoders end up decoding them as such. Consider the following example:

import simplejson as json

>>> test = '{"name": "John Doe", "born": "Thu, 1 Mar 2012 10:00:49 UTC"}'
>>> json.loads(test)
{'born': u'Thu, 1 Mar 2012 10:00:49 UTC', 'name': u'John Doe'}

As you can see, in the resulting dictionary born is still a string.

The solution

jsondatetime is a very simple wrapper around Python simplejson loads and dumps methods. It decodes datetime values contained in JSON strings:

import jsondatetime as json

>>> test = '{"name": "John Doe", "born": "Thu, 1 Mar 2012 10:00:49 UTC"}'
>>> j = json.loads(test); j
{'name': 'John Doe', 'born': datetime.datetime(2012, 3, 1, 10, 0 ,49, tzinfo=tzutc())}
>>> dumps(j)
"{'name': 'John Doe', 'born': '2012-03-01T10:00:49+00:00'}"

Strings are parsed using dateutil.parser.parse which is fairly flexible for common datetime formats.

Custom parsing

Being just a wrapper around the loads method, you can still use all the standard loads arguments, object_hook included. This means that you can still perform custom parsing of your inbound JSON stream.

Installation

pip install jsondatetime

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

jsondatetime-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file jsondatetime-0.1.1.tar.gz.

File metadata

  • Download URL: jsondatetime-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1

File hashes

Hashes for jsondatetime-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e7ce907f548a175bc3951963d5608b016283e5880abcb09e5b7ca8109423f2bf
MD5 dd335cdd7a60908e82fecefe669d0409
BLAKE2b-256 038b6fd91e463696cc7b0a6ba3216f23878028124d527a5f0102a07f9889cf6e

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