Skip to content

Input ordered_json doesn't work #3023

Description

@supsm

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: bugsolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions