During the write, if we just dump object data to files, we can’t really control how YAML is presented. It would not matter when we deserialize it back programmatically, but it would matter to a human, or for Git diff purposes.
The idea is to read objects from YAML the normal way (yaml.load()), but write YAML using a template string. (After the write, we can load back YAML as a safety check—if it throws, then it’s probably indentation somewhere in the template string.) Since we are not dealing with arbitrary objects, we know the properties of each type, this is viable.
While at that we can also format non-string keys the way we want (e.g., for readability we don’t want the default yaml.dump() behavior of including full timestamps with timezones on things like issue publication dates).
For example, here’s what happens if we update publication date on an already existing issue.
Dates are output as full timestamps with timezone
Formatting is changed (it got rid of whitespace, inline object syntax, multiline string syntax)
Interestingly, the order of keys doesn’t change (but we shouldn’t rely on this!)
That boolean yes are converted to string 'yes' is the issue at the loading stage, so we can ignore that
During the write, if we just dump object data to files, we can’t really control how YAML is presented. It would not matter when we deserialize it back programmatically, but it would matter to a human, or for Git diff purposes.
The idea is to read objects from YAML the normal way (
yaml.load()
), but write YAML using a template string. (After the write, we can load back YAML as a safety check—if it throws, then it’s probably indentation somewhere in the template string.) Since we are not dealing with arbitrary objects, we know the properties of each type, this is viable.While at that we can also format non-string keys the way we want (e.g., for readability we don’t want the default
yaml.dump()
behavior of including full timestamps with timezones on things like issue publication dates).For example, here’s what happens if we update publication date on an already existing issue.
yes
are converted to string'yes'
is the issue at the loading stage, so we can ignore thatPreviously existing (human-written) YAML loaded:
Having altered the loaded data structure for above YAML (updating publication/cutoff date), the new YAML dumped: