Skip to content

a bug of inheritance ?  #608

Closed
Closed

Description

@zhishupp

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

Activity

  1. nlohmann commented on Jun 6, 2017

    @nlohmann
    Owner

    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.

  2. theodelrieu commented on Aug 14, 2017

    @theodelrieu
    Contributor

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

  3. stale commented on Oct 25, 2017

    @stale

    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.

  4. added
    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated
    on Oct 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedkind: bugstate: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions