Jezza/toml

Feature - API to write toml

Opened this issue · 2 comments

A248 commented

Would it be possible to have an API to write toml values back to a stream? I'm imagining a method such as Toml.writeTo(Writer) throws IOException

I noticed from the source code that the toString() of TomlTable and TomlArray looks like it writes toml, but of course, the output of toString() is not an API guarantee.

Also, on another note, would it be possible to add comments to toml values while writing them?

Jezza commented

I've actually thought about this for some time.

There's a couple of issues that I'd need to resolve before I commit to a serialization API.

I don't differentiate between the different types of tables beyond the parser.
I'd either need to provide some way for the user to specify which "type of table" is desired.

I don't preserve key structure.
I mean, I obviously preserve the structure, but rather, I don't preserve the "pattern" that it used?
As in, someone could write:

a.b.c = "asd"

but without some token preserving it could be turned into:

[a.b]
c = "asd"

or something else bizarre.

The serialiser/formatter/writer would act something like a normaliser,
which isn't the end of the world, but that leads me onto the next issue.

Comments aren't preserved at all.
The parser isn't even aware of them, and by extension, neither are the data structures (TomlTable and TomlArray).

Adding support for comments would be a bit tricky, as I wouldn't want to pollute the data structures with Comment objects.
At least, I would like to avoid it.

So, in a nutshell, it would be possible, and as you rightfully noted, the toString() is already very similar.
But users would need to be made aware that, while it would be internally consistent, the style itself would be undefined.

And comments are a whole other ball game.

A248 commented

I see the problem. For my use-case, I only need to write valid toml, but not necessarily round-trip reading and writing with exact text preserved.

I don't imagine that the style of written toml being undefined would be a huge issue for API users. It's certainly better than not being able to write toml at all. Maybe it would make sense to implement toml writing, with an undefined style for now, and if desired at a later time, the exact style of toml could be preserved.

For comments, I'll go ahead and open a separate issue. I'm sure those would be more complicated.