ruyaml icon indicating copy to clipboard operation
ruyaml copied to clipboard

feature: Allow dumping to a string without using streams

Open lyz-code opened this issue 5 years ago • 1 comments

I want to be able to use .dump() to save the output as a string for later processing.

Right now, you need to work with streams. Where the simplest code is:

# Configure YAML formatter
yaml = YAML()
string_stream = StringIO()
yaml.dump({'products': ['item 1', 'item 2']}, string_stream)
source_code = string_stream.getvalue()
string_stream.close()

It would be really nice to be able to do:

yaml = YAML()
source_code = yaml.dump({'products': ['item 1', 'item 2']}, string_stream)

Thanks

lyz-code avatar Nov 24 '20 20:11 lyz-code

Personally I would expect this to be named consistently with the std-lib JSON library, and instead be called dumps (and loads for the load equivalent).

pelson avatar Aug 30 '22 08:08 pelson