Support datetime serialization in json encoder. Fixes #34312 (#34516)

This commit is contained in:
Matt Martz 2018-01-11 09:10:14 -06:00 committed by GitHub
parent cf86c6aaf6
commit bfb5264138
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,7 +35,7 @@ import uuid
import yaml import yaml
from collections import MutableMapping, MutableSequence from collections import MutableMapping, MutableSequence
from datetime import datetime import datetime
from functools import partial from functools import partial
from random import Random, SystemRandom, shuffle from random import Random, SystemRandom, shuffle
@ -69,6 +69,8 @@ class AnsibleJSONEncoder(json.JSONEncoder):
def default(self, o): def default(self, o):
if isinstance(o, HostVars): if isinstance(o, HostVars):
return dict(o) return dict(o)
elif isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
else: else:
return super(AnsibleJSONEncoder, self).default(o) return super(AnsibleJSONEncoder, self).default(o)
@ -126,7 +128,7 @@ def to_bool(a):
def to_datetime(string, format="%Y-%m-%d %H:%M:%S"): def to_datetime(string, format="%Y-%m-%d %H:%M:%S"):
return datetime.strptime(string, format) return datetime.datetime.strptime(string, format)
def strftime(string_format, second=None): def strftime(string_format, second=None):