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

Type Conversion #779

Closed
ccge opened this issue Oct 12, 2017 · 11 comments
Closed

Type Conversion #779

ccge opened this issue Oct 12, 2017 · 11 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ccge
Copy link

ccge commented Oct 12, 2017

Is there a way to convert a string value to a number value or vice versa?

For example:

{
    "number": "9"
}

I figured this word work, but it doesn't:
j.at( "number" ).get<int>();

Do I just have to grab it as a string and convert myself, or is there a better way?

@nlohmann
Copy link
Owner

The library does not provide such conversions.

You may need something like

#include "json.hpp"

using json = nlohmann::json;

int main()
{
    json j = {{"number", "9"}};
    int i = std::stoi(j["number"].get_ref<std::string&>());
}

@ccge
Copy link
Author

ccge commented Oct 12, 2017

Thank you for the quick response. Great library.

@ccge
Copy link
Author

ccge commented Oct 12, 2017

I have one more question. What is the difference between?:

j["number"].get_ref<std::string&>()
j["number"]
j.at("number")

They all seem to do the same thing and return a reference.

@gregmarr
Copy link
Contributor

j.at("number") and j["number"] will both return a reference to the json object corresponding to the key "number" if it exists. If not, then at will throw, as will [] on a const j object. For a non-const j, it will create a new key with a null value.

Then .get_ref<std::string&>() returns a reference to the std::string value in the returned object, assuming that is its type.

@ccge
Copy link
Author

ccge commented Oct 12, 2017

So, what will tmp be, string& or string?:
auto tmp = j.at("number");

@gregmarr
Copy link
Contributor

This is a copy, auto never deduces to a reference. If you want a reference, use

auto &tmp = j.at("number");

@ccge
Copy link
Author

ccge commented Oct 12, 2017

So at and [] can both return a reference or a copy of the value. Why did you say it returns the reference to the key?

@gregmarr
Copy link
Contributor

They always return a reference, which is then copied if you don't use &, because auto deduces json instead of json & or const json &. When you add the &, it deduces json & or const json & depending on whether or not j is const.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 13, 2017
@nlohmann
Copy link
Owner

Can I close this issue?

@nlohmann
Copy link
Owner

💤 I closed this issue due to inactivity. Please feel free to add a comment and I shall reopen it.

@yalov
Copy link

yalov commented Jul 5, 2023

@nlohmann
so, if I got nlohmann::json json, that represent MyStruct, but someone put "value": "5", instead of "value": 5,
then auto myStruct = json.get<MyStruct>() will throw exception?

what would be the proper way to solve this,
implement to_json and from_json manually, instead of one of NLOHMANN_DEFINE_TYPE...() ?
how safe function would looks like (that could work with both int and string) instead of
type_to.val = json_from.value("val", default_obj.val);

Or there is something better, that leaves autogenerating with NLOHMANN_DEFINE_TYPE...() ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants