not working with Qt 5.3

Issue #11 closed
Evgeniy Dushistov created an issue

I tried your library with Qt 5.3, and it not work as expected. I dig into the problem and find out that root of problem is code here:

#if QT_VERSION >= 0x050200
    return d->object->value(QLatin1String("id")).toInt();
#else
    return d->object->value(QLatin1String("id")).toDouble();
#endif

in src/qjsonrpcmessage.cpp,

sometimes type of d->object->value(...) is String, sometimes Double. So this code should have both parts of "#ifdef".

I fixed it like this:

@@ -292,11 +282,12 @@ int QJsonRpcMessage::id() const
     if (d->type == QJsonRpcMessage::Notification || !d->object)
         return -1;

-#if QT_VERSION >= 0x050200
-    return d->object->value(QLatin1String("id")).toInt();
-#else
-    return d->object->value(QLatin1String("id")).toDouble();
-#endif
+    const QJsonValue val = d->object->value(QLatin1String("id"));
+
+    if (val.type() == QJsonValue::Double)
+           return val.toDouble();
+    else
+           return val.toString().toInt();
 }

 QString QJsonRpcMessage::method() const
@@ -332,11 +323,11 @@ int QJsonRpcMessage::errorCode() const

     QJsonObject error =
         d->object->value(QLatin1String("error")).toObject();
-#if QT_VERSION >= 0x050200
-    return error.value(QLatin1String("code")).toInt();
-#else
-    return error.value(QLatin1String("code")).toDouble();
-#endif
+    const QJsonValue val = error.value(QLatin1String("code"));
+    if (val.type() == QJsonValue::Double)
+       return val.toDouble();
+    else
+        return val.toString().toInt();
 }

 QString QJsonRpcMessage::errorMessage() const

Sorry I have github account, so I can not use "pull request", but fix is small enough, so it can be applied by hands.

Comments (4)

  1. Matt Broadstone

    Ah yes, this is an unfortunate ambiguity in the spec. Originally the ID had to be an integer, however it makes a lot of sense sometimes to make the id a full string. I'm not really sure what to do when someone chooses to have an ID of e.g. "myrpcsystem_2345", but I will fix this to take into account id's being sent as strings. Thanks!

  2. Matt Broadstone

    Support ids and errorCodes sent as strings

    Sometimes implementations use strings to represent integer values for message ids and error codes. This closes issue #11

    → <<cset cb240a68a813>>

  3. Matt Broadstone

    Support ids and errorCodes sent as strings

    Sometimes implementations use strings to represent integer values for message ids and error codes. This closes issue #11

    → <<cset d2efc673ac11>>

  4. Log in to comment