Repository navigation
Expand file tree
/
Copy pathDekiJson.cpp
More file actions
275 lines (243 loc) · 5.89 KB
/
Copy pathDekiJson.cpp
File metadata and controls
275 lines (243 loc) · 5.89 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "DekiJson.h"
#include <deki/LogSystem.h>
// cJSON is vendored under third_party/cJSON/, so this package needs nothing
// from any platform. The package's recursive source glob compiles its .c file.
#include "third_party/cJSON/cJSON.h"
#include <cstring>
namespace DekiJson
{
// ---- ctor / dtor / move ----
Document::Document() = default;
Document::~Document()
{
if (m_Owns && m_Node)
{
cJSON_Delete(static_cast<cJSON*>(m_Node));
}
m_Node = nullptr;
m_Owns = false;
}
Document::Document(Document&& other) noexcept
: m_Node(other.m_Node),
m_Owns(other.m_Owns)
{
other.m_Node = nullptr;
other.m_Owns = false;
}
Document& Document::operator=(Document&& other) noexcept
{
if (this != &other)
{
if (m_Owns && m_Node)
{
cJSON_Delete(static_cast<cJSON*>(m_Node));
}
m_Node = other.m_Node;
m_Owns = other.m_Owns;
other.m_Node = nullptr;
other.m_Owns = false;
}
return *this;
}
// ---- construction ----
Document Document::Parse(const std::string& text)
{
cJSON* root = cJSON_Parse(text.c_str());
if (!root)
{
DEKI_LOG_ERROR("[deki-json] parse failed");
return Document();
}
return Document(root, /*owns=*/true);
}
Document Document::Object()
{
return Document(cJSON_CreateObject(), /*owns=*/true);
}
Document Document::Array()
{
return Document(cJSON_CreateArray(), /*owns=*/true);
}
bool Document::Valid() const
{
return m_Node != nullptr;
}
// ---- reading ----
bool Document::HasKey(const char* key) const
{
if (!m_Node || !key)
{
return false;
}
return cJSON_GetObjectItem(static_cast<cJSON*>(m_Node), key) != nullptr;
}
double Document::GetNumber(const char* key, double fallback) const
{
if (!m_Node || !key)
{
return fallback;
}
cJSON* n = cJSON_GetObjectItem(static_cast<cJSON*>(m_Node), key);
return cJSON_IsNumber(n) ? n->valuedouble : fallback;
}
bool Document::GetBool(const char* key, bool fallback) const
{
if (!m_Node || !key)
{
return fallback;
}
cJSON* n = cJSON_GetObjectItem(static_cast<cJSON*>(m_Node), key);
if (cJSON_IsBool(n))
{
return cJSON_IsTrue(n);
}
return fallback;
}
std::string Document::GetString(const char* key, const char* fallback) const
{
if (!m_Node || !key)
{
return fallback ? fallback : "";
}
cJSON* n = cJSON_GetObjectItem(static_cast<cJSON*>(m_Node), key);
if (cJSON_IsString(n) && n->valuestring)
{
return n->valuestring;
}
return fallback ? fallback : "";
}
Document Document::GetChild(const char* key) const
{
if (!m_Node || !key)
{
return Document();
}
cJSON* n = cJSON_GetObjectItem(static_cast<cJSON*>(m_Node), key);
if (!n)
{
return Document();
}
return Document(n, /*owns=*/false); // borrowed view
}
int Document::Size() const
{
if (!m_Node)
{
return 0;
}
return cJSON_GetArraySize(static_cast<cJSON*>(m_Node));
}
Document Document::GetAt(int index) const
{
if (!m_Node || index < 0)
{
return Document();
}
cJSON* n = cJSON_GetArrayItem(static_cast<cJSON*>(m_Node), index);
if (!n)
{
return Document();
}
return Document(n, /*owns=*/false);
}
// ---- building ----
// Puts `item` under `key`, replacing a member already there. cJSON's Add*
// functions append even when the key exists, which left {"n":1,"n":2} and
// reads that kept returning the first value. Lookups are case-insensitive, as
// GetNumber and the other readers are. Takes ownership of `item`.
static void SetMember(cJSON* object, const char* key, cJSON* item)
{
if (!item)
{
return;
}
const bool stored = cJSON_GetObjectItem(object, key) ? cJSON_ReplaceItemInObject(object, key, item)
: cJSON_AddItemToObject(object, key, item);
if (!stored)
{
cJSON_Delete(item);
}
}
void Document::SetNumber(const char* key, double value)
{
if (!m_Node || !key)
{
return;
}
SetMember(static_cast<cJSON*>(m_Node), key, cJSON_CreateNumber(value));
}
void Document::SetBool(const char* key, bool value)
{
if (!m_Node || !key)
{
return;
}
SetMember(static_cast<cJSON*>(m_Node), key, cJSON_CreateBool(value));
}
void Document::SetString(const char* key, const std::string& value)
{
if (!m_Node || !key)
{
return;
}
SetMember(static_cast<cJSON*>(m_Node), key, cJSON_CreateString(value.c_str()));
}
// The node to link into another tree. A borrowed view (GetChild / GetAt)
// still belongs to its own tree, so it is copied: linked as-is, one node
// would sit in two trees and both roots would free it.
static cJSON* TakeForLinking(void*& node, bool& owns)
{
cJSON* taken = owns ? static_cast<cJSON*>(node) : cJSON_Duplicate(static_cast<cJSON*>(node), /*recurse=*/1);
if (owns)
{
node = nullptr;
owns = false;
}
return taken;
}
void Document::SetChild(const char* key, Document child)
{
if (!m_Node || !key || !child.Valid())
{
return;
}
cJSON* node = TakeForLinking(child.m_Node, child.m_Owns);
if (!node)
{
return;
}
SetMember(static_cast<cJSON*>(m_Node), key, node);
}
void Document::PushBack(Document child)
{
if (!m_Node || !child.Valid())
{
return;
}
cJSON* node = TakeForLinking(child.m_Node, child.m_Owns);
if (!node)
{
return;
}
if (!cJSON_AddItemToArray(static_cast<cJSON*>(m_Node), node))
{
cJSON_Delete(node);
}
}
// ---- serialization ----
std::string Document::Dump() const
{
if (!m_Node)
{
return {};
}
char* s = cJSON_PrintUnformatted(static_cast<cJSON*>(m_Node));
if (!s)
{
return {};
}
std::string out(s);
cJSON_free(s);
return out;
}
} // namespace DekiJson