-
Notifications
You must be signed in to change notification settings - Fork 163
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
The toml table order is changed #133
Comments
Hi, It seems that the problem is that you explicitly specified the type of auto tomlData = toml::parse<toml::preserve_comments, std::map>(file);
// ^
std::ofstream ofs;
ofs.open(file, std::ios::out | std::ios::binary);
ofs << tomlData << std::endl; If you want to preserve the insertion order, you can use a |
Thank you, but I tried both the way by declaring the "tomlData" with "auto" or "toml::basic_value<toml::preserve_comments, std::map>", the file still cannot keep the original order, it seems ordered by the ascending order of charactor. (ABCDE...) |
Sorry for the confusing wording. The problem of using The reason why it is sorted is that And there are several implementations of a map that keeps the insertion order in github, like https://github.com/Tessil/ordered-map . To keep the original order, you can use those libraries instead of So, I mean, you can try the following. #include "ordered_map.hpp"
#include "toml.hpp"
#include <iostream>
int main()
{
auto tomlData = toml::parse<toml::preserve_comments, tsl::ordered_map>("sample.toml");
std::cout << tomlData << std::endl;
} |
Thank you very much! The order now is saved as expected. |
I'm glad the problem is solved. But I'm not sure why you need to change the definition of
Actually I have tried the following code without any modification. #include "ordered-map/include/tsl/ordered_map.h"
#include "toml.hpp"
#include <iostream>
int main()
{
auto tomlData = toml::parse<
toml::preserve_comments, tsl::ordered_map>("issue133.toml");
std::cout << tomlData << std::endl;
return 0;
} [table3]
[table3.3]
key2 = "foo"
key1 = "bar"
[table3.2]
[table3.1]
[table2]
[table2.2]
[table2.3]
[table2.1]
[table1]
[table1.3]
[table1.1]
[table1.2] And the order seems to be preserved. I want to make it easier (and because of some curiosity), so if it is not too much trouble, I would like to see the code that does not work. |
Sorry for late reply, as the project is too large, so I'll try to reproduce it with a new simple project and later share with you. |
Ther verison of toml11 I used is the latest code I guess it's v3.6.0 |
Hi,
Thank you for your developing of this library.
When I use this library to save some data to a toml file, I found if the toml file have many tables like below,
the order of the table will be changed and has no rule for every saving.
Is there a way to save the data with the original order of these tables?
I use the following code to serialize the data to the file:
toml::value tomlData = toml::parse<toml::preserve_comments, std::map>(file);
std::ofstream ofs;
ofs.open(file, std::ios::out | std::ios::binary);
ofs << tomlData << std::endl;
[Title]
Title = "TOML Configration File"
[Owner]
Author = "XXX"
[Table1]
Key1 = "234"
[Table2]
Key2 = 233
The text was updated successfully, but these errors were encountered: