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 original order #111

Closed
KangJH opened this issue Mar 31, 2020 · 3 comments
Closed

Preserving original order #111

KangJH opened this issue Mar 31, 2020 · 3 comments

Comments

@KangJH
Copy link

KangJH commented Mar 31, 2020

Hi
Is there a way to preserve original order of toml file?
This question is similar with #71.
So, I changed my code to use toml::parse<toml::preserve_comments, std::map, std::deque>.
But it still does not preserve original order.
I also tried nlohmann::fifo_map, but it does not seems to support table array and nested table quite well.
Can you recommend a way to preserve original order of toml file?

@ToruNiina
Copy link
Owner

The serializer dumps the content in the same order as the container has. Since std::map::iterator iterates over the elements from the smallest to the largest, according to the given comparator (by default std::less), the resulting toml file will be sorted by its key. So using std::map does not preserve the order of definition.
I have no idea why nlohmann/fifo_map fails, but you can try another library, for example, https://github.com/Tessil/ordered-map.
For the arrays, std::vector already preserves the order, so you don't need to change it to std::deque.

@KangJH
Copy link
Author

KangJH commented Apr 1, 2020

Thanks, tsl::ordered_map works perfectly!
FYI, I've changed 2 parts.

  1. toml::parse<toml::preserve_comments, tsl::ordered_map, std::vector>(filename)
  2. using value = basic_value<preserve_comments, tsl::ordered_map, std::vector>; in value.hpp

When I changed parse part(no.1) only, the nested table was not ordered well.
But, when I applied parsing part and toml::value definition both, it's working well.

Thank you very much
Have a nice day.

@KangJH KangJH closed this as completed Apr 1, 2020
@ToruNiina
Copy link
Owner

Great! And thank you for the detailed information.

The default toml::table is defined as toml::value::table_type. Without modifying the defualt definition of toml::value, after converting a value into toml::table, the elements will be sorted, unexpectedly.

I know it is a confusing behavior. But, AFAIK, there is no way to automatically change the toml::table that is an alias without any template argument depending on the customized toml::value.

Note that, you can obtain the actual table_type used in the toml::basic_value<...> via typename basic_value<...>::table_type.

auto file = toml::parse<toml::preserve_comment, tsl::ordered_map>(filename);
auto tables = toml::find<
        std::vector<typename decltype(file)::table_type>
    >(file, "array_of_table");

But yes, this code looks strange.

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