Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
ekke
Posts: 1,556
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone
Accepted Solution

is this efficient QtCode ?

you probably know that Qt and C++ is new for me after so many years of Java...

 

so here's a question:

 

is this code efficient ?

or is something weong ?

or could be done better ?

 

... imagine a JSON as result from HTTP request like this one:

{"body": {"token":"1234","sample":"one"},"error":""}

 and this C++ code:

// reply is QNetworkReply
QByteArray s = reply->readAll();
JsonDataAccess jda; // we load a JSON with root as Object QVariant rootObject(jda.loadFromBuffer(s)); if (!rootObject.isNull()) { // there are some values at root, so we get a Map QVariantMap rootMap(rootObject.toMap()); if (!rootMap.isEmpty()) { // now see if there's a body QVariant level1(rootMap.value("body")); if (!level1.isNull()) { // level1 contains some Values in a map QVariantMap level1map(level1.toMap()); if (!level1map.isEmpty()) { // now let's process the entries QString tokenString(level1map.value("token").toString()); if (!tokenString.isEmpty()) { // YEP we found the token qDebug() << "TOKEN:" << tokenString; } else { qDebug() << "tokenstring is empty"; } // no process other entries // ....... } else { qDebug() << "level1 body found, but is empty"; } } else { qDebug() << "level1 body is null !"; } // parse for an error entry level1 = rootMap.value("error"); if (!level1.isNull()) { // level1 contains some Values in a map QString errorString(level1.toString()); if (!errorString.isEmpty()) { qDebug() << "ERROR:" << errorString; } else { qDebug() << "errorString is empty"; } } else { qDebug() << "level1 error is null !"; } } else { qDebug() << "root is no QMap or empty"; } } else { qDebug() << "root is no QVariant"; }

the JSON is simplified - in reality some levels with Strings and ints

 

 thx

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: is this efficient QtCode ?

const QVariantMap data = jda.loadFromBuffer(s).toMap()
if(map.contains("body") {
const QVariantMap body = data.value("body").toMap();
QString token = body.value("token","").toString();
QString level = body.value("level","").toString(); if(token.isEmpty()) { } //no token found }
if(map.contains("error")) {
//do what you want with the error
}

You could do this in wayyyyyyy less lines. :smileyhappy: 

Like all of my posts
Developer
ekke
Posts: 1,556
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: is this efficient QtCode ?


kylefowler wrote:
const QVariantMap data = jda.loadFromBuffer(s).toMap()
if(map.contains("body") {
const QVariantMap body = data.value("body").toMap();
QString token = body.value("token","").toString();
QString level = body.value("level","").toString(); if(token.isEmpty()) { } //no token found }
if(map.contains("error")) {
//do what you want with the error
}

You could do this in wayyyyyyy less lines. :smileyhappy: 


Kyle,

I was sure there was a shorter way - THX :smileyhappy:

coming from java always thinking about NULL problems and casting-problems ....

I'll try your way for the complete JSON - thx for help

... Cascades / QML coding was easy to learn

... C++ / Qt - I still have to learn much.

ekke

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900

Re: is this efficient QtCode ?

Its definitely good to use the default value parameter of the value() method. You can still get null values but that prevents it in general, and if not then just check with contains before doing anything on that object.
Like all of my posts