Skip to content
Discussion options

You must be logged in to vote

push_back(myStruct) does not merge object fields. Once you've done testJson["A"] = "A", the json value is an object, and pushing a plain value into an object is not the operation you want; it throws a type_error. += fails for the same reason.

What you want is a mergeupdate() does exactly this:

testJson["A"] = "A";
testJson["B"] = "B";

TestStruct myStruct;
testJson.update(nlohmann::json(myStruct));

Result:

{
    "A": "A",
    "B": "B",
    "field1": 5,
    "field2": 10,
    "someStr": "Abcd",
    "myArray": [0, 0, 0, 0, 0]
}

update(const json&) constructs a temporary json from myStruct via the to_json produced by your NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE macro, then folds its keys into t…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by ventsyv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants