Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - API to write toml #1

Open
A248 opened this issue Mar 8, 2021 · 2 comments
Open

Feature - API to write toml #1

A248 opened this issue Mar 8, 2021 · 2 comments

Comments

@A248
Copy link

A248 commented Mar 8, 2021

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
Copy link
Owner

Jezza commented Mar 14, 2021

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
Copy link
Author

A248 commented Mar 21, 2021

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.

@A248 A248 changed the title Feature - API to write toml (and with comments) Feature - API to write toml Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants