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

How can I save json object in file in order? #1717

Closed
Linjackffy opened this issue Aug 21, 2019 · 3 comments · Fixed by #2258
Closed

How can I save json object in file in order? #1717

Linjackffy opened this issue Aug 21, 2019 · 3 comments · Fixed by #2258
Assignees
Labels
kind: question release item: ✨ new feature solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@Linjackffy
Copy link

I create a json object and save it in file:

    json root = {
        {
            "information", {
                {"model", 1},
                {"dof", 6},
                {"aux dof", 0}
            }
        },
        {
            "cartisian limit", {
                {
                    "translation", {
                        {"velocity", 1},
                        {"acceleration", 1},
                        {"deceleration", 1},
                        {"jerk", 1}
                    }
                },
                {
                    "rotation", {
                        {"velocity", 1},
                        {"acceleration", 1},
                        {"deceleration", 1},
                        {"jerk", 1}
                    }
                }
            }
        }
    };
    std::ofstream ob("robot_params.json");
    ob << std::setw(4) << root << std::endl;

And the file robot_params.json looks like this:

{
    "cartisian limit": {
        "rotation": {
            "acceleration": 1,
            "deceleration": 1,
            "jerk": 1,
            "velocity": 1
        },
        "translation": {
            "acceleration": 1,
            "deceleration": 1,
            "jerk": 1,
            "velocity": 1
        }
    },
    "information": {
        "aux dof": 0,
        "dof": 6,
        "model": 1
    }
}

But what I need is saving the json object in order like this:

{
    "information": {
        "model": 1,
        "dof": 6,
        "aux dof": 0
    },
    "cartisian limit": {
        "translation": {
            "velocity": 1,
            "acceleration": 1,
            "deceleration": 1,
            "jerk": 1
        },
        "rotation": {
            "velocity": 1,
            "acceleration": 1,
            "deceleration": 1,
            "jerk": 1
        }
    }
}

Please let me know is it feasible to do so?

@nlohmann
Copy link
Owner

This seems related to https://github.com/nlohmann/json#order-of-object-keys. The library does not preserve the order of object keys.

@Linjackffy
Copy link
Author

It solved my problem!Thanks for you reply.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 23, 2019
@nlohmann nlohmann self-assigned this Jul 11, 2020
@nlohmann nlohmann added this to the Release 3.8.1 milestone Jul 11, 2020
@nlohmann nlohmann reopened this Jul 11, 2020
@nlohmann
Copy link
Owner

To be fixed with #2258.

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

Successfully merging a pull request may close this issue.

2 participants