QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxSerializeQJson.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_NO_JSON
33#ifndef _QX_SERIALIZE_QJSON_H_
34#define _QX_SERIALIZE_QJSON_H_
35
36#ifdef _MSC_VER
37#pragma once
38#endif
39
47#include <QtCore/qjsonvalue.h>
48#include <QtCore/qjsonobject.h>
49#include <QtCore/qjsonarray.h>
50#include <QtCore/qjsondocument.h>
51#include <QtCore/qfile.h>
52
53#include <QxCommon/QxBool.h>
54
55#include <QxConvert/QxConvert.h>
56
57namespace qx {
58namespace serialization {
59
64namespace json {
65
66template <class T>
67inline QByteArray to_byte_array(const T & obj, void * owner = NULL, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
68{
69 Q_UNUSED(flags); Q_UNUSED(owner);
70 QJsonValue val = qx::cvt::to_json(obj, format);
71 QJsonDocument doc = (val.isArray() ? QJsonDocument(val.toArray()) : QJsonDocument(val.toObject()));
72 return doc.toJson();
73}
74
75template <class T>
76inline qx_bool from_byte_array(T & obj, const QByteArray & data, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
77{
78 Q_UNUSED(flags);
79 QJsonParseError err;
80 QJsonDocument doc = QJsonDocument::fromJson(data, (& err));
81 if (err.error != QJsonParseError::NoError)
82 { return qx_bool(false, static_cast<long>(err.error), err.errorString()); }
83 QJsonValue val = (doc.isArray() ? QJsonValue(doc.array()) : QJsonValue(doc.object()));
84 return qx::cvt::from_json(val, obj, format);
85}
86
87template <class T>
88inline QString to_string(const T & obj, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
89{ return QString::fromUtf8(qx::serialization::json::to_byte_array(obj, NULL, flags, format)); }
90
91template <class T>
92inline qx_bool from_string(T & obj, const QString & sString, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
93{ return qx::serialization::json::from_byte_array(obj, sString.toUtf8(), flags, format); }
94
95template <class T>
96inline qx_bool to_file(const T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
97{
98 QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
99 QFile file(sFileName);
100 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
101 { return qx_bool(false, "cannot open file : " + sFileName); }
102 file.write(data); file.close();
103 return qx_bool(true);
104}
105
106template <class T>
107inline qx_bool from_file(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
108{
109 QFile file(sFileName);
110 if (! file.open(QIODevice::ReadOnly))
111 { return qx_bool(false, "cannot open file : " + sFileName); }
112 QByteArray data = file.readAll(); file.close();
113 return qx::serialization::json::from_byte_array(obj, data, flags, format);
114}
115
116template <class T>
117inline 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())
118{
119 QByteArray data = qx::serialization::json::to_byte_array(obj, NULL, flags, format);
120 QByteArray compressed = qCompress(data, iCompressionLevel);
121 QFile file(sFileName);
122 if (! file.open(QIODevice::WriteOnly | QIODevice::Truncate))
123 { return qx_bool(false, "cannot open file : " + sFileName); }
124 file.write(compressed); file.close();
125 return qx_bool(true);
126}
127
128template <class T>
129inline qx_bool from_file_compressed(T & obj, const QString & sFileName, unsigned int flags = 1 /* boost::archive::no_header */, const QString & format = QString())
130{
131 QFile file(sFileName);
132 if (! file.open(QIODevice::ReadOnly))
133 { return qx_bool(false, "cannot open file : " + sFileName); }
134 QByteArray data = file.readAll(); file.close();
135 QByteArray uncompressed = qUncompress(data);
136 return qx::serialization::json::from_byte_array(obj, uncompressed, flags, format);
137}
138
139} // namespace json
140} // namespace serialization
141} // namespace qx
142
143#endif // _QX_SERIALIZE_QJSON_H_
144#endif // _QX_NO_JSON
qx_bool : QxOrm library boolean type with code and description message when an error occured
qx::QxBool qx_bool
Definition QxBool.h:150
qx::cvt : namespace to provide global functions to convert any kind of objects to/from QString and QV...
qx_bool : boolean type with code and description message when an error occured
Definition QxBool.h:71
qx_bool from_json(const QJsonValue &j, T &t, const QString &format=QString())
Definition QxConvert.h:84
QJsonValue to_json(const T &t, const QString &format=QString())
Definition QxConvert.h:83
QByteArray to_byte_array(const T &obj, void *owner=NULL, unsigned int flags=1, const QString &format=QString())
QString to_string(const T &obj, unsigned int flags=1, const QString &format=QString())
qx_bool to_file(const T &obj, const QString &sFileName, unsigned int flags=1, const QString &format=QString())
qx_bool from_string(T &obj, const QString &sString, unsigned int flags=1, const QString &format=QString())
qx_bool from_file(T &obj, const QString &sFileName, unsigned int flags=1, const QString &format=QString())
qx_bool to_file_compressed(const T &obj, const QString &sFileName, unsigned int flags=1, int iCompressionLevel=-1, const QString &format=QString())
qx_bool from_byte_array(T &obj, const QByteArray &data, unsigned int flags=1, const QString &format=QString())
qx_bool from_file_compressed(T &obj, const QString &sFileName, unsigned int flags=1, const QString &format=QString())
Root namespace for all QxOrm library features.