-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonFile.cpp
243 lines (242 loc) · 8.25 KB
/
JsonFile.cpp
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
241
242
//---------------------------------------------------------------------------
#include "agdv.pch.h"
#pragma hdrstop
//---------------------------------------------------------------------------
#include <stack>
#include "JsonFile.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
namespace System
{
//---------------------------------------------------------------------------
__fastcall JsonFile::JsonFile()
: TPersistent()
, m_StringWriter(nullptr)
, m_JsonWriter(nullptr)
{
}
//---------------------------------------------------------------------------
__fastcall JsonFile::~JsonFile()
{
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Open(const String& file)
{
m_File = file;
m_StringWriter = new TStringWriter();
m_JsonWriter = new TJsonTextWriter(m_StringWriter);
m_JsonWriter->Formatting = TJsonFormatting::Indented;
m_JsonWriter->WriteStartObject();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Close()
{
if (m_StringWriter != nullptr && m_JsonWriter != nullptr)
{
m_JsonWriter->WriteEndObject();
File::File::WriteText(m_File, m_StringWriter->ToString());
delete m_JsonWriter;
m_JsonWriter = nullptr;
delete m_StringWriter;
m_StringWriter = nullptr;
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::StartObject() const
{
m_JsonWriter->WriteStartObject();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::EndObject() const
{
m_JsonWriter->WriteEndObject();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Push(const String& section) const
{
m_JsonWriter->WritePropertyName(section);
m_JsonWriter->WriteStartObject();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Pop() const
{
m_JsonWriter->WriteEndObject();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::ArrayStart(const String& property) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteStartArray();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::ArrayEnd() const
{
m_JsonWriter->WriteEndArray();
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& value) const
{
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const int& value) const
{
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& property, const String& value) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& property, const int& value) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& property, const unsigned int& value) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& property, const float& value) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Write(const String& property, const bool& value) const
{
m_JsonWriter->WritePropertyName(property);
m_JsonWriter->WriteValue(value);
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Set(const String& property, const String& value)
{
auto size = m_PropertyMap.size();
if (m_PropertyMap.count(property) != 0)
{
((*(String*)m_PropertyMap[property])) = value;
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Set(const String& property, const int& value)
{
if (m_PropertyMap.count(property) != 0)
{
((*(int*)m_PropertyMap[property])) = value;
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Set(const String& property, const unsigned int& value)
{
if (m_PropertyMap.count(property) != 0)
{
((*(unsigned int*)m_PropertyMap[property])) = value;
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Set(const String& property, const float& value)
{
if (m_PropertyMap.count(property) != 0)
{
((*(float*)m_PropertyMap[property])) = value;
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Set(const String& property, const bool& value)
{
if (m_PropertyMap.count(property) != 0)
{
((*(bool*)m_PropertyMap[property])) = value;
}
}
//---------------------------------------------------------------------------
String __fastcall JsonFile::ProcessPath(const String& path) const
{
String newPath = "";
auto skip = false;
for (auto i = 1; i <= path.Length(); i++)
{
if (path[i] == '[')
{
skip = true;
newPath += path[i];
}
else if (skip && path[i] == ']')
{
skip = false;
newPath += path[i];
}
else if (!skip)
{
newPath += path[i];
}
}
return newPath;
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::Load(const String& file)
{
if (File::File::Exists(file))
{
auto json = File::File::ReadText(file);
auto sr = std::make_unique<TStringReader>(json);
auto jr = std::make_unique<TJsonTextReader>(sr.get());
auto inArray = false;
while (jr->Read())
{
auto path = ProcessPath(jr->Path);
switch (jr->TokenType)
{
case TJsonToken::StartObject:
OnStartObject(path);
break;
case TJsonToken::EndObject:
OnEndObject(path);
break;
case TJsonToken::StartArray:
inArray = true;
break;
case TJsonToken::EndArray:
inArray = false;
break;
case TJsonToken::PropertyName:
break;
case TJsonToken::String:
Set(path, jr->Value.AsString());
if (inArray) OnEndObject(path);
break;
case TJsonToken::Integer:
Set(path, jr->Value.AsInteger());
if (inArray) OnEndObject(path);
break;
case TJsonToken::Float:
Set(path, (float)jr->Value.AsExtended());
if (inArray) OnEndObject(path);
break;
case TJsonToken::Boolean:
Set(path, jr->Value.AsBoolean());
if (inArray) OnEndObject(path);
break;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::OnStartObject(const String& object)
{
// do nothing; should be overridden
}
//---------------------------------------------------------------------------
void __fastcall JsonFile::OnEndObject(const String& object)
{
// do nothing; should be overridden
}
//---------------------------------------------------------------------------
} // namespace File
//---------------------------------------------------------------------------