-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Object properties should not be re-sorted alphabetically #2179
Comments
This behavior is described in the readme including ways to circumvent it. |
Thanks, I had missed the Order of object keys section in README. |
I just want to highlight that some
This produces exactly the same as the default:
|
The README does not mention using |
Sure though the commented output under To preserve insertion order in my use case, I made a minimal solution based on
Again, thanks for this great JSON library! |
Your |
Of course 👍 |
It works well in my tests.
|
It was missing |
I am currently trying to put this all together to add this as std::size_t erase(const key_type& key)
{
std::size_t result = 0;
for (auto it = this->begin(); it != this->end();)
{
if (it->first == key)
{
++result;
it = Container::erase(it);
}
else
{
++it;
}
}
return result;
} which fails due to an error in
Any ideas? Here is the current state: 4fd0d02 |
Yup, I had that working somewhere... |
The problem is about moving the |
Actually there seems to be no cleaner way than to make those pairs have a non-const Key. |
I could not make it to work. Can you make a concrete example how to remove the const? |
This is my latest working version, with erase:
https://repl.it/repls/EducatedImpeccableCalculators#main.cpp |
This does not work for me:
|
I see your code running - maybe it's a C++17 vs. C++11 issue? I already had to change the return type of |
I see. Definitely C++17 vs C++11 may have something to do... |
Adapted to C++11 here:
|
This code does not work with my setup (Clang 10):
This error goes away if I switch the type back to |
That may be cause the allocator passed by basic_json is not compatible. |
I did not get it to work, I'm sorry. It would be great to see a PR. |
Will do that. I am away from computer today just trying to help from my phone 🙂 |
I forked, but how should I build and test?
|
Just open a merge request, then the CI is run with a lot of different compilers. |
I see it fails only on MacOS build, with "Apple clang version 11.0.3 (clang-1103.0.32.62)", with the assertion you mentioned above. |
The problem is in the definition of
And a |
I took the default template arguments from template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map; I'm afraid changing this would break the API. Any idea how to fix the "vector of pairs" approach? Maybe using two vectors? |
I see 3 ways forward:
Probably the cleanest way is by wrapping std::map:
then ObjectType = MapWrapper, and
Only I would not want to go into mayor refactoring 🙂 so...
|
Thanks for taking this in! |
great feature @gatopeich |
@gatopeich that is a really good feature |
@nlohmann I think this should have been named
Similarly |
This is not true. In
See https://json.nlohmann.me/features/object_order/ on the rationale. |
Usually JSON documents have a meaningful ordering of properties, and rarely it is alphabetical.
With this library all JSON data gets sorted alphabetically, which is undesirable.
I guess it must be due to use of
std::map
.Please describe the steps to reproduce the issue.
What is the expected behavior?
And what is the actual behavior instead?
Which compiler and operating system are you using?
Which version of the library did you use?
The text was updated successfully, but these errors were encountered: