Timesheets-Server
Timesheets server
Načítám...
Vyhledávám...
Nebylo nic nalezeno
json.cpp
1#include "json.h"
2#include <QVariant>
3#include <QList>
4#include <QSet>
5#include <QDebug>
6
7#if QT_VERSION > 0x050000
8#include <QJsonObject>
9#include <QJsonParseError>
10#else
11#ifdef WIN32
12#include <QJson/Parser>
13#include <QJson/Serializer>
14#else
15#include <qjson/parser.h>
16#include <qjson/serializer.h>
17#endif
18#endif
19
20
21QByteArray JSON::json(const QVariant& data) {
22 #if QT_VERSION > 0x050000
23 QByteArray json = QJsonDocument::fromVariant(data).toJson(QJsonDocument::Compact);
24 return json;
25
26 #else
27
28 bool ok = true;
29 QJson::Serializer serializer;
30 #ifdef ARM
31 QByteArray json = serializer.serialize(data);
32 #else
33 QByteArray json = serializer.serialize(data, &ok);
34 #endif
35 if (!ok) {
36 qDebug() << QString("Sorry, there is a problem with data serializing in JSON::json()");
37 }
38
39 return json;
40 #endif
41}
42
43
44QByteArray JSON::json(const QVariant& data, bool *ok) {
45 #if QT_VERSION > 0x050000
46 QByteArray json = QJsonDocument::fromVariant(data).toJson(QJsonDocument::Compact);
47 *ok = true;
48 return json;
49
50 #else
51
52 *ok = true;
53 QJson::Serializer serializer;
54 #ifdef ARM
55 QByteArray json = serializer.serialize(data);
56 #else
57 QByteArray json = serializer.serialize(data, ok);
58 #endif
59 if (!*ok) {
60 qDebug() << QString("Sorry, there is a problem with data serializing in JSON::json()");
61 }
62
63 return json;
64 #endif
65
66}
67
68
69QVariant JSON::data(const QByteArray& json) {
70 #if QT_VERSION > 0x050000
71 QJsonParseError error;
72 QJsonDocument jdoc = QJsonDocument::fromJson(json, &error);
73 return jdoc.toVariant();
74 #else
75 bool ok;
76 QJson::Parser parser;
77 return parser.parse(json, &ok);
78 #endif
79}
80
81
82QVariant JSON::data(const QByteArray& json, bool *ok) {
83 #if QT_VERSION > 0x050000
84 QJsonParseError error;
85 QJsonDocument jdoc = QJsonDocument::fromJson(json, &error);
86 *ok = (error.error == QJsonParseError::NoError);
87 return jdoc.toVariant();
88 #else
89 *ok = true;
90 QJson::Parser parser;
91 return parser.parse(json, ok);
92 #endif
93}
94
static QVariant data(const QByteArray &json)
Converts json to data.
Definition json.cpp:69
static QByteArray json(const QVariant &data)
Converts data to json.
Definition json.cpp:21