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

Preserving order for tables #71

Closed
NikolausDemmel opened this issue Jun 25, 2019 · 3 comments
Closed

Preserving order for tables #71

NikolausDemmel opened this issue Jun 25, 2019 · 3 comments

Comments

@NikolausDemmel
Copy link

Is there a way to control the order of how elements in a table are serialized? From the readme it seems the tables are implemented as unorderd_map, so I guess that currently results in a (more or less) random order.

For example, it would be nice to optionally have the output preserve the order of the how keys are entered into the table.

For example in https://github.com/nlohmann/json this is realized by providing a template argument that specifies the type used internally to store the table, where one can specify a map data structure that preserves order, see nlohmann/json#546 (comment) and nlohmann/json#485 (comment)

@ToruNiina
Copy link
Owner

Thank you for the suggestion and information.

You are right, the current implementation does not preserve the order of the keys while serialization.

In the last few months, I was working on the next major update. Although I didn't think about the key-order problem, the next major release will include custom container support in the similar (a bit different, but essentially the same) way as you mentioned.

The main reason why I changed the definition of toml::value was to support comments in a more powerful way. While I was introducing a template argument to it, I added custom container support for providing a way to optimize the performance and memory usage in some cases. It is nice to hear that it might be helpful also for other purposes.

Now it is still a beta release and has not been merged into master yet, but you can try it. It already passes the same tests that are applied to the current version, but I plan to merge it after I try it with some of my own tools because it includes breaking changes.

@ToruNiina
Copy link
Owner

Sorry for the late reply. After v3, you can specify STL-like containers that would be used in toml::value.

const auto data = toml::parse<
    toml::preserve_comments, std::map, std::deque
    >("example.toml");

For more detail, please see here.

@NikolausDemmel
Copy link
Author

Thanks for the updates! I just check out version 3. It works great with fifo_map as well, as in:

using ordered_toml = toml::basic_value<toml::discard_comments, nlohmann::fifo_map, std::vector>;

With that, it preserves the insertion order, which is nice if you want to write back or output in the initial order from the config file.

https://github.com/nlohmann/fifo_map

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