Skip to content

Type Conversion #779

Description

@ccge

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?

Activity

  1. nlohmann commented on Oct 12, 2017

    @nlohmann
    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&>());
    }
  2. ccge commented on Oct 12, 2017

    @ccge
    Author

    Thank you for the quick response. Great library.

  3. ccge commented on Oct 12, 2017

    @ccge
    Author

    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.

  4. gregmarr commented on Oct 12, 2017

    @gregmarr
    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.

  5. ccge commented on Oct 12, 2017

    @ccge
    Author

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

  6. gregmarr commented on Oct 12, 2017

    @gregmarr
    Contributor

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

    auto &tmp = j.at("number");
    
  7. ccge commented on Oct 12, 2017

    @ccge
    Author

    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?

  8. gregmarr commented on Oct 12, 2017

    @gregmarr
    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.

  9. nlohmann commented on Oct 17, 2017

    @nlohmann
    Owner

    Can I close this issue?

  10. nlohmann commented on Oct 21, 2017

    @nlohmann
    Owner

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

  11. yalov commented on Jul 5, 2023

    @yalov

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions