Im using qt 5.0 and its support such classes as QJsonObject QJsonDocument and QJsonArray. In my programm i need to serialize json array and convert it to qstring/qbytearray but i didn't found any serialize or encode methods in those classes. Is there any way i can serialize data using included qt 5.0. libs? I found this example:
QVariant id(1), name("John Doe");
QJsonObject json;
json["Name"] = name.toString();
json.insert("id", id.toInt());
But i can't find how i can make an array from it.
Question closed. Use QJsonDocument::toJson to get data from QJsonObject.
I think the secret is that when serializing to JSON, Qt writes out a UTF-8 binary encoded version of the text into a QByteArray. You can recover the string using QString::fromUtf8. Also, even though toJson writes out a QByteArray it is not a true binary representation; as I just mentioned, it is the bytes of the UTF-8 encoded string representation of the JSON. For binary serialization you must use either Qt's own QJsonDocument::toBinaryData or find a BSON library.
Text serialization
Use QJsonDocument::toJson to serialize as text via a QString, and QJsonDocument::fromJson to deserialize from text.
Tip: Consider passing the QJsonDocument::Compact format option to get compact JSON output during serialization.
To QString
QString json = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
From QString
QJsonDocument doc = QJsonDocument::fromJson(json);
Binary serialization
Use QJsonDocument::toBinaryData to serialize as binary via a QByteArray, and QJsonDocument::fromBinaryData to deserialize from binary.
Caveat: The binary format used by the above functions is Qt's own binary format (qbjs) so may not be suitable for interoperation with other binary formats such as BSON. For that, you might want to look into choosing a BSON implementation.
To QByteArray
QByteArray bytes = doc.toBinaryData();
From QByteArray
QJsonDocument doc = QJsonDocument::fromBinaryData(bytes);
Related
I have a common string, which is encoded like ISO-88-59-5 and I want to transform this string to UTF-8 format, by the way, I have the code example on C# which is working well. I need to do the same on C++
result = mainString.Substring(nameStart + 3, symbols);
Encoding enc = Encoding.GetEncoding("ISO-8859-5");
byte[] bytes = enc.GetBytes(result);
result = Encoding.UTF8.GetString(bytes);
result is a string with text
The procedure to do this on Linux is as follows:
Use iconv_open() as described in its manual page to create a handle for a conversion from windows-1251 to UTF-8. I just double-checked and "windows-1251" is supported by the iconv library.
Use iconv() as described in its manual page.
Use iconv_close() as described in its manual page.
Here's the situation.
Let's say I have a gltf file containing :
"uri" : "data:application/gltf-buffer;base64,AAAIAAcAAAABAAgAAQAJAAgAAQACAAkAAgAKAAkAAgADAAoAAwALAAoAAwAEAAsABAAMAAsABAAFAAwABQANAAwABQAGAA0AAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAQAAAAAAAAAAAAABAQAAAAAAAAAAAAACAQAAAAAAAAAAAAACgQAAAAAAAAAAAAADAQAAAAAAAAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAAAAQAAAgD8AAAAAAABAQAAAgD8AAAAAAACAQAAAgD8AAAAAAACgQAAAgD8AAAAAAADAQAAAgD8AAAAACAAKAAwAAAAAAIA/AAAAQAAAAAAAAEBAAABAQAAAAAAAAKBAAACAQAAAAAA=",
I understand how to extract the type and the base using QJsonValue::toString() and QString parsing functions (like split(";")) but my problems remain : I have now a string that correspond to my gltf buffer and I don't know how to read it. (I know what it is, an d how the data should be interpreted thanks to accessors and bufferviews)
tldr: how to convert that string of letters to a QByteArray ?
you tried it like that?))
QByteArray by = QByteArray::fromBase64("AAAIAAcAAAABAAgAAQAJAAgAAQACAAkAAgAKAAkAAgADAAoAAwALAAoAAwAEAAsABAAMAAsABAAFAAwABQANAAwABQAGAA0AAAAAAAAAAAAAAAAAAACAPwAAAAAAAAAAAAAAQAAAAAAAAAAAAABAQAAAAAAAAAAAAACAQAAAAAAAAAAAAACgQAAAAAAAAAAAAADAQAAAAAAAAAAAAAAAAAAAgD8AAAAAAACAPwAAgD8AAAAAAAAAQAAAgD8AAAAAAABAQAAAgD8AAAAAAACAQAAAgD8AAAAAAACgQAAAgD8AAAAAAADAQAAAgD8AAAAACAAKAAwAAAAAAIA/AAAAQAAAAAAAAEBAAABAQAAAAAAAAKBAAACAQAAAAAA=");
Hello i have a project using both objective-c and c++ , I never set any encoding and on the right panel of the file page it says “no specific encoding set”, but I’ve read that NSString is natively utf-16 so how would I translate a c++ string(utf-8) to NSString(utf-16)?
You can use the std::string::data() method to get access to the raw bytes of the std::string. Once you have that, you can use the init(bytes:length:encoding:) constructor for NSString to convert the raw bytes into a NSString. Specify that the encoding is UTF-8.
I am using Qt on Windows and I want to turn this part here:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />
into the native binary encoding of a .png file. I have already isolated the iVBORw0KGgoAAAANSUhEUgAAADIA... part as a QString, my only question is how to convert that into say, a QByteArray that can be written to a file. Specifically, a .png file. The QByteArray::fromBase64 method is what I tried, it doesn't work.
So my code is:
QDataStream stream(&file);
QByteArray qba;
qba.append(sourcestring);
stream << QByteArray::fromBase64(qba);
sourcestring is the source string (with the data:image/png;base64, part removed) and file is the png file I am writing to.
You might do this:
QString s("iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAl...");
QByteArray ba = QByteArray::fromBase64(s.toUtf8());
QImage img = QImage::fromData(ba);
img.save("test.png");
I'm under Windows and I supposed the default codec for QString is GBK, but I have to send some content to a Linux platform which doesn't support GBK. I'm sending some CJK content so I decided to use UTF8.
How can I get what codec I'm using for QString and set the codec for it ?
Here's the line:
packet = packet.arg(MAC, operation, text_type, text.toUtf8());
I'm trying to insert some CJK text to a normal QString.
You do not necessarily need to think about the codec. What about:
QString::fromLocal8Bit(myInput).toUtf8();
This should work fine. If you really need to manually mess with the codec look for QTextCodec.