You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
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.
Actually i disagree a bit. It would be possible to tell the library to allow "lax" parsing where a get<number_type> would automatically do a lexical cast
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:
In C++ I would like to parse like this:
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.
The text was updated successfully, but these errors were encountered: