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

Updation of child object isn't reflected in parent Object #968

Closed
traw opened this issue Feb 11, 2018 · 1 comment
Closed

Updation of child object isn't reflected in parent Object #968

traw opened this issue Feb 11, 2018 · 1 comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@traw
Copy link

traw commented Feb 11, 2018

I've following snippet, where I read and modify child obj attributes with at, But it doesn't get reflected in parent Obj. At the end of following code _root is written to file, But with previoius values of _child["array"].

void fun(std::vector<int> &vec) {
        std::ifstream in(file);
        _root = Json::parse(in);
        in.close();
        if (_root.find("child") != _root.end()) {
            _child = _root.at("child");
            _child["array"] = vec;
          std::ofstream writer(file);
          writer << root;    
        }
}

For example Previous values for _child["array"] were [1, 2] and vec contains [99, 98]. So at the end it should be 99, 98.

Previous Json values:

{
  "child" : {
     "array" : [1, 2]
  }
} 

Expected Json Values:


{
  "child" : {
     "array" : [99, 98]
  }
} 

Am I doing it right?

@nlohmann
Copy link
Owner

I do not know the type of _child, but if it is json &, then it should work.

This is a small example:

#include "json.hpp"
#include <iostream>

using json = nlohmann::json;

json _root;

void fun(std::vector<int> &vec) {
    if (_root.find("child") != _root.end()) {
        json& _child = _root.at("child");
        _child["array"] = vec;
        std::cout << _root << std::endl;
    }
}

int main(int argc, char** argv)
{
    _root = R"({
        "child" : {
            "array" : [1, 2]
        }
    })"_json;

    std::vector<int> vec = {98, 99};
    fun(vec);
}

Output: {"child":{"array":[98,99]}}

@traw traw closed this as completed Feb 11, 2018
@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Feb 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants