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

unflatten vs objects with number-ish keys #1575

Closed
hoehrmann opened this issue Apr 21, 2019 · 5 comments
Closed

unflatten vs objects with number-ish keys #1575

hoehrmann opened this issue Apr 21, 2019 · 5 comments
Labels
documentation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@hoehrmann
Copy link

hoehrmann commented Apr 21, 2019

The documentation for flatten and unflatten should point out that JSON pointers as per RFC 6901 do not distinguish between arrays, and objects with number-ish strings as keys. There should also be a note about how unflatten decides whether to make an array or an object (e.g., it might just look at the first pointer, it might look at all of them, what happens for path elements like 1.0, 010, or 1E1).

json x = json::object({
  {"0", json::object({{"1", "2"}})},
  {"9", json::object({{"3", "4"}})},
});

is actually

{"0":{"1":"2"},"9":{"3":"4"}}

flattens to

{"/0/1":"2","/9/3":"4"}

and that unflattens to

[{"1":"2"},null,null,null,null,null,null,null,null,{"3":"4"}]
@nlohmann
Copy link
Owner

Good point! This should go into the documentation.

@stale
Copy link

stale bot commented May 21, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 21, 2019
@stale stale bot closed this as completed May 28, 2019
@levinandrew
Copy link

I think it would be helpful if there was a way to override the unflatten() behavior so that it didn't treat "0", "1", and "2" as list indices. In my application I actually want to use incrementing values from 0 as keys, and the automatic conversion to lists here makes unflatten() unusable for me

    // create JSON value
    json j_flattened =
    {
        {"/answer/everything", 42},
        {"/list/0", 1},
        {"/list/1", 0},
        {"/list/2", 2},
        {"/name", "Niels"},
    };

    // call unflatten()
    std::cout << std::setw(4) << j_flattened.unflatten() << '\n';

output:

{
    "answer": {
        "everything": 42
    },
    "list": [
        1,
        0,
        2
    ],
    "name": "Niels"
}

@nlohmann nlohmann removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jul 31, 2019
@nlohmann
Copy link
Owner

As the flatten and in particular the unflatten function are not really standardized, one could think about adding a parameter to not interpret numbers as strings.

@stale
Copy link

stale bot commented May 13, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label May 13, 2020
@stale stale bot closed this as completed May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants