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
Contributor
mrmusic
Posts: 12
Registered: ‎07-11-2012
My Device: BB10 Dev Alpha
Accepted Solution

JsonDataAccess and UTF-8 encoding

Hi, 

I am fetching JSON from the web service and I am parsing it using JsonDataAccess. JSON itself contains Swedish characters (äöåÄÅÖ)

If I print response string before it’s parsed using JsonDataAccess all characters are correct, but after parsing it with JsonDataAccess.loadFromBuffer(info) characters are no longer correct.


Am I doing something wrong or this is a bug?

 

Code: 


int available = reply->bytesAvailable();

QString response;

if (available > 0)
{
     int bufSize = sizeof(char) * available + sizeof(char);
     QByteArray buffer(bufSize, 0);
     int read = reply->read(buffer.data(), available);
     response = QString::fromUtf8(buffer);

}

qDebug() << response; // gives correct characters

bb::data::JsonDataAccess ja;

QVariantMap jsonva = ja.loadFromBuffer(info).toMap();

qDebug() << jsonva; //gives incorrect characters




BlackBerry Development Advisor (Retired)
selom
Posts: 60
Registered: ‎05-10-2012
My Device: Blackberry 10 Alpha

Re: JsonDataAccess and UTF-8 encoding

Hello,

 

It is possible that it is a bug, but I can't say since I don't have access to a non-english json response. JsonDataAccess also has an overloaded loadFromBufffer() method that takes a QString, so as a workaround you could do

 

QVariantMap jsonva = ja.loadFromBuffer(QString::fromUtf8(buffer));

 

PS: as a side note your code snippet does not specify where the "info" variable is initialized from. Shouldn't it be reading from buffer?

 

Cheers

Selom

Contributor
mrmusic
Posts: 12
Registered: ‎07-11-2012
My Device: BB10 Dev Alpha

Re: JsonDataAccess and UTF-8 encoding

I am sorry regarding "info" variable, the code pasted is merged from several different functions.. "info" is the same as "response". 

However, following link will give you access to JSON with swedish characters

http://2.app-oteket.appspot.com/searchAll?lon=12.99892&lat=55.61184

 

BlackBerry Development Advisor (Retired)
selom
Posts: 60
Registered: ‎05-10-2012
My Device: Blackberry 10 Alpha

Re: JsonDataAccess and UTF-8 encoding

Hi,

 

I think the behavior is normal. When I use

 

QVariantMap jsonva = ja.loadFromBuffer(QString::fromUtf8(buffer));

 

the data is printed correctly, but when I use

 

QVariantMap jsonva = ja.loadFromBuffer(QString(buffer));

 

it assumes the format is ascii and prints it out that way. Since there is no way to specify the format of the data when using "loadFromBuffer" with a bytearray, it is normal to assume it defaults to ascii.

 

To handle utf-8, it should be converted using fromUtf8() before calling loadFromBuffer().

 

cheers

Selom 

Contributor
mrmusic
Posts: 12
Registered: ‎07-11-2012
My Device: BB10 Dev Alpha

Re: JsonDataAccess and UTF-8 encoding

Hi,

I am already using  QString::fromUtf8(buffer); to create response string, please have a look at the pasted code below. 

When I print out response the charcters are OK, but after  loadFromBuffer() characters get wrong.

 

int available = reply->bytesAvailable();

QString response;

if (available > 0)
{
     int bufSize = sizeof(char) * available + sizeof(char);
     QByteArray buffer(bufSize, 0);
     int read = reply->read(buffer.data(), available);
     response = QString::fromUtf8(buffer);

}

qDebug() << response; // gives correct characters

bb::data::JsonDataAccess ja;

QVariantMap jsonva = ja.loadFromBuffer(response).toMap();

qDebug() << jsonva; //gives incorrect characters

BlackBerry Development Advisor (Retired)
selom
Posts: 60
Registered: ‎05-10-2012
My Device: Blackberry 10 Alpha

Re: JsonDataAccess and UTF-8 encoding

My apologies, you are correct. I rerun the test again and looked carefully this time and they are different. I suspect this is a bug too, and will recommend you create a Jira bug for it.

 

Cheers

Selom

Contributor
mrmusic
Posts: 12
Registered: ‎07-11-2012
My Device: BB10 Dev Alpha

Re: JsonDataAccess and UTF-8 encoding

I will do so. 

Thanks!

 

 

New Member
xqupVoUiMVgHC
Posts: 1
Registered: ‎09-03-2012
My Device: n/a

Re: JsonDataAccess and UTF-8 encoding

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));