I need to parse booleans in PYYAML in a custom way and convert them into strings. For that purpose I tried to write a 'constructor' and registered it with yaml, but it didn't do anything. Booleans got parsed as normal. Is it a problem with the tag or do I have to handle this problem in a different way?
def bool_constructor(loader, node):
value = loader.construct_yaml_bool(node)
if value == False:
return '$false'
else:
return '$true'
enter code here`yaml.add_constructor('!!bool', bool_constructor)
I would appreciate your help, thank you.
After some try and error I found the answer myself. it seems, that I used the wrong tag. The correct python code looks like this:
def bool_constructor(self, node):
value = self.construct_yaml_bool(node)
if value == False:
return '$false'
else:
return '$true'
yaml.Loader.add_constructor(u'tag:yaml.org,2002:bool', bool_constructor)
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:bool', bool_constructor)