I am writing a script to convert a series of YAML files into one piece of JSON. I have a YAML file:
---
AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation ECS Sample
Parameters:
- SolrCloudInstanceType:
Type: String
Description: Solr Cloud EC2 Instance Type
Default: m3.2xlarge
Resources:
- ContainerInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: m3.xlarge
I load it like this
import yaml
with open('base.yml', 'rb') as f:
result = yaml.safe_load(f)
I wonder if I do a check AWSTemplateFormatVersion, I get a Python object datetime.date. This causes the JSON output to fail:
>>> json.dump(result, sys.stdout, sort_keys=True, indent=4)
{
"AWSTemplateFormatVersion": Traceback (most recent call last):
File "./c12n-assemble", line 42, in <module>
__main__()
File "./c12n-assemble", line 25, in __main__
assembler.assemble()
File "./c12n-assemble", line 39, in assemble
json.dump(self.__result, self.__output_file, sort_keys=True, indent=4, separators=(',', ': '))
File "/usr/lib/python2.7/json/__init__.py", line 189, in dump
for chunk in iterable:
File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
for chunk in _iterencode_dict(o, _current_indent_level):
File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
for chunk in chunks:
File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
o = _default(o)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.date(2010, 9, 9) is not JSON serializable
Is there a way to make the YAML parser not be smart about what it considers date or date + time and just parses the string?