//! Called when a REST request for fetching object version information becomes available. void ObjectCore::versionAvailable( QNetworkReply* reply, bool latest ) { // Process only if there was no error. if( reply->error() != QNetworkReply::NoError ) { qDebug( "Error when sending the property values." ); return; } // Parse the returned JSON. QByteArray replyContent = reply->readAll(); QJsonDocument result = QJsonDocument::fromJson( replyContent ); QJsonObject extendedVersion = result.object(); // Populate the cache. bool latestKnownVersionChanged = false; { // Construct properties array and then remove it to rever the extended version to standard object version. QJsonArray properties = extendedVersion[ "Properties" ].toArray(); extendedVersion.remove( "Properties" ); QJsonArray propertiesForDisplay; if( extendedVersion.contains( "PropertiesForDisplay" ) ) { propertiesForDisplay = extendedVersion[ "PropertiesForDisplay" ].toArray(); extendedVersion.remove( "PropertiesForDisplay" ); } QJsonObject objverObject = extendedVersion[ "ObjVer" ].toObject(); int version = objverObject[ "Version" ].toDouble(); // Store the new object. QMutexLocker lock( &m_mtx ); // Was the latest known version changed? if( latest ) { // Did the latest known version change? if( version != m_latestKnownVersion ) latestKnownVersionChanged = true; m_latestKnownVersion = version; } // end if // Create and cache the version core. MFiles::ObjVer objver( m_id, version ); ObjectVersionCore* versionCore = new ObjectVersionCore( this, objver ); versionCore->setProperties( properties ); if( ! propertiesForDisplay.empty() ) versionCore->setPropertiesForDisplay( propertiesForDisplay ); else versionCore->requestPropertiesForDisplay(); m_versions.insert( objver.version(), versionCore ); } // end lock // Was latest version changed? if( latestKnownVersionChanged ) emit latestVersionChanged(); }
void PHD2::sendJSONRPCRequest(const QString & method, const QJsonArray args) { QJsonObject jsonRPC; jsonRPC.insert("jsonrpc", "2.0"); jsonRPC.insert("method", method); if (args.empty() == false) jsonRPC.insert("params", args); jsonRPC.insert("id", methodID++); QJsonDocument json_doc(jsonRPC); emit newLog(json_doc.toJson(QJsonDocument::Compact)); tcpSocket->write(json_doc.toJson(QJsonDocument::Compact)); tcpSocket->write("\r\n"); }
//------------------------------------------------------------------------------ // Name: toJson //------------------------------------------------------------------------------ QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const { QString b; QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text); switch(v.type()) { case QJsonValue::Null: ss << "null"; break; case QJsonValue::Bool: ss << (v.toBool() ? "true" : "false"); break; case QJsonValue::Double: ss << v.toDouble(); break; case QJsonValue::String: ss << '"' << escapeString(v.toString()) << '"'; break; case QJsonValue::Array: { const QJsonArray a = v.toArray(); ss << "["; if(!a.empty()) { QJsonArray::const_iterator it = a.begin(); QJsonArray::const_iterator e = a.end(); ss << toJson(*it++, format); for(;it != e; ++it) { ss << ','; ss << toJson(*it, format); } } ss << "]"; } break; case QJsonValue::Object: { const QJsonObject o = v.toObject(); ss << "{"; if(!o.empty()) { QJsonObject::const_iterator it = o.begin(); QJsonObject::const_iterator e = o.end(); ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); ++it; for(;it != e; ++it) { ss << ','; ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); } } ss << "}"; } break; case QJsonValue::Undefined: Q_ASSERT(0); break; } return b; }