Some weeks ago, I wrote a little JSON-RPC implemention on my Windows Notebook based on C++ and Qt. I used the Visual C++ 2013 compiler and it worked without any problems.
Now I copied my code to an Linux Mint machine with an GCC compiler and I always get the following error:
jsonrpc.h:18: Error: conversion from 'long int' to 'QJsonValue' is ambiguous
static QJsonObject generateErrorObj(ErrorCode code, QString message, QJsonValue data = NULL);
This error also appears on line 19 (method generateErrorResponse) and line 20 (method generateRequest). So... now I'm not that familiar with C++ or Qt, so I don't get it, why this doesn't work, althought it worked on Windows...
Here is the full code of the jsonrpc.h:
#ifndef JSONRPC_H
#define JSONRPC_H
#include <QtCore>
class JSONRPC
{
public:
enum ErrorCode
{
PARSE_ERROR = -32700,
INVALID_REQUEST = -32600,
METHOD_NOT_FOUND = -32601,
INVALID_PARAMS = -32602,
INTERNAL_ERROR = -32603
};
static QJsonObject generateObj(QString id, bool isNotification = false);
static QJsonObject generateErrorObj(ErrorCode code, QString message, QJsonValue data = NULL);
static QJsonObject generateErrorResponse(QString id, ErrorCode code, QString message, QJsonValue data = NULL);
static QJsonObject generateRequest(QString id, QString method, QJsonValue parameters = NULL, bool isNotification = false);
static QJsonObject generateResponse(QString id, QJsonValue result);
};
#endif // JSONRPC_H
NULL is likely implemented in a different way on both platforms. The "problematic" implementation creates an ambiguity as there is more than one likely candidate for implicit conversion.
Instead of JsonValue parameters = NULL try JsonValue parameters = JsonValue() - that constructor will create a json value of type null.
Furthermore, even if JsonValue parameters = NULL "works" it will likely be wrong, as it will be equal to something like JsonValue(int(0)) and not a "null" json value as in JsonValue() which has a default QJsonValue::Type::Null parameter. So you won't have a NULL json value, but a NUMBER json value with value of 0 - two completely different things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With