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

compile error in from_json converting to container with std::pair #1299

Closed
brainiac opened this issue Oct 15, 2018 · 3 comments
Closed

compile error in from_json converting to container with std::pair #1299

brainiac opened this issue Oct 15, 2018 · 3 comments
Assignees
Labels
release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@brainiac
Copy link

It looks like the change to fix #1292 (11fecc2) causes a regression when converting to a container std::pair with a non-trivial type. I didn't see a unit test that covers this case.

Example code that show the failure:
https://wandbox.org/permlink/BVgyd1o4MW60bqvS

#include "nlohmann/json.hpp"

#include <iostream>
#include <map>
#include <string>

using json = nlohmann::json;

struct Data
{
    std::string a;
    std::string b;
};

void from_json(const json& j, Data& data)
{
    j["a"].get_to(data.a);
    j["b"].get_to(data.b);
}

int main()
{
    // create a JSON object
    json j =
    {
        {"1", {
            {"a", "testa_1"},
            {"b", "testa_1"}
        }},
        {"2", {
            {"a", "testa_2"},
            {"b", "testb_2"}
        }},
        {"3", {
            {"a", "testa_3"},
            {"b", "testb_3"}
        }},
    };
    
    std::map<std::string, Data> data;
    j.get_to(data);
    
    for (const auto& p : data)
    {
        std::cout << p.first << " -> " << p.second.a << ", " << p.second.b << std::endl;
    }
}
@brainiac brainiac changed the title compile error in from_json converting to std::pair compile error in from_json converting to container with std::pair Oct 15, 2018
@theodelrieu
Copy link
Contributor

Thanks for the report.

Seems like the std::map overload is not chosen, thus it falls back trying to construct a std::pair<**const** std::string, std::string>...

I'll fix it tomorrow.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 16, 2018
@nlohmann
Copy link
Owner

@brainiac Could you please check the fix in #1301?

@brainiac
Copy link
Author

brainiac commented Oct 16, 2018

Hi @nlohmann and @theodelrieu, thanks for looking into this. It looks like with the fix in #1301 it is working now.

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

No branches or pull requests

3 participants