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

Input ordered_json doesn't work #3023

Closed
1 of 5 tasks
supsm opened this issue Sep 18, 2021 · 3 comments
Closed
1 of 5 tasks

Input ordered_json doesn't work #3023

supsm opened this issue Sep 18, 2021 · 3 comments
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@supsm
Copy link

supsm commented Sep 18, 2021

What is the issue you have?

When inputting/parsing an ordered_json, it does not keep the order found in the file. This is due to the fact that the parser functions use basic_json instead of ordered_json

Please describe the steps to reproduce the issue.

Create an ordered_json
Input a json with its keys not in alphabetical order
Output the ordered_json
The order will not be preserved

  1. Create an ordered_json
  2. Input a json with its keys not in alphabetical order
  3. Output the ordered_json
  4. The order will not be preserved

Can you provide a small but working code example?

#include <fstream>
#include "nlohmann/json.hpp"

int main()
{
    nlohmann::ordered_json j;
    std::ifstream fin("json.json");
    fin >> j;
    fin.close();
    std::ofstream fout("json.json");
    fout << j << std::endl;
    fout.close()
}

json.json:

{
    "b": "b",
    "c": "c",
    "a": "a",
}

What is the expected behavior?

When the json is outputted, it should be exactly the same

And what is the actual behavior instead?

The order is changed to "a", "b", "c"

Which compiler and operating system are you using?

  • Compiler: MSVC 19.29.30037 (also tried with clang++ 12.0.0)
  • Operating system: Windows 11 22458.1000

Which version of the library did you use?

  • latest release version 3.10.2
  • other release - please state the version: 3.9.1
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no - please copy/paste the error message below
@supsm
Copy link
Author

supsm commented Sep 18, 2021

Sorry I'm an idiot

@supsm supsm closed this as completed Sep 18, 2021
@sentiment-bot
Copy link

sentiment-bot bot commented Sep 18, 2021

Please be sure to review the code of conduct and be respectful of other users. cc/ @nlohmann

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 20, 2021
@nlohmann
Copy link
Owner

For reference: if you want to parse to ordered_json, then you need to call ordered_json::parse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants