json: Object properties should not be re-sorted alphabetically

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.

#include <iostream>
#include "json.hpp"

using Json = nlohmann::json;

int main() {
  std::cout << ">>> " << Json{{"C",1},{"B",2},{"A",3}}.dump() << std::endl;
}

What is the expected behavior?

>>> {"C":1, "B":2, "A":3}

And what is the actual behavior instead?

{“A”:3,“B”:2,“C”:1}

Which compiler and operating system are you using?

  • Compiler: clang-7 and gcc-7
  • Operating system: Ubuntu 18

Which version of the library did you use?

  • [ x ] latest release version 3.7.3

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 35 (32 by maintainers)

Most upvoted comments

@gatopeich that is a really good feature

Hey @nlohmann my pull request #2206 is ready and waiting…

Thanks for taking this in!

Will do that. I am away from computer today just trying to help from my phone 🙂

This behavior is described in the readme including ways to circumvent it.