Parsing XML vs JSON (best one to use?)



  • Hi,

    I have a web service that return XML data for now. I then parse this result with the class "QXmlStreamReader".

    I heard a lot of praise on JSON, is it worth it to modify my web service so that it return JSON data instead of XML?
    I have some method from my web service that returns ~200 elements, each element with ~40 tags to parse (so approx 8000 fields to parse and put in Qt object. Is there a real benefit of switching to JSON with QT?

    Example of my current parsing code that retrieve user info from DB (this one is lightweight) :

    @ QByteArray arrayData = reply->readAll();
    qDebug() << arrayData;
    QXmlStreamReader xml(arrayData);

    /// Modify global user properties
    XmlUtil::createUserFromXml(xml);
    

    void XmlUtil::createUserFromXml(QXmlStreamReader& xml) {

    int id = 0;
    QString username;
    QString password;
    QUuid session_id;
    QString first_name;
    QString last_name;
    QDate birthdate;
    QChar sex;
    QString email;
    QString last_ip;
    QDateTime last_login;
    double weight;
    double height;
    int FTP;
    int LTHR;
    int KM_done;
    
    
    while(!xml.atEnd() && !xml.hasError())
    {
        /* Read next element.*/
        xml.readNext();
        if(xml.name() == "id") {
            id = xml.readElementText().toInt();
        }
        else if(xml.name() == "username") {
            username = xml.readElementText();
        }
        else if(xml.name() == "password") {
            password = xml.readElementText();
        }
        else if(xml.name() == "first_name") {
            first_name = xml.readElementText();
        }
        else if(xml.name() == "last_name") {
            last_name = xml.readElementText();
        }
        else if(xml.name() == "birthdate") {
            birthdate = QDate::fromString( xml.readElementText(), "yyyy-MM-dd");
        }
        else if(xml.name() == "sex") {
            sex = xml.readElementText().at(0);
        }
        else if(xml.name() == "email") {
            email = xml.readElementText();
        }
    

    // else if(xml.name() == "last_ip") {
    // last_ip = xml.readElementText();
    // }
    // else if(xml.name() == "last_login") {
    // last_login = QDateTime::fromString(xml.readElementText(), "yyyy-MM-dd hh:mm:ss");
    // }
    else if(xml.name() == "weight") {
    weight = xml.readElementText().toDouble();
    }
    else if(xml.name() == "height") {
    height = xml.readElementText().toDouble();
    }
    else if(xml.name() == "FTP") {
    FTP = xml.readElementText().toInt();
    }
    else if(xml.name() == "LTHR") {
    LTHR = xml.readElementText().toInt();
    }
    else if(xml.name() == "KM_done") {
    KM_done = xml.readElementText().toInt();
    }

    }
    User *user = qApp->property("User").value<User*>();
    user->setData(id, username, password, session_id, first_name,  last_name,
                          birthdate,  sex, email, last_ip, last_login,
                          weight,  height,  FTP,  LTHR,  KM_done);
    

    }
    @


  • Moderators

    JSON is easier to read and faster to parse (parsing should consume less CPU cycles and less RAM).

    However, I think that rewriting your current code just because JSON has some advantages is not a good decision. If you have some users of your web service, they would all need to rewrite their code (so that they can communicate with your server), and also you would need to rewrite all your code, retest everything, etc. IMO: it's not worth it, the changes in performance would probably be minimal (do you have any performance problems right now, with the current code?), but the effort considerable. What you can do is to start a side project: new API for your web service, working in parallel with the old one. This way you don't risk breaking current behaviour for anybody, yet it will enable you to take your time and polish JSON implementation.



  • Thanks for your answer sierdzio!

    My web service is currently used only by my Qt application (the project is still in development). The web service should probably always be used only by myself, it's a bridge to my database from my Qt app. That's why i'm thinking of trying JSON, because it's still early and i'm not going to break anything yet :)

    So far I don't have any performance problem, and I find testing the web service with xml quite easy (I can test in my browser before using it with QtNetworkManager, will I be able to do that with JSON?)

    I guess I will stick to xml for now, and when I get performance problem, I will start it in parallel and see if it makes a difference. !

    Exemple of a a web service query that returns xml :
    http://maximumtrainer.com/phprestsql/?username=max3&password=123&getListWorkoutForUser=1

    I don't mind giving the password it's a test account, you can try to sql inject or anything, also I Need to test security...will pay 100 dogecoin if you find a hole and don't break it :) └(°ᴥ°)┘


  • Moderators

    Hehe, sorry but security testing is not my thing.

    I think all browsers should happily read JSON just as they read XML. "Link":http://json.org/example.html.


Log in to reply
 

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