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

Get string field content without return std::string copy #2091

Closed
zmwangcode opened this issue May 7, 2020 · 13 comments
Closed

Get string field content without return std::string copy #2091

zmwangcode opened this issue May 7, 2020 · 13 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@zmwangcode
Copy link

I am dealing with json with a lot of string fields, I checked the souce code , seems there is no way to get the underlying string of string field without a std::string copy?

auto content = j.get<char*>()

Something likes this will be very helpful.

@zmwangcode
Copy link
Author

I am fully aware of the fact that the returned char* may be invalidate when the json object is altered later. It's the client's responsbility to use the returned raw string properly, is ther any way to get it ?

@AraHaan
Copy link

AraHaan commented May 7, 2020

just take the std::string return and to .to_cstr() or something like that.

@zmwangcode
Copy link
Author

just take the std::string return and to .to_cstr() or something like that.

this required a std::string copy as well. I want the content without any copy..

@nlohmann
Copy link
Owner

nlohmann commented May 7, 2020

This works:

json j = "Hello";
auto s = j.get_ref<std::string&>().c_str();

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label May 7, 2020
@zmwangcode
Copy link
Author

due to compatbility reason, I need to use version 2.1.1, and seems this version does not support such operation.

how to achieve this in version 2.1.?

@nlohmann
Copy link
Owner

nlohmann commented May 7, 2020

According to the Release Notes, get_ref() was introduced in version 1.1.0.

@zmwangcode
Copy link
Author

Cool, I got build error and though it was caused by no such definition.

I checked the code, my issue was not provide const before std::string reference, because my input json j is a const object.

@nlohmann
Copy link
Owner

nlohmann commented May 7, 2020

Cool! (But you really should update to the recent version...)

@AraHaan
Copy link

AraHaan commented May 7, 2020

This is why I use submodules makes update just a git submodule foreach "git pull" away.

@nlohmann
Copy link
Owner

nlohmann commented May 7, 2020

This depends on your policies of how updates are made. But yes, this may be a convenient method.

@zmwangcode
Copy link
Author

This is why I use submodules makes update just a git submodule foreach "git pull" away.

this is not quite a safe way for large project, since it may introduce new features automatically that may break existing functionality.

I prefer to test intensively before mannually upgrade to new version.

@zmwangcode
Copy link
Author

Cool! (But you really should update to the recent version...)

Will check and upgarade. :-)

@AraHaan
Copy link

AraHaan commented May 7, 2020

@zmwangcode I mean if it did not work out you can always git reset the submodule updates away.

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

3 participants