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

a bug of inheritance ? #608

Closed
zhishupp opened this issue Jun 6, 2017 · 3 comments
Closed

a bug of inheritance ? #608

zhishupp opened this issue Jun 6, 2017 · 3 comments
Labels
confirmed kind: bug state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@zhishupp
Copy link

zhishupp commented Jun 6, 2017

I did a test of inheriting from class nlohmann::json, and it's a little difference if I directly use nlohmann::json.

#include "json.hpp"
using json = nlohmann::json;
class JSON : public json {};
int main(int argc, char* argv[])
{
    JSON j;
    JSON j1;
    j1["k"] = "v";
    j["o"] = j1;
    std::cout << j.dump() << std::endl;
    return 0;
}

the result: {"o":["v"]}

#include "json.hpp"
using json = nlohmann::json;
//class JSON : public json {};
int main(int argc, char* argv[])
{
    json j;
    json j1;
    j1["k"] = "v";
    j["o"] = j1;
    std::cout << j.dump() << std::endl;
    return 0;
}

the result: {"o":{"k":"v"}}

"k" is lost in inheritance condition

@nlohmann
Copy link
Owner

nlohmann commented Jun 6, 2017

It seems as if is_compatible_array_type<BasicJsonType, CompatibleArrayType>::value holds true in the inheritance case when j["o"] = j1 is called: j1 is treated as if it was an array, so static void construct(BasicJsonType& j, const CompatibleArrayType& arr) is called on the range j1.begin()/j1.end(). As j1 is a string, this range returns exactly 1 item. For this reason, the result is ["v"].

I am not sure how to fix this right now. Maybe @theodelrieu has an idea. I guess we all just forgot about the case of inheriting from basic_json.

@theodelrieu
Copy link
Contributor

Sorry for the veeery late reply, just found about this issue.
I will look into it!

@stale
Copy link

stale bot commented Oct 25, 2017

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 Oct 25, 2017
@stale stale bot closed this as completed Nov 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed kind: bug 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