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:
@ -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)
|
||||
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user