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

Use reference or pointer as Object value #841

Closed
go2sh opened this issue Nov 21, 2017 · 7 comments
Closed

Use reference or pointer as Object value #841

go2sh opened this issue Nov 21, 2017 · 7 comments

Comments

@go2sh
Copy link

go2sh commented Nov 21, 2017

Hi there,

I have a small question. Is it possible to set the value of an object element to a reference or point without copying it?
What I like to achieve is the following

json data, result, response;
bool hasError = calcResult(data, result);
if (hasError) {
response["error"] = result; // no copy
} else {
response["result"] = result; // nocopy
}

Thanks for your help. :)

@nlohmann
Copy link
Owner

No, this is currently not possible. The only thing that could avoid copies is to move the data to the JSON value.

@go2sh
Copy link
Author

go2sh commented Nov 22, 2017

@nlohmann just for fun. Would it be hard to change the behavior? I guess so...

@itviewer
Copy link

I use this method to avoid copying

json data;
json response = {{"result",{}}};
json &result = response["result"];

bool hasError = calcResult(data, result);
//std::cout << response ;

@nlohmann
Copy link
Owner

Of course, you can use references to JSON values where possible - you just can't store them in a JSON value itself.

@nlohmann
Copy link
Owner

Can I close this issue?

@go2sh
Copy link
Author

go2sh commented Nov 27, 2017

Yes, I found some ways around it. SharedPtr and Value References. Thanks.

@go2sh go2sh closed this as completed Nov 27, 2017
@CraigHutchinson
Copy link

CraigHutchinson commented Oct 4, 2019

+1 For supporting references to allow:
json datas = { "vals", json::array() };
… elsewhere ..
json document = { {"meta", "something"}, {"content", datas} );
… next..
datas["vals"] = {1,2,3,4,5}
… so …
document.dump() == { {"meta", "something"}, {"content", [1,2,3,4,5] } );

This could be with the use of a new syntax of some manner: `json::by_ref(datas)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants