[SOLVED] Double or single quoting: compiler dependent?



  • Hallo.

    I have a simple predefined string:

    pSender->sendTextMessage(QString("{\"action\":\"auth\",\"status\":\"error\",\"data\": {\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}") );
    

    As you might guess, it is about WebSocket programming.
    The problem I have is that when I compile with msvc2013,
    this message string is created and transmitted properly with all the double quotes at the right place:

    {"action":"auth","status":"error","data": {"id":3,"msg":"Unknown user or incorrect password."}}
    

    But when I compile with gcc version 4.9.1 (Ubuntu 4.9.1-16ubuntu6), I get this output
    on the client side:

    { action: 'auth',status: 'error',data: {id:'3', msg:'Unknown user or incorrect password.'} }
    

    As you see, the JSON tags have no quote signs at all and their values are single quoted
    which is not JSON standard.

    I read through http://stackoverflow.com/questions/21431633/does-g-meets-stdstring-c11-requirements. Do not know if I went astray with that...
    But on the other hand side: such a simple thing as quoting a string should be compiler dependent?

    So would updating to g++-5 solve the problem as this Stackoverflow link says:
    The other answers were correct at the time, but as of nowadays, accordingly to the GCC 5.x Change Log, libstdc++ as shipped by gcc 5 is now fully C++11 conformant.


  • Moderators

    This post is deleted!

  • Moderators

    Just tried on Ubuntu with g++:

    std::cout << QString("{\"action\":\"auth\",\"status\":\"error\",\"data\": {\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}").toStdString();
    

    and got correct result.



  • @jsulm

    Thanks for your reply.

    Can you tell me please which version of g++ you used?
    Was it 5.x ?


  • Moderators

    g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4



  • @jsulm
    Thanks again.

    But what would you see if you try:

    qDebug () << QString("{\"action\":\"auth\",\"status\":\"error\",\"data\": {\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}"
    

  • Moderators

    In this case I see:

    "{\"action\":\"auth\",\"status\":\"error\",\"data\": {\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}"
    


  • @jsulm

    Yes, seems to be something QWebSockets and compiler specific.
    See http://code.woboq.org/qt5/qtwebsockets/src/websockets/qwebsocket_p.cpp.html#_ZN17QWebSocketPrivate15sendTextMessageERK7QString

    qint64 QWebSocketPrivate::sendTextMessage(const QString &message)
    {
           return doWriteFrames(message.toUtf8(), false);
    }
    

    And so on as doWriteFrames does coms char* conversion.


  • Moderators

    toUtf8() returns a QByteArray, QByteArray::data() returns char*. if I do:

    std::cout << QString("{\"action\":\"auth\",\"status\":\"error\",\"data\": {\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}").toUtf8().data();
    

    I still get the correct result.
    I don't think it is a compiler issue (why should compiler remove " or replace them with ' ?).
    How do you print out the string?



  • @jsulm said:

    std::cout << QString("{"action":"auth","status":"error","data": {"id":3,"msg":"Unknown user or incorrect password."}}").toUtf8().data();

    I set up this simple app:

    #include <QCoreApplication>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        std::cout << QString("{\"action\":\"auth\",\"status\":\"error\",\"data\":{\"id\":3,\"msg\":\"Unknown user or incorrect password.\"}}").toUtf8().constData();
        return a.exec();
    }
    

    This works as it should with mingw51_x64 and msvc2013_x64 on Windows.

    But with gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) no output is printed out at all,
    although it compiles without errors.

    So if it is not a compiler issue then it is a compiler installation issue.



  • As always complicated problems have unexpectedly simple solutions.
    Due to restricted user privileges I did not see the old chat server process
    started by a colleague of mine.

    So we were testing the old version all the time where the quoting issue
    had not been yet dealt with.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.