-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParser.cpp
More file actions
126 lines (103 loc) · 2.33 KB
/
Parser.cpp
File metadata and controls
126 lines (103 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "Parser.h"
using namespace Json;
Parser::Parser(QObject *parent) : QObject(parent)
{
}
Document *Parser::createDocument()
{
Document * d = new Document(this);
d->__root = new Element(this);
return d;
}
QByteArray Parser::toJSONString(Document *doc)
{
Q_ASSERT(doc != 0);
QByteArray ret;
if (doc->root().__value.count())
{
ret.append(QString("[%1]").arg(
QString(toJsonArray((doc->__root), true))));
}
else
{
ret.append(QString("{%1}").arg(
QString(toJsonObject((doc->__root), true))));
}
return ret;
}
QByteArray Parser::toBytea(Document *doc)
{
Q_ASSERT(doc != 0);
QByteArray ret;
return ret;
}
QByteArray Parser::toJsonArray(const Element *element, bool rec)
{
QByteArray ret;
if (rec)
{
if (element->__elements.count())
{
for (int idx = 0; idx < element->__elements.count(); idx++)
{
Element * e = element->__elements.at(idx);
ret.append(QString("\"%1\": {%2},").arg(
e->__keys.at(idx),
QString(toJsonObject(e))));
}
}
}
for (int v = 0; v < element->__value.__data.count(); v++)
{
QVariant value = element->__value.__data.at(v);
ret.append(formatValue(value));
if (element->__value.__data.count() - v > 1)
{
ret.append(",");
}
}
return ret;
}
QByteArray Parser::toJsonObject(const Element *element, bool rec)
{
Q_UNUSED(rec);
QByteArray ret;
for (int i = 0; i < element->__elements.count(); i++)
{
Element * e = element->__elements.at(i);
QString k = element->__keys.at(i);
ret.append(QString("\"%1\": ").arg(k));
if (e->__value.count() == 1)
{
ret.append(QString("%2,").arg(
formatValue(e->__value.at(0))));
}
else if ((e->__value.count() > 1)&&(rec))
{
ret.append(QString("[%2],").arg(
QString(toJsonArray(e))));
}
if (e->__elements.count() > 0)
{
ret.append(QString("{%2},").arg(
QString(toJsonObject(e))));
}
}
return ret.left(ret.length() - 1);
}
QString Parser::formatValue(const QVariant &value)
{
switch (value.type())
{
case QVariant::Double : case QVariant::Int : case QVariant::UInt : case QVariant::Bool :
return QString("%1").arg(value.toString());
break;
case QVariant::String:
return QString("\"%1\"").arg(value.toString());
break;
default:
qDebug("Unsupported format! %d", value.type());
return "null";
break;
}
}