-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonStthm.h
358 lines (298 loc) · 9.2 KB
/
JsonStthm.h
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef __JSON_STTHM_H__
#define __JSON_STTHM_H__
#include "JsonStthmConfig.h"
#include <string.h> //memcpy
#include <stdint.h> //int64_t
namespace JsonStthm
{
class JsonValue;
struct Allocator
{
JsonValue* (*CreateJsonValue) (Allocator* pAllocator, void* pUserData);
void (*DeleteJsonValue) (JsonValue* pValue, void* pUserData);
char* (*AllocString) (size_t iSize, void* pUserData);
void (*FreeString) (char* pAlloc, void* pUserData);
void* pUserData;
};
namespace Internal
{
bool IsNaN(double x);
bool IsInfinite(double x);
template <typename T, size_t HeapSize = 1024>
struct Buffer
{
public:
Buffer(size_t iReserve = 8)
{
m_pData = m_pHeapData;
m_iSize = m_iCapacity = 0;
m_bUseHeap = true;
Reserve(iReserve);
}
Buffer(size_t iSize, const T& oValue)
{
m_pData = m_pHeapData;
m_iSize = m_iCapacity = 0;
m_bUseHeap = true;
Resize(iSize);
for (size_t i = 0; i < iSize; ++i)
{
m_pData[i] = oValue;
}
}
~Buffer()
{
if (!m_bUseHeap)
JsonStthmFree(m_pData);
}
Buffer<T>& operator +=(const T& oValue)
{
Push(oValue);
return *this;
}
void Push(const T& oValue)
{
Resize(m_iSize + 1);
m_pData[m_iSize - 1] = oValue;
}
void PushRange(const T* pBegin, size_t iLength)
{
Resize(m_iSize + iLength);
memcpy(&m_pData[m_iSize - iLength], pBegin, iLength);
}
size_t Size() const
{
return m_iSize;
}
void Reserve(size_t iCapacity, bool bForceAlloc = false)
{
if (iCapacity != m_iCapacity)
{
if (!m_bUseHeap || iCapacity >= HeapSize || bForceAlloc)
{
T* pTemp = (T*)JsonStthmMalloc(iCapacity * sizeof(T));
JsonStthmAssert(pTemp != NULL);
memcpy(pTemp, m_pData, m_iSize * sizeof(T));
if (!m_bUseHeap)
JsonStthmFree(m_pData);
m_pData = pTemp;
m_bUseHeap = false;
}
m_iCapacity = iCapacity;
}
}
void Resize(size_t iSize)
{
size_t iNewCapacity = m_iCapacity > 0 ? m_iCapacity : 8;
while (iSize > iNewCapacity)
iNewCapacity *= 2;
if (iNewCapacity != m_iCapacity)
Reserve(iNewCapacity);
m_iSize = iSize;
}
const T* Data() const { return m_pData; }
T* Take(Allocator* pAllocator)
{
char* pTemp;
if (pAllocator != NULL)
{
pTemp = pAllocator->AllocString(m_iSize * sizeof(T), pAllocator->pUserData);
memcpy(pTemp, m_bUseHeap ? m_pHeapData : m_pData, m_iSize * sizeof(T));
m_iSize = 0;
return pTemp;
}
else if (m_bUseHeap)
{
pTemp = (T*)JsonStthmMalloc(m_iSize * sizeof(T));
memcpy(pTemp, m_pHeapData, m_iSize * sizeof(T));
}
else
{
pTemp = m_pData;
m_pData = NULL;
}
m_iCapacity = 0;
m_iSize = 0;
m_bUseHeap = true;
m_pData = m_pHeapData;
return pTemp;
}
void WriteTo(T* pOut)
{
memcpy(pOut, m_pData, m_iSize * sizeof(T));
}
void Clear()
{
m_iSize = 0;
}
protected:
T m_pHeapData[HeapSize];
T* m_pData;
size_t m_iSize;
size_t m_iCapacity;
bool m_bUseHeap;
};
typedef Buffer<char> CharBuffer;
}
class STTHM_API JsonValue
{
friend class JsonDoc;
public:
enum EType
{
E_TYPE_NULL = 0, //null
E_TYPE_OBJECT, //JsonMembers
E_TYPE_ARRAY, //JsonArray
E_TYPE_STRING, //String
E_TYPE_BOOLEAN, //bool
E_TYPE_INTEGER, //long
E_TYPE_FLOAT //double
};
class STTHM_API Iterator
{
public:
Iterator(const JsonValue* pJson);
Iterator(const Iterator& oIt);
bool IsValid() const;
bool operator!=(const Iterator& oIte) const;
void operator++();
const JsonValue& operator*() const;
const JsonValue* operator->() const;
protected:
const JsonValue* m_pChild;
};
static JsonValue INVALID;
protected:
JsonValue(Allocator* pAllocator);
public:
JsonValue();
JsonValue(const JsonValue& oSource);
JsonValue(bool bValue);
#ifdef JsonStthmString
JsonValue(const JsonStthmString& sValue);
#endif //JsonStthmString
JsonValue(const char* pValue);
JsonValue(int64_t iValue);
JsonValue(double fValue);
~JsonValue();
void InitType(EType eType);
void Reset();
EType GetType() const;
int ReadString(const char* pJson, const char* pJsonEnd = NULL);
int ReadFile(const char* pFilename);
void Write(Internal::CharBuffer& sOutJson, size_t iIndent, bool bCompact) const;
#ifdef JsonStthmString
void WriteString(JsonStthmString& sOutJson, bool bCompact = false) const;
#endif //JsonStthmString
char* WriteString(bool bCompact) const;
bool WriteFile(const char* pFilename, bool bCompact = false) const;
int GetMemberCount() const;
const char* GetName() const { return m_pName; }
const JsonValue* GetNext() const { return m_pNext; }
bool IsValid() const { return this != &JsonValue::INVALID; }
bool IsNull() const { return m_eType == E_TYPE_NULL; }
bool IsObject() const { return m_eType == E_TYPE_OBJECT; }
bool IsArray() const { return m_eType == E_TYPE_ARRAY; }
bool IsString() const { return m_eType == E_TYPE_STRING; }
bool IsBoolean() const { return m_eType == E_TYPE_BOOLEAN; }
bool IsInteger() const { return m_eType == E_TYPE_INTEGER; }
bool IsFloat() const { return m_eType == E_TYPE_FLOAT; }
bool IsNumeric() const { return m_eType == E_TYPE_INTEGER || m_eType == E_TYPE_FLOAT; }
bool IsContainer() const { return m_eType == E_TYPE_ARRAY || m_eType == E_TYPE_OBJECT; }
const char* ToString() const;
bool ToBoolean() const;
int64_t ToInteger() const;
double ToFloat() const;
#ifdef STTHM_ENABLE_IMPLICIT_CAST
operator const char*() const;
operator bool() const;
operator int64_t() const;
operator double() const;
#endif //STTHM_ENABLE_IMPLICIT_CAST
void SetString(const char* pValue);
void SetBoolean(bool bValue);
void SetInteger(int64_t iValue);
void SetFloat(double fValue);
JsonValue& Append();
// bMergeSubMembers parameter is for object type and gonna try to merge sub members when possible
// Other types will just add values
bool Combine(const JsonValue& oRight, bool bMergeSubMembers);
bool operator ==(const JsonValue& oRight) const;
bool operator !=(const JsonValue& oRight) const;
const JsonValue& operator [](const char* pName) const;
JsonValue& operator [](const char* pName);
const JsonValue& operator [](char* pName) const;
JsonValue& operator [](char* pName);
const JsonValue& operator [](int iIndex) const;
JsonValue& operator [](int iIndex);
JsonValue& operator =(const JsonValue& oValue);
#ifdef JsonStthmString
JsonValue& operator =(const JsonStthmString& sValue);
#endif //JsonStthmString
JsonValue& operator =(const char* pValue);
JsonValue& operator =(bool bValue);
JsonValue& operator =(int64_t iValue);
JsonValue& operator =(double fValue);
JsonValue& operator +=(const JsonValue& oValue);
protected:
void SetStringValue(const char* pString);
Allocator* m_pAllocator;
EType m_eType;
char* m_pName;
JsonValue* m_pNext;
struct JsonChilds
{
JsonValue* m_pFirst;
JsonValue* m_pLast;
};
union ValueUnion
{
JsonChilds Childs;
char* String;
bool Boolean;
int64_t Integer;
double Float;
};
ValueUnion m_oValue;
bool Parse(const char*& pString, const char* pEnd);
static inline int ReadSpecialChar(const char*& pString, const char* pEnd, char* pOut);
static inline char* ReadStringValue(const char*& pString, const char* pEnd, Allocator* pAllocator);
static inline bool ReadNumericValue(const char*& pString, const char* pEnd, JsonValue& oValue);
static inline bool ReadObjectValue(const char*& pString, const char* pEnd, JsonValue& oValue);
static inline bool ReadArrayValue(const char*& pString, const char* pEnd, JsonValue& oValue);
static void WriteStringEscaped(Internal::CharBuffer& sOutJson, const char* pBuffer);
static JsonValue* DefaultAllocatorCreateJsonValue(Allocator* pAllocator, void* pUserData);
static void DefaultAllocatorDeleteJsonValue(JsonValue* pValue, void* pUserData);
static char* DefaultAllocatorAllocString(size_t iSize, void* pUserData);
static void DefaultAllocatorFreeString(char* pString, void* pUserData);
static Allocator s_oDefaultAllocator;
};
// Quicker and use less memory than loading a Json with JsonValue, but read only
class STTHM_API JsonDoc
{
public:
JsonDoc(size_t iBlockSize = 4096);
~JsonDoc();
const JsonValue& GetRoot() { return m_oRoot; }
void Clear();
int ReadString(const char* pJson, const char* pJsonEnd = NULL);
int ReadFile(const char* pFilename);
size_t MemoryUsage() const;
protected:
Allocator m_oAllocator;
JsonValue m_oRoot;
struct Block
{
size_t m_iUsed;
Block* m_pPrevious;
};
size_t m_iBlockSize;
Block* m_pLastBlock;
static void* Allocate(JsonDoc* pDoc, size_t iSize, size_t iAlign);
static JsonValue* CreateJsonValue(Allocator* pAllocator, void* pUserData);
static void DeleteJsonValue(JsonValue* pValue, void* pUserData);
static char* AllocString(size_t iSize, void* pUserData);
static void FreeString(char* pString, void* pUserData);
};
}
#endif // __JSON_STTHM_H__