forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSerializer.mqh
240 lines (202 loc) · 6.73 KB
/
JsonSerializer.mqh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef JSON_SERIALIZER_MQH
#define JSON_SERIALIZER_MQH
// Includes.
#include "DictBase.mqh"
#include "JsonIterator.mqh"
#include "JsonNode.mqh"
#include "JsonParam.mqh"
#include "Log.mqh"
enum JsonSerializerEnterMode { JsonEnterArray, JsonEnterObject };
enum JsonSerializerMode { JsonSerialize, JsonUnserialize };
class JsonSerializer {
protected:
JsonNode* _node;
JsonNode* _root;
JsonSerializerMode _mode;
Ref<Log> _logger;
public:
/**
* Constructor.
*/
JsonSerializer(JsonNode* node, JsonSerializerMode mode) : _node(node), _mode(mode) {
_root = node;
_logger = new Log();
}
/**
* Destructor.
*/
~JsonSerializer() {
if (_root != NULL) delete _root;
}
/**
* Returns logger object.
*/
Log* Logger() { return _logger.Ptr(); }
template <typename X>
JsonIterator<X> Begin() {
JsonIterator<X> iter(&this, _node);
return iter;
}
/**
* Enters object or array for a given key or just iterates over objects/array during unserializing.
*/
void Enter(JsonSerializerEnterMode mode, string key = "") {
if (IsWriting()) {
JsonParam* nameParam = (key != NULL && key != "") ? JsonParam::FromString(key) : NULL;
// When writing, we need to make parent->child structure. It is not
// required when reading, because structure is full done by parsing the
// string.
_node = new JsonNode(mode == JsonEnterObject ? JsonNodeObject : JsonNodeArray, _node, nameParam);
if (_node.GetParent() != NULL) _node.GetParent().AddChild(_node);
if (_root == NULL) _root = _node;
} else {
JsonNode* child;
if (key != "") {
// We need to enter object that matches given key.
for (unsigned int i = 0; i < _node.NumChildren(); ++i) {
child = _node.GetChild(i);
if (child.GetKeyParam().AsString(false, false) == key) {
_node = child;
return;
}
}
} else if (key == "") {
child = _node.GetNextChild();
if (!child)
Print("End of objects during JSON deserialization! There were only ", _node.NumChildren(), " nodes!");
_node = child;
}
}
}
/**
* Leaves current object/array. Used in custom Serialize() method.
*/
void Leave() { _node = _node.GetParent(); }
/**
* Checks whether we are in serialization process. Used in custom Serialize() method.
*/
bool IsWriting() { return _mode == JsonSerialize; }
/**
* Checks whether we are in unserialization process. Used in custom Serialize() method.
*/
bool IsReading() { return _mode == JsonUnserialize; }
/**
* Checks whether current node is an array. Used in custom Serialize() method.
*/
bool IsArray() { return _mode == JsonUnserialize && _node != NULL && _node.GetType() == JsonNodeArray; }
/**
* Checks whether current node is an object. Used in custom Serialize() method.
*/
bool IsObject() { return _mode == JsonUnserialize && _node != NULL && _node.GetType() == JsonNodeObject; }
/**
* Returns number of child nodes.
*/
unsigned int NumChildren() { return _node ? _node.NumChildren() : 0; }
/**
* Returns root node or NULL. Could be used after unserialization.
*/
JsonNode* GetRoot() { return _root; }
/**
* Returns child node for a given index or NULL.
*/
JsonNode* GetChild(unsigned int index) { return _node ? _node.GetChild(index) : NULL; }
/**
* Serializes or unserializes object.
*/
template <typename T, typename V>
void PassObject(T& self, string name, V& value) {
PassStruct(self, name, value);
}
/**
* Serializes or unserializes structure.
*/
template <typename T, typename V>
void PassStruct(T& self, string name, V& value) {
Enter(JsonEnterObject, name);
JsonNodeType newType = value.Serialize(this);
if (newType != JsonNodeUnknown) _node.SetType(newType);
Leave();
}
/**
* Serializes or unserializes enum value (stores it as integer).
*/
template <typename T, typename V>
void PassEnum(T& self, string name, V& value) {
int enumValue;
if (_mode == JsonSerialize) {
enumValue = (int)value;
Pass(self, name, enumValue);
} else {
Pass(self, name, enumValue);
value = (V)enumValue;
}
}
/**
* Serializes or unserializes pointer to object.
*/
template <typename T, typename V>
void Pass(T& self, string name, V*& value) {
if (_mode == JsonSerialize) {
PassObject(self, name, value);
} else {
V* newborn = new V();
PassObject(self, name, newborn);
value = newborn;
}
}
/**
* Serializes or unserializes simple value.
*/
template <typename T, typename V>
void Pass(T& self, string name, V& value) {
if (_mode == JsonSerialize) {
JsonParam* key = name != "" ? JsonParam::FromString(name) : NULL;
JsonParam* val = JsonParam::FromValue(value);
_node.AddChild(new JsonNode(JsonNodeObjectProperty, _node, key, val));
} else {
for (unsigned int i = 0; i < _node.NumChildren(); ++i) {
JsonNode* child = _node.GetChild(i);
if (child.GetKeyParam().AsString(false, false) == name) {
JsonParamType paramType = child.GetValueParam().GetType();
switch (paramType) {
case JsonParamBool:
value = (V)child.GetValueParam()._integral._bool;
break;
case JsonParamLong:
value = (V)child.GetValueParam()._integral._long;
break;
case JsonParamDouble:
value = (V)child.GetValueParam()._integral._double;
break;
case JsonParamString:
value = (V)child.GetValueParam()._string;
break;
}
return;
}
}
}
}
};
#endif