1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-07-17 01:32:37 +02:00

fix for datetime objects in yaml files

This commit is contained in:
Kelly Brazil
2022-04-19 14:43:47 -04:00
parent c7173ecd89
commit 6c0f0cddfe
2 changed files with 14 additions and 1 deletions

View File

@ -85,7 +85,7 @@ from jc.exceptions import LibraryNotInstalled
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.6'
version = '1.7'
description = 'YAML file parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
@ -147,6 +147,11 @@ def parse(data, raw=False, quiet=False):
yaml = YAML(typ='safe')
# modify the timestamp constructor to output datetime objects as
# strings since JSON does not support datetime objects
yaml.constructor.yaml_constructors['tag:yaml.org,2002:timestamp'] = \
yaml.constructor.yaml_constructors['tag:yaml.org,2002:str']
for document in yaml.load_all(data):
raw_output.append(document)

View File

@ -41,6 +41,14 @@ class MyTests(unittest.TestCase):
"""
self.assertEqual(jc.parsers.yaml.parse(self.generic_yaml_istio_sidecar, quiet=True), self.generic_yaml_istio_sidecar_json)
def test_yaml_datetime(self):
"""
Test yaml file with datetime object (should convert to a string)
"""
data = 'deploymentTime: 2022-04-18T11:12:47'
expected = [{"deploymentTime":"2022-04-18T11:12:47"}]
self.assertEqual(jc.parsers.yaml.parse(data, quiet=True), expected)
if __name__ == '__main__':
unittest.main()