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

Unexpected behaviour with structured binding #1404

Closed
ChristianJacobsen opened this issue Dec 23, 2018 · 3 comments
Closed

Unexpected behaviour with structured binding #1404

ChristianJacobsen opened this issue Dec 23, 2018 · 3 comments

Comments

@ChristianJacobsen
Copy link

  • What is the issue you have?
    When using range-for loop with structured binding over a simple JSON structure it puts the entire object in the value argument as if it's treated as an array.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

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

using nlohmann::json;

int main() {
    /**
     * config.json:
     *
     * {
     *     "someKey": "someValue"
     * }
     */
    json o{json::parse(std::ifstream{"./config.json"})};
    for (const auto& [key, value] : o.items()) {
        std::cout << key << ": " << value << "\n";
        // 0: {"someKey":"someValue"}

        for (const auto& [innerKey, innerValue] : value.items()) {
            std::cout << innerKey << ": " << innerValue << "\n";
            // someKey: "someValue"
        }
    }

    return 0;
}
  • What is the expected behavior?
    someKey: "someValue" in the first print.

  • And what is the actual behavior instead?
    What is in the comments below the print statements in the code above.

  • Which compiler and operating system are you using? Is it a supported compiler?
    GCC version 8.2.1 on Linux 4.19.11.

  • Did you use a released version of the library or the version from the develop branch?
    On version 3.5.0 provided by the AUR (Arch User Repository).

  • If you experience a compilation error: can you compile and run the unit tests?
    No compilation errors.

@nlohmann
Copy link
Owner

The problem are the braces in line

json o{json::parse(std::ifstream{"./config.json"})};

which yield an array.

Try:

json o = json::parse(std::ifstream{"./config.json"});

@nlohmann
Copy link
Owner

(see also: #1359)

@ChristianJacobsen
Copy link
Author

Damn you're fast! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants