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

Convert strings to numbers #341

Closed
oysteinmyrmo opened this issue Oct 22, 2016 · 1 comment
Closed

Convert strings to numbers #341

oysteinmyrmo opened this issue Oct 22, 2016 · 1 comment
Labels
kind: enhancement/improvement solution: invalid the issue is not related to the library

Comments

@oysteinmyrmo
Copy link

oysteinmyrmo commented Oct 22, 2016

I am using the json library to parse a text file containing json data to insert into an SQLite database. Parsing the file works fine, but everything is stored as strings because the data originates from a Wordpress database on a Wordpress server which gives all the contents as strings because PHP. I can of course read all data as strings and then convert the data to numbers myself (data types are known), but I just wanted to check if there is some built-in functionality for this in the library.

As an example, consider the following json file fetched from the Wordpress server:

{
    "my_table": [
        {
            "id": "1",
            "name": "foo"
        },
        {
            "id": "2",
            "name": "bar"
        }
    ]
}

In C++ I would like to parse like this:

std::string fileContents = getFileContents(); // We get the data somehow
json dbData = json::parse(fileContents);

json tableData = dbData["my_table"];
for (json j : tableData)
{
    // This will fail because PHP gives quoted numbers and j["id"] is thus a string.
    // Can the library do this conversion in some way, or must it be done manually?
    int id = j["id"];

    // This works fine, but it causes some overhead (not really important though).
    // Besides, it is not pretty and kinda bloated. It will also have to handle possible
    // null values in some way.
    std::string sId = j["id"];
    int nId = std::stoi(sId);

    // This is fine, of course.
    std::string name = j["name"];

    // Run some SQLite queries with the data.
}

It is of course also possible to fix this on the server side, but as far as I have been able to figure out it is rather painful and I would like to stay clear of PHP if possible.

@nlohmann
Copy link
Owner

Hey @oysteinmyrmo, I understand your problem. But this is out of scope of the library: we parse JSON and choose the best C++ type to store the input. If your numbers are given as string, there is nothing we can do about it. If we would add string-to-number conversion to the library, it would consist of the same std::stoi call you described - including any exceptions thrown in case of unsuccessful conversion.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Oct 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants