32#ifndef _QX_SERIALIZE_QDATASTREAM_H_
33#define _QX_SERIALIZE_QDATASTREAM_H_
48#include <QtCore/qdatastream.h>
49#include <QtCore/qfile.h>
54namespace serialization {
63inline QByteArray
to_byte_array(
const T & obj,
void * owner = NULL,
unsigned int flags = 1 )
65 Q_UNUSED(flags); Q_UNUSED(owner);
66 QByteArray ba; QString err;
67 QDataStream stream((& ba), QIODevice::WriteOnly);
68 stream << (quint32)(9438);
69 try { stream << obj; }
70 catch (
const std::exception & e) { err = QString(
"serialization error '%ERR%'").replace(
"%ERR%", e.what()); }
71 catch (...) { err = QString(
"serialization error '%ERR%'").replace(
"%ERR%",
"unknown error"); }
72 if (! err.isEmpty()) { qDebug(
"[QxOrm] qx::serialization::qt::to_byte_array() : %s", qPrintable(err)); ba.clear(); }
81 if (data.isEmpty()) {
return qx_bool(
false,
"input binary data is empty"); }
82 QDataStream stream(data);
83 quint32 magic = 0; stream >> magic;
84 if (magic != 9438) {
return qx_bool(
false,
"input binary data is not valid"); }
85 try { stream >> obj; result =
true; }
86 catch (
const std::exception & e) { result.
setDesc(QString(
"deserialization error '%ERR%'").replace(
"%ERR%", e.what())); }
87 catch (...) { result.
setDesc(QString(
"deserialization error '%ERR%'").replace(
"%ERR%",
"unknown error")); }
88 if (! result.
getDesc().isEmpty()) { QString msg = result.
getDesc(); qDebug(
"[QxOrm] qx::serialization::qt::from_byte_array() : %s", qPrintable(msg)); }
93inline QString
to_string(
const T & obj,
unsigned int flags = 1 )
101inline qx_bool to_file(
const T & obj,
const QString & sFileName,
unsigned int flags = 1 )
104 QFile file(sFileName);
105 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
106 {
return qx_bool(
false,
"cannot open file : " + sFileName); }
115 QFile file(sFileName);
116 if (! file.open(QIODevice::ReadOnly))
117 {
return qx_bool(
false,
"cannot open file : " + sFileName); }
118 QByteArray data = file.readAll(); file.close();
126 QByteArray compressed = qCompress(data, iCompressionLevel);
127 QFile file(sFileName);
128 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
129 {
return qx_bool(
false,
"cannot open file : " + sFileName); }
130 file.write(compressed);
138 QFile file(sFileName);
139 if (! file.open(QIODevice::ReadOnly))
140 {
return qx_bool(
false,
"cannot open file : " + sFileName); }
141 QByteArray data = file.readAll(); file.close();
142 QByteArray uncompressed = qUncompress(data);
qx_bool : QxOrm library boolean type with code and description message when an error occured
qx_bool : boolean type with code and description message when an error occured
void setDesc(const QString &sDesc)
qx_bool to_file(const T &obj, const QString &sFileName, unsigned int flags=1)
QString to_string(const T &obj, unsigned int flags=1)
qx_bool from_string(T &obj, const QString &sString, unsigned int flags=1)
qx_bool from_file_compressed(T &obj, const QString &sFileName, unsigned int flags=1)
qx_bool from_file(T &obj, const QString &sFileName, unsigned int flags=1)
qx_bool from_byte_array(T &obj, const QByteArray &data, unsigned int flags=1)
QByteArray to_byte_array(const T &obj, void *owner=NULL, unsigned int flags=1)
qx_bool to_file_compressed(const T &obj, const QString &sFileName, unsigned int flags=1, int iCompressionLevel=-1)
Root namespace for all QxOrm library features.