-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIJansson.h
302 lines (249 loc) · 9 KB
/
IJansson.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
#ifndef _INCLUDE_SOURCEMOD_JANSSON_IFACE_H_
#define _INCLUDE_SOURCEMOD_JANSSON_IFACE_H_
#include <sm_platform.h>
#include <IShareSys.h>
#include <IHandleSys.h>
#include <ISourceMod.h>
#include <vector>
#include <cstring>
#include <functional>
#define SMINTERFACE_JANSSON_NAME "IJansson"
/**
* @brief IFace version (32 bits)
*
* @details
* 8 high bits - major \n
* 12 bits - minor \n
* 12 low bits - bugs \n
*
*/
#define SMINTERFACE_JANSSON_VERSION ((3 << 24) & (1 << 12) & 0)
namespace nJansson
{
enum JsonType
{
jtInvalid = -1,
jtObject,
jtArray,
jtString,
jtInteger,
jtReal,
jtBoolean,
jtNull
};
enum JsonDecodingFlag
{
RejectDuplicates = 0x1, /**< Error if any JSON object contains duplicate keys */
DisableEOFCheck = 0x2, /**< Allow extra data after a valid JSON array or object */
DecodeAny = 0x4, /**< Decode any value */
DecodeIntegerAsReal = 0x8, /**< Interpret all numbers as floats */
AllowNull = 0x10 /**< Allow \u0000 escape inside string values */
};
enum JsonEncodingFlag
{
Compact = 0x20, /**< Compact representation */
EnsureASCII = 0x40, /**< Escape all Unicode characters outside the ASCII range */
SortKeys = 0x80, /**< Sort object keys */
EncodeAny = 0x200, /**< Encode any value */
EscapeSlash = 0x400, /**< Escape / with \/ */
Embed = 0x10000
};
enum JsonObjectUpdateType
{
utDefault,
utExisting,
utMissing,
utRecursive
};
class IHandleType;
using IHT = IHandleType;
class IHandleTypeManager;
using UHTM = IHandleTypeManager;
class IJson;
using IJS = IJson;
class IJsonObject;
using IJSO = IJsonObject;
class IJsonArray;
using IJSA = IJsonArray;
class IJsonObjectKeyIterator;
using IJSOI = IJsonObjectKeyIterator;
struct JsonError_t {
int line;
int column;
int position;
char source[PLATFORM_MAX_PATH];
char text[PLATFORM_MAX_PATH];
bool equal(const JsonError_t& another) const {
return line == another.line
&& column == another.column
&& position == another.position
&& std::strcmp(source, another.source) == 0
&& std::strcmp(text, another.text) == 0;
}
};
const JsonError_t JSON_ERROR_NULL = {};
class IJansson : public SourceMod::SMInterface
{
public:
const char *GetInterfaceName() override {
return SMINTERFACE_JANSSON_NAME;
}
unsigned int GetInterfaceVersion() override {
return SMINTERFACE_JANSSON_VERSION;
}
/**
* @brief Checking interface version compatibility
*
* @param version
*
* @return true if major equal
*/
bool IsVersionCompatible(unsigned int version) override {
return (((GetInterfaceVersion() >> 24) & (version >> 24)) == 1);
}
public:
/**
*
* @return Handle type manager (ref with IHandleType)
*/
virtual IHandleTypeManager* types() const =0;
public:
/**
* @brief Creating json wrapper via string
*
* @param str Json string
* @param flags Decoding flags
* @param pError Error pointer (may be nullptr)
*
* @return wrapped json on success
*
* */
virtual IJson *create(const char *, const size_t &flags, JsonError_t* pError) =0;
/**
* @brief Creating json wrapper via file
* @param input File pointer
* @param flags Decoding flags
* @param pError Error pointer (may be nullptr)
*
* @return wrapped json on success
*/
virtual IJson *create(FILE *input, const size_t &flags, JsonError_t* pError) =0;
/**
* @brief Creating json wrapper via SourceMod formatted path
*
* @param type path type
* @param path path
* @param flags decoding flags
* @param pError Error pointer (may be nullptr)
*
* @return wrapped json on success
*/
virtual IJson *create(const SourceMod::PathType&, const char*, const size_t& flags, JsonError_t* pError) =0;
public:
/**
* @brief Safely memory freeing under the wrapper
*
* @param json
*
*/
virtual void close(IJson* json) =0;
};
class IJsonObject
{
public:
virtual IJson* get(const char *key) const =0;
public:
virtual bool set(const char *key, const IJson *value) =0;
virtual bool set(const char *key, const char* value) =0;
virtual bool set(const char *key, double value) =0;
virtual bool set(const char *key, bool value) =0;
virtual bool set(const char *key, long long value) =0;
public:
virtual bool update(const IJsonObject* another, JsonObjectUpdateType type) =0;
virtual JsonType type(const char *key) const =0;
virtual bool exist(const char *key) const =0;
virtual bool remove(const char *key) =0;
public:
virtual IJsonArray* keys(const JsonType& type, const size_t& flags) const =0;
};
class IJsonArray
{
public:
virtual IJson *get(const size_t& index) const =0;
public:
virtual bool set(const size_t& index, const IJson *value) =0;
virtual bool set(const size_t& index, const char* value) =0;
virtual bool set(const size_t& index, double value) =0;
virtual bool set(const size_t& index, bool value) =0;
virtual bool set(const size_t& index, long long value) =0;
public:
virtual bool push(const IJson *value) =0;
virtual bool push(const char* value) =0;
virtual bool push(double value) =0;
virtual bool push(bool value) =0;
virtual bool push(long long value) =0;
public:
virtual JsonType type(const size_t& index) const =0;
virtual bool extend(const IJsonArray *another) =0;
virtual bool remove(const size_t& index) =0;
};
class IJson : public IJsonArray, public IJsonObject
{
public:
/// @brief must be freed via free()
virtual const char* dump(const size_t& flags) const =0;
virtual bool dump(char* buff, const size_t& maxl, const size_t &decodingFlags) const =0;
virtual int dump(const char* path, const size_t& flags) const =0;
public:
virtual bool equal(const IJson* json) const =0;
virtual IJson* find(const JsonType& type, const std::function<bool(const IJson*)>& condition) const =0;
public:
virtual bool get(long long *value) =0;
virtual bool get(double *value) =0;
virtual bool get(bool *value) =0;
virtual const char* get() =0;
public:
virtual size_t size() const =0;
virtual void clear() =0;
public:
virtual JsonType type() const =0;
public:
virtual bool isOK() const =0;
};
class IHandleType
{
public:
virtual const char *name() const =0;
virtual SourceMod::IHandleTypeDispatch* dispatch() const =0;
virtual SourceMod::HandleType_t parent() const =0;
virtual const SourceMod::TypeAccess* typeAccess() const =0;
virtual const SourceMod::HandleAccess* handleAccess() const =0;
virtual const SourceMod::IdentityToken_t *identity() const =0;
public:
virtual SourceMod::Handle_t createHandle(void *object,
SourceMod::IdentityToken_t* pOwner,
SourceMod::IdentityToken_t* pIdent,
SourceMod::HandleError* pHandleError) const =0;
virtual SourceMod::Handle_t createHandle(void *object,
const SourceMod::HandleSecurity* owner,
const SourceMod::HandleAccess* access,
SourceMod::HandleError* error) const =0;
public:
virtual SourceMod::HandleType_t id() const =0;
};
class IHandleTypeManager
{
public:
virtual SourceMod::HandleType_t add(const IHandleType* pHType) =0;
virtual void remove(const SourceMod::HandleType_t&) =0;
public:
virtual const IHandleType* find(const char* name) const =0;
virtual const IHandleType* find(const size_t& index) const =0;
virtual const IHandleType* find_t(const SourceMod::HandleType_t&) const =0;
public:
virtual size_t count() const =0;
protected:
virtual const std::vector<const IHandleType *>& container() const =0;
};
}
#endif