QxOrm  1.5.0
C++ Object Relational Mapping library
QxSerializeQJson.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** https://www.qxorm.com/
00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
00005 **
00006 ** This file is part of the QxOrm library
00007 **
00008 ** This software is provided 'as-is', without any express or implied
00009 ** warranty. In no event will the authors be held liable for any
00010 ** damages arising from the use of this software
00011 **
00012 ** Commercial Usage
00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
00014 ** accordance with the commercial license agreement provided with the
00015 ** Software or, alternatively, in accordance with the terms contained in
00016 ** a written agreement between you and Lionel Marty
00017 **
00018 ** GNU General Public License Usage
00019 ** Alternatively, this file may be used under the terms of the GNU
00020 ** General Public License version 3.0 as published by the Free Software
00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
00022 ** packaging of this file. Please review the following information to
00023 ** ensure the GNU General Public License version 3.0 requirements will be
00024 ** met : http://www.gnu.org/copyleft/gpl.html
00025 **
00026 ** If you are unsure which license is appropriate for your use, or
00027 ** if you have questions regarding the use of this file, please contact :
00028 ** contact@qxorm.com
00029 **
00030 ****************************************************************************/
00031 
00032 #ifndef _QX_NO_JSON
00033 #ifndef _QX_SERIALIZE_QJSON_H_
00034 #define _QX_SERIALIZE_QJSON_H_
00035 
00036 #ifdef _MSC_VER
00037 #pragma once
00038 #endif
00039 
00047 #include <QtCore/qjsonvalue.h>
00048 #include <QtCore/qjsonobject.h>
00049 #include <QtCore/qjsonarray.h>
00050 #include <QtCore/qjsondocument.h>
00051 #include <QtCore/qfile.h>
00052 
00053 #include <QxCommon/QxBool.h>
00054 
00055 #include <QxConvert/QxConvert.h>
00056 
00057 namespace qx {
00058 namespace serialization {
00059 
00064 namespace json {
00065 
00066 template <class T>
00067 inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00068 {
00069    Q_UNUSED(flags); Q_UNUSED(owner);
00070    QJsonValue val = qx::cvt::to_json(obj, format);
00071    QJsonDocument doc = (val.isArray() ? QJsonDocument(val.toArray()) : QJsonDocument(val.toObject()));
00072    return doc.toJson();
00073 }
00074 
00075 template <class T>
00076 inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00077 {
00078    Q_UNUSED(flags);
00079    QJsonParseError err;
00080    QJsonDocument doc = QJsonDocument::fromJson(data, (& err));
00081    if (err.error != QJsonParseError::NoError)
00082    { return qx_bool(false, static_cast<long>(err.error), err.errorString()); }
00083    QJsonValue val = (doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object()));
00084    return qx::cvt::from_json(val, obj, format);
00085 }
00086 
00087 template <class T>
00088 inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00089 { return QString::fromUtf8(qx::serialization::json::to_byte_array(obj, NULL, flags, format)); }
00090 
00091 template <class T>
00092 inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00093 { return qx::serialization::json::from_byte_array(obj, sString.toUtf8(), flags, format); }
00094 
00095 template <class T>
00096 inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00097 {
00098    QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
00099    QFile file(sFileName);
00100    if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
00101    { return qx_bool(false, "cannot open file : " + sFileName); }
00102    file.write(data); file.close();
00103    return qx_bool(true);
00104 }
00105 
00106 template <class T>
00107 inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00108 {
00109    QFile file(sFileName);
00110    if (! file.open(QIODevice::ReadOnly))
00111    { return qx_bool(false, "cannot open file : " + sFileName); }
00112    QByteArray data = file.readAll(); file.close();
00113    return qx::serialization::json::from_byte_array(obj, data, flags, format);
00114 }
00115 
00116 template <class T>
00117 inline qx_bool to_file_compressed(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, int iCompressionLevel = -1, const QString & format = QString())
00118 {
00119    QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
00120    QByteArray compressed = qCompress(data, iCompressionLevel);
00121    QFile file(sFileName);
00122    if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
00123    { return qx_bool(false, "cannot open file : " + sFileName); }
00124    file.write(compressed); file.close();
00125    return qx_bool(true);
00126 }
00127 
00128 template <class T>
00129 inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
00130 {
00131    QFile file(sFileName);
00132    if (! file.open(QIODevice::ReadOnly))
00133    { return qx_bool(false, "cannot open file : " + sFileName); }
00134    QByteArray data = file.readAll(); file.close();
00135    QByteArray uncompressed = qUncompress(data);
00136    return qx::serialization::json::from_byte_array(obj, uncompressed, flags, format);
00137 }
00138 
00139 } // namespace json
00140 } // namespace serialization
00141 } // namespace qx
00142 
00143 #endif // _QX_SERIALIZE_QJSON_H_
00144 #endif // _QX_NO_JSON