I am currently working on a server project using C++ and Qt.
Upon reception of a post request, I collect the json data from the request, and I would like to convert it to a QVariantMap.
Currently, the code snippet I have is the following:
QJsonParseError err;
QJsonDocument body;
body = QJsonDocument::fromJson(request->getRequest()->collectedData(), &err);
if ( !(err.error == QJsonParseError::NoError)) return;
QVariantMap userConfig = body.object().toVariantMap();
Note that the collectedData()
returns the desired QByteArray value.
The problem I have is on the decoding of numerical values.
While a float pi = 3.14
will be considered as a float within the QVariantMap, a float almost_pi = 3.0
will be considered as an int.
This means that if I want to write into a JSON file reusing my QVariantMap, the original Json
{
"pi":3.14,
"almost_pi":3.0
}
will be translated to
{
"pi":3.14,
"almost_pi":3
}
This is problematic for me as the rest of my pipeline depends on the newly generated json.
Is there a way to make sure the values inside a QVariantMap are correctly set ? Also, I have no way of knowing in advance what kind of values I will be receiving, so using toDouble()
, toInt()
methods is not an option here.
Thanks in advance!